From 0de0c7a87b6bbdf14da8927eea4cea691f337fb9 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Sat, 18 May 2024 17:57:53 +0200 Subject: [PATCH] Try to rely on Sylius Behat helpers to know if a form or an ajax request is pending --- .../Application/public/media/image/.gitignore | 0 .../Element/Admin/OrderCreateFormElement.php | 50 +++++-------------- .../Behat/Page/Admin/NewOrderCustomerPage.php | 9 +--- tests/Behat/Page/Admin/OrderPreviewPage.php | 8 +-- tests/Behat/Service/AutoCompleteSelector.php | 40 +++++---------- 5 files changed, 30 insertions(+), 77 deletions(-) delete mode 100644 tests/Application/public/media/image/.gitignore diff --git a/tests/Application/public/media/image/.gitignore b/tests/Application/public/media/image/.gitignore deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/Behat/Element/Admin/OrderCreateFormElement.php b/tests/Behat/Element/Admin/OrderCreateFormElement.php index ad3d61f6..79eae124 100644 --- a/tests/Behat/Element/Admin/OrderCreateFormElement.php +++ b/tests/Behat/Element/Admin/OrderCreateFormElement.php @@ -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; @@ -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); } @@ -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 @@ -232,22 +233,9 @@ 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'); @@ -255,11 +243,6 @@ private function addItemAndWaitForIt(): NodeElement 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) { @@ -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 diff --git a/tests/Behat/Page/Admin/NewOrderCustomerPage.php b/tests/Behat/Page/Admin/NewOrderCustomerPage.php index 1c64c7be..c101835c 100644 --- a/tests/Behat/Page/Admin/NewOrderCustomerPage.php +++ b/tests/Behat/Page/Admin/NewOrderCustomerPage.php @@ -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 @@ -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 diff --git a/tests/Behat/Page/Admin/OrderPreviewPage.php b/tests/Behat/Page/Admin/OrderPreviewPage.php index a57658c6..2d0c3585 100644 --- a/tests/Behat/Page/Admin/OrderPreviewPage.php +++ b/tests/Behat/Page/Admin/OrderPreviewPage.php @@ -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); } @@ -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); } diff --git a/tests/Behat/Service/AutoCompleteSelector.php b/tests/Behat/Service/AutoCompleteSelector.php index 5e6466e2..3fb674e7 100644 --- a/tests/Behat/Service/AutoCompleteSelector.php +++ b/tests/Behat/Service/AutoCompleteSelector.php @@ -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'); } }