Skip to content

Commit

Permalink
Try to rely on Sylius Behat helpers to know if a form or an ajax requ…
Browse files Browse the repository at this point in the history
…est is pending
  • Loading branch information
Prometee committed May 18, 2024
1 parent 49359d1 commit 0de0c7a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 77 deletions.
Empty file.
50 changes: 13 additions & 37 deletions tests/Behat/Element/Admin/OrderCreateFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\Exception;
use Behat\Mink\Session;
use Sylius\Behat\Service\JQueryHelper;
use Sylius\Component\Core\Model\AddressInterface;
use Tests\Sylius\AdminOrderCreationPlugin\Behat\Element\Element;
use Tests\Sylius\AdminOrderCreationPlugin\Behat\Service\AutoCompleteSelector;
Expand All @@ -29,18 +30,18 @@ public function __construct(
public function addProduct(string $productVariantDescriptor): void
{
$this->clickOnTabAndWait('Items');
$item = $this->addItemAndWaitForIt();

$this->autoCompleteSelector->selectOption($item, $productVariantDescriptor);
$item = $this->addItemAndReturnIt();
$this->autoCompleteSelector->selectOption($this->getSession(), $item, $productVariantDescriptor);
}

public function addMultipleProducts(string $productVariantDescriptor, int $quantity): void
{
$this->clickOnTabAndWait('Items');

$item = $this->addItemAndWaitForIt();
$item = $this->addItemAndReturnIt();

$this->autoCompleteSelector->selectOption($item, $productVariantDescriptor);
$this->autoCompleteSelector->selectOption($this->getSession(), $item, $productVariantDescriptor);
$item->fillField('Quantity', (string) $quantity);
}

Expand All @@ -56,9 +57,9 @@ public function areProductsVisible(): bool
{
$this->clickOnTabAndWait('Items');

$item = $this->addItemAndWaitForIt();
$item = $this->addItemAndReturnIt();

return $this->autoCompleteSelector->areItemsVisible($item);
return $this->autoCompleteSelector->areItemsVisible($this->getSession(), $item);
}

public function specifyShippingAddress(AddressInterface $address): void
Expand Down Expand Up @@ -232,34 +233,16 @@ private function selectMethod(string $type, string $field, string $name, bool $a
$collection->selectFieldOption($field, $name);
}

private function addItemAndWaitForIt(): NodeElement
private function addItemAndReturnIt(): NodeElement
{
$itemsCount = $this->countItems();
$this->getDocument()->waitFor(10, function () {
try {
$this->getDocument()->clickLink('Add');

return true;
} catch (Exception $exception) {
return false;
}
});

$this->getDocument()->waitFor(1, function () use ($itemsCount) {
return $this->countItems() > $itemsCount;
});
$this->getDocument()->clickLink('Add');

/** @var NodeElement $lastItem */
$lastItem = $this->getDocument()->find('css', '#items [data-form-collection="item"]:last-child');

return $lastItem;
}

private function countItems(): int
{
return count($this->getDocument()->findAll('css', '#items [data-form-collection="item"]'));
}

private function getItemWithProductSelected(string $productVariantDescriptor): NodeElement
{
foreach ($this->getDocument()->findAll('css', '#items [data-form-collection="item"]') as $item) {
Expand All @@ -279,28 +262,21 @@ private function clickOnTabAndWait(string $tabName): void
{
/** @var NodeElement $tab */
$tab = $this->getDocument()->find('css', sprintf('.title:contains("%s")', $tabName));

if ($tab->hasClass('active')) {
return;
}

$tab->click();

$this->getDocument()->waitFor(5, function () use ($tabName) {
/** @var NodeElement $title */
$title = $this->getDocument()->find('css', sprintf('.title:contains("%s") + .content', $tabName));
/** @var NodeElement $tabContent */
$tabContent = $this->getDocument()->find('css', sprintf('.title:contains("%s") + .content', $tabName));

return $title->hasClass('active');
});
$this->getDocument()->waitFor(5, fn () => $tabContent->hasClass('active'));
}

private function waitForFormToLoad(): void
{
/** @var NodeElement $form */
$form = $this->getDocument()->find('css', '[name="sylius_admin_order_creation_new_order"]');
$this->getDocument()->waitFor(1000, function () use ($form) {
return !$form->hasClass('loading');
});
JQueryHelper::waitForAsynchronousActionsToFinish($this->getSession());
}

public function isAddPaymentButtonVisible(): bool
Expand Down
9 changes: 2 additions & 7 deletions tests/Behat/Page/Admin/NewOrderCustomerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@

final class NewOrderCustomerPage extends SymfonyPage implements NewOrderCustomerPageInterface
{
/** @var AutoCompleteSelector */
private $autoCompleteSelector;

public function __construct(
Session $session,
$parameters,
RouterInterface $router,
AutoCompleteSelector $autoCompleteSelector,
private AutoCompleteSelector $autoCompleteSelector,
) {
parent::__construct($session, $parameters, $router);

$this->autoCompleteSelector = $autoCompleteSelector;
}

public function getRouteName(): string
Expand All @@ -32,7 +27,7 @@ public function getRouteName(): string

public function selectCustomer(string $customerEmail): void
{
$this->autoCompleteSelector->selectOption($this->getDocument(), $customerEmail);
$this->autoCompleteSelector->selectOption($this->getSession(), $this->getDocument(), $customerEmail);
}

public function next(): void
Expand Down
8 changes: 2 additions & 6 deletions tests/Behat/Page/Admin/OrderPreviewPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ public function lowerOrderPriceBy(string $discount): void
$discountCollection = $this->getDocument()->find('css', '#sylius_admin_order_creation_new_order_adjustments');

$discountCollection->clickLink('Add discount');
$this->getDocument()->waitFor(1, function () use ($discountCollection) {
return $discountCollection->has('css', '[data-form-collection="item"]');
});
$this->getDocument()->waitFor(1, fn () => $discountCollection->has('css', '[data-form-collection="item"]'));

$discountCollection->fillField('Order discount', $discount);
}
Expand All @@ -104,9 +102,7 @@ public function lowerItemWithProductPriceBy(string $productCode, string $discoun
/** @var NodeElement $discountCollection */
$discountCollection = $item->find('css', '[data-form-type="collection"]');

$this->getDocument()->waitFor(1, function () use ($discountCollection) {
return $discountCollection->has('css', '[data-form-collection="item"]');
});
$this->getDocument()->waitFor(1, fn () => $discountCollection->has('css', '[data-form-collection="item"]'));

$discountCollection->fillField('Item discount', $discount);
}
Expand Down
40 changes: 13 additions & 27 deletions tests/Behat/Service/AutoCompleteSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,27 @@
namespace Tests\Sylius\AdminOrderCreationPlugin\Behat\Service;

use Behat\Mink\Element\ElementInterface;
use Behat\Testwork\Call\Exception\FatalThrowableError;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Session;
use Sylius\Behat\Service\AutocompleteHelper;
use Sylius\Behat\Service\JQueryHelper;

final class AutoCompleteSelector
{
public function selectOption(ElementInterface $scope, string $optionName): void
public function selectOption(Session $session, ElementInterface $scope, string $optionName): void
{
$this->waitForItemsToLoad($scope);

$scope->waitFor(10, function () use ($scope, $optionName) {
try {
$scope->find('css', sprintf('.sylius-autocomplete .menu .item:contains("%s")', $optionName))->click();

return true;
} catch (FatalThrowableError $exception) {
return false;
}
});
/** @var NodeElement $autocomplete */
$autocomplete = $scope->find('css', '.sylius-autocomplete');
AutocompleteHelper::chooseValue($session, $autocomplete, $optionName);
}

public function areItemsVisible(ElementInterface $scope): bool
public function areItemsVisible(Session $session, ElementInterface $scope): bool
{
$this->waitForItemsToLoad($scope);
JQueryHelper::waitForAsynchronousActionsToFinish($session);

return strpos($scope->find('css', '.sylius-autocomplete .menu')->getText(), 'No results found') !== false;
}

private function waitForItemsToLoad(ElementInterface $scope): void
{
$scope->find('css', '.sylius-autocomplete .icon')->click();
/** @var NodeElement $menu */
$menu = $scope->find('css', '.sylius-autocomplete .menu');

$scope->waitFor(100, function () use ($scope) {
return $scope
->find('css', '.sylius-autocomplete .menu')
->hasClass('visible')
;
});
return str_contains($menu->getText(), 'No results found');
}
}

0 comments on commit 0de0c7a

Please sign in to comment.