From e9d999d6485168f112c0796fdf842b696257ba8a Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 13 Nov 2012 18:18:49 +0100 Subject: [PATCH] Support for chosen.js drop downs in behat steps --- .../Framework/Test/Behaviour/CmsUiContext.php | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) 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 ed7deb48c..85200c835 100644 --- a/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php +++ b/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php @@ -202,7 +202,7 @@ class CmsUiContext extends BehatContext public function theTableShouldContain($table, $text) { $table_element = $this->getGridfieldTable($table); -var_dump($table_element); + $element = $table_element->find('named', array('content', "'$text'")); assertNotNull($element, sprintf('Element containing `%s` not found in `%s` table', $text, $table)); } @@ -261,4 +261,54 @@ var_dump($table_element); $this->getMainContext()->assertPageNotContainsText($content); $driver->switchToWindow(); } + + /** + * Workaround for chosen.js dropdowns which hide the original dropdown field. + * + * @When /^(?:|I )fill in "(?P(?:[^"]|\\")*)" dropdown with "(?P(?:[^"]|\\")*)"$/ + * @When /^(?:|I )fill in "(?P(?:[^"]|\\")*)" for "(?P(?:[^"]|\\")*)" dropdown$/ + */ + public function theIFillInTheDropdownWith($field, $value) + { + $field = $this->fixStepArgument($field); + $value = $this->fixStepArgument($value); + + $inputField = $this->getSession()->getPage()->findField($field); + if(null === $inputField) { + throw new ElementNotFoundException(sprintf( + 'Chosen.js dropdown named "%s" not found', + $field + )); + } + + $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(); + } + + /** + * Returns fixed step argument (with \\" replaced back to "). + * + * @param string $argument + * + * @return string + */ + protected function fixStepArgument($argument) + { + return str_replace('\\"', '"', $argument); + } }