diff --git a/features/taxonomy/managing_taxons/editing_taxon_slug_in_multiple_locales.feature b/features/taxonomy/managing_taxons/editing_taxon_slug_in_multiple_locales.feature index 395dde89d19..7e8dae2475b 100644 --- a/features/taxonomy/managing_taxons/editing_taxon_slug_in_multiple_locales.feature +++ b/features/taxonomy/managing_taxons/editing_taxon_slug_in_multiple_locales.feature @@ -3,7 +3,7 @@ Feature: Editing taxon's slug in multiple locales In order to manage access path to taxon page in many languages As an Administrator I want to be able to edit taxon's slug in multiple locales - + Background: Given the store is available in "English (United States)" And the store is also available in "Polish (Poland)" diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingProductsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingProductsContext.php index 14121cfd694..c87faa43b10 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingProductsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingProductsContext.php @@ -417,7 +417,7 @@ public function theCodeFieldShouldBeDisabled() */ public function theSlugFieldShouldNotBeEditable($localeCode = 'en_US') { - Assert::true($this->updateSimpleProductPage->isSlugReadOnlyIn($localeCode)); + Assert::true($this->updateSimpleProductPage->isSlugReadonlyIn($localeCode)); } /** diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php index 8e7d93f96c5..ebdff7afd95 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingPromotionsContext.php @@ -155,9 +155,7 @@ public function iAddTheHasTaxonRuleConfiguredWith(...$taxons) { $this->createPage->addRule('Has at least one from taxons'); - foreach ($taxons as $taxon) { - $this->createPage->selectAutocompleteRuleOption('Taxons', $taxon, true); - } + $this->createPage->selectAutocompleteRuleOption('Taxons', $taxons, true); } /** diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php index ab4f5c10839..d655157a363 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php @@ -138,7 +138,7 @@ public function iSetItsSlugToIn($slug = null, $language = 'en_US') */ public function theSlugFieldShouldNotBeEditable($language = 'en_US') { - Assert::true($this->updatePage->isSlugReadOnly($language)); + Assert::true($this->updatePage->isSlugReadonly($language)); } /** diff --git a/src/Sylius/Behat/Page/Admin/Product/CreateConfigurableProductPage.php b/src/Sylius/Behat/Page/Admin/Product/CreateConfigurableProductPage.php index 072249742d1..428ec7fe704 100644 --- a/src/Sylius/Behat/Page/Admin/Product/CreateConfigurableProductPage.php +++ b/src/Sylius/Behat/Page/Admin/Product/CreateConfigurableProductPage.php @@ -15,6 +15,7 @@ use Behat\Mink\Element\NodeElement; use Sylius\Behat\Behaviour\SpecifiesItsCode; use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage; +use Sylius\Behat\Service\SlugGenerationHelper; use Webmozart\Assert\Assert; /** @@ -33,7 +34,9 @@ public function nameItIn($name, $localeCode) sprintf('sylius_product_translations_%s_name', $localeCode), $name ); - $this->waitForSlugGenerationIfNecessary(); + if ($this->getDriver() instanceof Selenium2Driver) { + SlugGenerationHelper::waitForSlugGeneration($this->getSession(), $this->getElement('slug')); + } } /** @@ -100,13 +103,4 @@ private function getLastImageElement() return end($items); } - - private function waitForSlugGenerationIfNecessary() - { - if ($this->getDriver() instanceof Selenium2Driver) { - $this->getDocument()->waitFor(10, function () { - return '' !== $this->getElement('slug')->getValue(); - }); - } - } } diff --git a/src/Sylius/Behat/Page/Admin/Product/CreateSimpleProductPage.php b/src/Sylius/Behat/Page/Admin/Product/CreateSimpleProductPage.php index 9a1f7e40ab6..13b96870ba7 100644 --- a/src/Sylius/Behat/Page/Admin/Product/CreateSimpleProductPage.php +++ b/src/Sylius/Behat/Page/Admin/Product/CreateSimpleProductPage.php @@ -15,6 +15,7 @@ use Behat\Mink\Element\NodeElement; use Sylius\Behat\Behaviour\SpecifiesItsCode; use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage; +use Sylius\Behat\Service\SlugGenerationHelper; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Currency\Model\CurrencyInterface; use Sylius\Component\Product\Model\ProductAssociationTypeInterface; @@ -44,7 +45,12 @@ public function nameItIn($name, $localeCode) $this->activateLanguageTab($localeCode); $this->getElement('name', ['%locale%' => $localeCode])->setValue($name); - $this->waitForSlugGenerationIfNecessary($localeCode); + if ($this->getDriver() instanceof Selenium2Driver) { + SlugGenerationHelper::waitForSlugGeneration( + $this->getSession(), + $this->getElement('slug', ['%locale%' => $localeCode]) + ); + } } /** @@ -342,16 +348,4 @@ private function getLastImageElement() return end($items); } - - /** - * @param string $locale - */ - private function waitForSlugGenerationIfNecessary($locale) - { - if ($this->getDriver() instanceof Selenium2Driver) { - $this->getDocument()->waitFor(10, function () use ($locale) { - return '' !== $this->getElement('slug', ['%locale%' => $locale])->getValue(); - }); - } - } } diff --git a/src/Sylius/Behat/Page/Admin/Product/UpdateSimpleProductPage.php b/src/Sylius/Behat/Page/Admin/Product/UpdateSimpleProductPage.php index f4a41e1411f..cdcacbb0cb8 100644 --- a/src/Sylius/Behat/Page/Admin/Product/UpdateSimpleProductPage.php +++ b/src/Sylius/Behat/Page/Admin/Product/UpdateSimpleProductPage.php @@ -16,6 +16,7 @@ use Sylius\Behat\Behaviour\ChecksCodeImmutability; use Sylius\Behat\Page\Admin\Crud\UpdatePage as BaseUpdatePage; use Sylius\Behat\Service\AutocompleteHelper; +use Sylius\Behat\Service\SlugGenerationHelper; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Currency\Model\CurrencyInterface; @@ -38,7 +39,12 @@ public function nameItIn($name, $localeCode) $this->activateLanguageTab($localeCode); $this->getElement('name', ['%locale%' => $localeCode])->setValue($name); - $this->waitForSlugGenerationIfNecessary($localeCode); + if ($this->getDriver() instanceof Selenium2Driver) { + SlugGenerationHelper::waitForSlugGeneration( + $this->getSession(), + $this->getElement('slug', ['%locale%' => $localeCode]) + ); + } } /** @@ -113,8 +119,6 @@ public function selectMainTaxon(TaxonInterface $taxon) { $this->openTaxonBookmarks(); - Assert::isInstanceOf($this->getDriver(), Selenium2Driver::class); - $mainTaxonElement = $this->getElement('main_taxon')->getParent(); AutocompleteHelper::chooseValue($this->getSession(), $mainTaxonElement, $taxon->getName()); @@ -153,7 +157,10 @@ public function isTracked() */ public function enableSlugModification($locale) { - $this->getElement('toggle_slug_modification_button', ['%locale%' => $locale])->press(); + SlugGenerationHelper::enableSlugModification( + $this->getSession(), + $this->getElement('toggle_slug_modification_button', ['%locale%' => $locale]) + ); } /** @@ -248,9 +255,12 @@ public function countImages() /** * {@inheritdoc} */ - public function isSlugReadOnlyIn($locale) + public function isSlugReadonlyIn($locale) { - return 'readonly' === $this->getElement('slug', ['%locale%' => $locale])->getAttribute('readonly'); + return SlugGenerationHelper::isSlugReadonly( + $this->getSession(), + $this->getElement('slug', ['%locale%' => $locale]) + ); } /** @@ -517,26 +527,6 @@ private function getFirstImageElement() return reset($imageElements); } - /** - * @param string $locale - */ - private function waitForSlugGenerationIfNecessary($locale) - { - if (!$this->getDriver() instanceof Selenium2Driver) { - return; - } - - $slugElement = $this->getElement('slug', ['%locale%' => $locale]); - if ($slugElement->hasAttribute('readonly')) { - return; - } - - $value = $slugElement->getValue(); - $this->getDocument()->waitFor(10, function () use ($slugElement, $value) { - return $value !== $slugElement->getValue(); - }); - } - /** * @param NodeElement $imageElement * @param string $type diff --git a/src/Sylius/Behat/Page/Admin/Product/UpdateSimpleProductPageInterface.php b/src/Sylius/Behat/Page/Admin/Product/UpdateSimpleProductPageInterface.php index f2dcc9db3c6..5fc50f1a949 100644 --- a/src/Sylius/Behat/Page/Admin/Product/UpdateSimpleProductPageInterface.php +++ b/src/Sylius/Behat/Page/Admin/Product/UpdateSimpleProductPageInterface.php @@ -29,9 +29,11 @@ interface UpdateSimpleProductPageInterface extends BaseUpdatePageInterface public function isCodeDisabled(); /** + * @param string $locale + * * @return bool */ - public function isSlugReadOnlyIn($locale); + public function isSlugReadonlyIn($locale); /** * @param string $channelName diff --git a/src/Sylius/Behat/Page/Admin/Promotion/CreatePage.php b/src/Sylius/Behat/Page/Admin/Promotion/CreatePage.php index 0653d531912..2066414309b 100644 --- a/src/Sylius/Behat/Page/Admin/Promotion/CreatePage.php +++ b/src/Sylius/Behat/Page/Admin/Promotion/CreatePage.php @@ -66,6 +66,12 @@ public function selectAutocompleteRuleOption($option, $value, $multiple = false) ->getParent() ; + if ($multiple && is_array($value)) { + AutocompleteHelper::chooseValues($this->getSession(), $ruleAutocomplete, $value); + + return; + } + AutocompleteHelper::chooseValue($this->getSession(), $ruleAutocomplete, $value); } @@ -200,6 +206,12 @@ public function selectAutoCompleteFilterOption($option, $value, $multiple = fals ->getParent() ; + if ($multiple && is_array($value)) { + AutocompleteHelper::chooseValues($this->getSession(), $filterAutocomplete, $value); + + return; + } + AutocompleteHelper::chooseValue($this->getSession(), $filterAutocomplete, $value); } diff --git a/src/Sylius/Behat/Page/Admin/Promotion/CreatePageInterface.php b/src/Sylius/Behat/Page/Admin/Promotion/CreatePageInterface.php index b328c5f3fdc..88c364d7fd7 100644 --- a/src/Sylius/Behat/Page/Admin/Promotion/CreatePageInterface.php +++ b/src/Sylius/Behat/Page/Admin/Promotion/CreatePageInterface.php @@ -44,7 +44,7 @@ public function selectRuleOption($option, $value, $multiple = false); /** * @param string $option - * @param string $value + * @param string|string[] $value * @param bool $multiple */ public function selectAutocompleteRuleOption($option, $value, $multiple = false); @@ -120,7 +120,7 @@ public function getValidationMessageForAction(); /** * @param string $option - * @param string $value + * @param string|string[] $value * @param bool $multiple */ public function selectAutoCompleteFilterOption($option, $value, $multiple = false); diff --git a/src/Sylius/Behat/Page/Admin/Taxon/CreatePage.php b/src/Sylius/Behat/Page/Admin/Taxon/CreatePage.php index 3d6f1f13015..7a835365a44 100644 --- a/src/Sylius/Behat/Page/Admin/Taxon/CreatePage.php +++ b/src/Sylius/Behat/Page/Admin/Taxon/CreatePage.php @@ -16,6 +16,7 @@ use Behat\Mink\Exception\ElementNotFoundException; use Sylius\Behat\Behaviour\SpecifiesItsCode; use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage; +use Sylius\Behat\Service\SlugGenerationHelper; use Sylius\Component\Core\Model\TaxonInterface; use Webmozart\Assert\Assert; @@ -91,7 +92,12 @@ public function nameIt($name, $languageCode) $this->activateLanguageTab($languageCode); $this->getElement('name', ['%language%' => $languageCode])->setValue($name); - $this->waitForSlugGenerationIfNecessary($languageCode); + if ($this->getDriver() instanceof Selenium2Driver) { + SlugGenerationHelper::waitForSlugGeneration( + $this->getSession(), + $this->getElement('slug', ['%language%' => $languageCode]) + ); + } } /** @@ -194,16 +200,4 @@ private function getLastImageElement() return end($items); } - - /** - * @param string $languageCode - */ - private function waitForSlugGenerationIfNecessary($languageCode) - { - if ($this->getDriver() instanceof Selenium2Driver) { - $this->getDocument()->waitFor(10, function () use ($languageCode) { - return '' !== $this->getElement('slug', ['%language%' => $languageCode])->getValue(); - }); - } - } } diff --git a/src/Sylius/Behat/Page/Admin/Taxon/UpdatePage.php b/src/Sylius/Behat/Page/Admin/Taxon/UpdatePage.php index d5415fc91ea..155f61719d5 100644 --- a/src/Sylius/Behat/Page/Admin/Taxon/UpdatePage.php +++ b/src/Sylius/Behat/Page/Admin/Taxon/UpdatePage.php @@ -17,6 +17,7 @@ use Sylius\Behat\Behaviour\ChecksCodeImmutability; use Sylius\Behat\Page\Admin\Crud\UpdatePage as BaseUpdatePage; use Sylius\Behat\Service\AutocompleteHelper; +use Sylius\Behat\Service\SlugGenerationHelper; use Sylius\Component\Core\Model\TaxonInterface; use Webmozart\Assert\Assert; @@ -51,7 +52,12 @@ public function nameIt($name, $languageCode) $this->activateLanguageTab($languageCode); $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_name', $languageCode), $name); - $this->waitForSlugGenerationIfNecessary(); + if ($this->getDriver() instanceof Selenium2Driver) { + SlugGenerationHelper::waitForSlugGeneration( + $this->getSession(), + $this->getElement('slug', ['%language%' => $languageCode]) + ); + } } /** @@ -101,9 +107,12 @@ public function isImageWithTypeDisplayed($type) /** * {@inheritdoc} */ - public function isSlugReadOnly($languageCode = 'en_US') + public function isSlugReadonly($languageCode = 'en_US') { - return 'readonly' === $this->getElement('slug', ['%language%' => $languageCode])->getAttribute('readonly'); + return SlugGenerationHelper::isSlugReadonly( + $this->getSession(), + $this->getElement('slug', ['%language%' => $languageCode]) + ); } /** @@ -126,7 +135,10 @@ public function removeFirstImage() */ public function enableSlugModification($languageCode = 'en_US') { - $this->getElement('toggle_taxon_slug_modification_button', ['%locale%' => $languageCode])->press(); + SlugGenerationHelper::enableSlugModification( + $this->getSession(), + $this->getElement('toggle_taxon_slug_modification_button', ['%locale%' => $languageCode]) + ); } /** @@ -313,24 +325,4 @@ private function getImageElementByType($type) return $typeInput->getParent()->getParent()->getParent(); } - - /** - * @param string $languageCode - */ - private function waitForSlugGenerationIfNecessary($languageCode = 'en_US') - { - if (!$this->getDriver() instanceof Selenium2Driver) { - return; - } - - $slugElement = $this->getElement('slug', ['%language%' => $languageCode]); - if ($slugElement->hasAttribute('readonly')) { - return; - } - - $value = $slugElement->getValue(); - $this->getDocument()->waitFor(10, function () use ($slugElement, $value) { - return $value !== $slugElement->getValue(); - }); - } } diff --git a/src/Sylius/Behat/Page/Admin/Taxon/UpdatePageInterface.php b/src/Sylius/Behat/Page/Admin/Taxon/UpdatePageInterface.php index 13b8651e446..344f49851cb 100644 --- a/src/Sylius/Behat/Page/Admin/Taxon/UpdatePageInterface.php +++ b/src/Sylius/Behat/Page/Admin/Taxon/UpdatePageInterface.php @@ -66,7 +66,7 @@ public function isImageWithTypeDisplayed($type); * * @return bool */ - public function isSlugReadOnly($languageCode = 'en_US'); + public function isSlugReadonly($languageCode = 'en_US'); /** * @param string $type diff --git a/src/Sylius/Behat/Service/AutocompleteHelper.php b/src/Sylius/Behat/Service/AutocompleteHelper.php index df3c15c7119..8168cdb5641 100644 --- a/src/Sylius/Behat/Service/AutocompleteHelper.php +++ b/src/Sylius/Behat/Service/AutocompleteHelper.php @@ -30,15 +30,44 @@ public static function chooseValue(Session $session, NodeElement $element, $valu { Assert::isInstanceOf($session->getDriver(), Selenium2Driver::class); - static::waitForAsynchronousActionsToFinish($session); + static::activateAutocompleteDropdown($session, $element); - $element->click(); + $element->find('css', sprintf('div.item:contains("%s")', $value))->click(); - static::waitForAsynchronousActionsToFinish($session); static::waitForElementToBeVisible($session, $element); + } - $element->find('css', sprintf('div.item:contains("%s")', $value))->click(); + /** + * @param Session $session + * @param NodeElement $element + * @param string[] $values + */ + public static function chooseValues(Session $session, NodeElement $element, array $values) + { + Assert::isInstanceOf($session->getDriver(), Selenium2Driver::class); + + static::activateAutocompleteDropdown($session, $element); + + foreach ($values as $value) { + $element->find('css', sprintf('div.item:contains("%s")', $value))->click(); + static::waitForAsynchronousActionsToFinish($session); + } + + static::waitForElementToBeVisible($session, $element); + } + + /** + * @param Session $session + * @param NodeElement $element + */ + private static function activateAutocompleteDropdown(Session $session, NodeElement $element) + { + static::waitForAsynchronousActionsToFinish($session); + + $element->click(); + + static::waitForAsynchronousActionsToFinish($session); static::waitForElementToBeVisible($session, $element); } diff --git a/src/Sylius/Behat/Service/SlugGenerationHelper.php b/src/Sylius/Behat/Service/SlugGenerationHelper.php new file mode 100644 index 00000000000..1bbfae2edec --- /dev/null +++ b/src/Sylius/Behat/Service/SlugGenerationHelper.php @@ -0,0 +1,103 @@ + + */ +abstract class SlugGenerationHelper +{ + /** + * @param Session $session + * @param NodeElement $element + */ + public static function waitForSlugGeneration(Session $session, NodeElement $element) + { + Assert::isInstanceOf($session->getDriver(), Selenium2Driver::class); + + static::waitForAsynchronousActionsToFinish($session); + static::isElementReadonly($session, $element); + static::waitForAsynchronousActionsToFinish($session); + } + + /** + * @param Session $session + * @param NodeElement $element + */ + public static function enableSlugModification(Session $session, NodeElement $element) + { + Assert::isInstanceOf($session->getDriver(), Selenium2Driver::class); + + static::waitForAsynchronousActionsToFinish($session); + static::waitForElementToBeClickable($session, $element); + + $element->click(); + + static::waitForAsynchronousActionsToFinish($session); + } + + /** + * @param Session $session + * @param NodeElement $element + * + * @return bool + */ + public static function isSlugReadonly(Session $session, NodeElement $element) + { + if (!$session->getDriver() instanceof Selenium2Driver) { + return $element->hasAttribute('readonly'); + } + + static::waitForAsynchronousActionsToFinish($session); + + return static::isElementReadonly($session, $element); + } + + /** + * @param Session $session + * @param NodeElement $element + */ + private static function waitForElementToBeClickable(Session $session, NodeElement $element) + { + $session->wait(5000, sprintf( + 'false === $(document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue).hasClass("loading")', + $element->getParent()->getParent()->getXpath() + )); + } + + /** + * @param Session $session + * @param NodeElement $element + * + * @return bool + */ + private static function isElementReadonly(Session $session, NodeElement $element) + { + return $session->wait(5000, sprintf( + 'undefined != $(document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue).attr("readonly")', + $element->getXpath() + )); + } + + /** + * @param Session $session + */ + private static function waitForAsynchronousActionsToFinish(Session $session) + { + $session->wait(5000, '0 === jQuery.active'); + } +}