diff --git a/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php b/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php index 1eaba63b4..9d263dcc9 100644 --- a/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php +++ b/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php @@ -6,8 +6,9 @@ use Behat\Behat\Context\ClosuredContextInterface, Behat\Behat\Context\TranslatedContextInterface, Behat\Behat\Context\BehatContext, Behat\Behat\Context\Step, - Behat\Behat\Exception\PendingException; -use Behat\Gherkin\Node\PyStringNode, + Behat\Behat\Exception\PendingException, + Behat\Mink\Exception\ElementNotFoundException, + Behat\Gherkin\Node\PyStringNode, Behat\Gherkin\Node\TableNode; @@ -263,7 +264,7 @@ class CmsUiContext extends BehatContext } /** - * Workaround for chosen.js dropdowns which hide the original dropdown field. + * Workaround for chosen.js dropdowns or tree dropdowns which hide the original dropdown field. * * @When /^(?:|I )fill in "(?P(?:[^"]|\\")*)" dropdown with "(?P(?:[^"]|\\")*)"$/ * @When /^(?:|I )fill in "(?P(?:[^"]|\\")*)" for "(?P(?:[^"]|\\")*)" dropdown$/ @@ -272,32 +273,55 @@ class CmsUiContext extends BehatContext { $field = $this->fixStepArgument($field); $value = $this->fixStepArgument($value); + $nativeField = $this->getSession()->getPage()->findField($field); + if($nativeField) { + $nativeField->selectOption($value); + } else { + // TODO Allow searching by label + $inputField = $this->getSession()->getPage()->find('xpath', "//*[@name='$field']"); + if(null === $inputField) { + throw new \InvalidArgumentException(sprintf( + 'Chosen.js dropdown named "%s" not found', + $field + )); + } - $inputField = $this->getSession()->getPage()->findField($field); - if(null === $inputField) { - throw new ElementNotFoundException(sprintf( - 'Chosen.js dropdown named "%s" not found', - $field - )); + do { + $container = $inputField->getParent(); + $containerClasses = explode(' ', $container->getAttribute('class')); + } while( + $container + && in_array('field', $containerClasses) + && $container->getTagName() != 'form' + ); + if(null === $container) throw new \InvalidArgumentException('Chosen.js field container not found'); + + $triggerEl = $container->find('xpath', './/a'); + if(null === $triggerEl) throw new \InvalidArgumentException('Chosen.js link element not found'); + $triggerEl->click(); + + if(in_array('treedropdown', $containerClasses)) { + // wait for ajax dropdown to load + $this->getSession()->wait( + 5000, + "jQuery('#" . $container->getAttribute('id') . " .treedropdownfield-panel li').length > 0" + ); + } else { + // wait for dropdown overlay to appear + $this->getSession()->wait(100); + } + + $listEl = $container->find('xpath', sprintf('.//li[contains(normalize-space(string(.)), \'%s\')]', $value)); + if(null === $listEl) { + throw new \InvalidArgumentException(sprintf( + 'Chosen.js list element with title "%s" not found', + $value + )); + } + $listEl->find('xpath', './/a')->click(); } - $container = $inputField->getParent()->getParent(); - if(null === $container) throw new ElementNotFoundException('Chosen.js field container not found'); - $linkEl = $container->find('xpath', './/a'); - if(null === $linkEl) throw new ElementNotFoundException('Chosen.js link element not found'); - $linkEl->click(); - $this->getSession()->wait(100); // wait for dropdown overlay to appear - - $listEl = $container->find('xpath', sprintf('.//li[contains(normalize-space(string(.)), \'%s\')]', $value)); - if(null === $listEl) - { - throw new ElementNotFoundException(sprintf( - 'Chosen.js list element with title "%s" not found', - $value - )); - } - $listEl->click(); } /** diff --git a/tests/behat/features/manage-files.feature b/tests/behat/features/manage-files.feature index 25dd8a549..dd5bca9fd 100644 --- a/tests/behat/features/manage-files.feature +++ b/tests/behat/features/manage-files.feature @@ -49,7 +49,7 @@ Feature: Manage files Scenario: I can change the folder of a file Given I click on "folder1" in the "Files" table And I click on "file1" in the "folder1" table - And I fill in =>Folder.folder2 for "ParentID" + And I fill in "folder2" for "ParentID" dropdown And I press the "Save" button # /show/0 is to ensure that we are on top level folder And I go to "/admin/assets/show/0"