From ff5624c57eb67858c6e2a75da85ce26f0adb3152 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 7 Jun 2013 11:45:09 +0200 Subject: [PATCH] BUG Fixed dropdown step definition for "preview" dropdowns Broke after I optimized it to work with a TreeDropdownField which assumes
  • structures that thie "preview" dropdowns don't have. I also failed at the recursion assignment, causing infinite loops... --- .../Framework/Test/Behaviour/CmsUiContext.php | 51 ++++++++++++------- 1 file changed, 33 insertions(+), 18 deletions(-) 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 fe5dea693..55e738cc7 100644 --- a/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php +++ b/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php @@ -9,7 +9,8 @@ use Behat\Behat\Context\ClosuredContextInterface, Behat\Behat\Exception\PendingException, Behat\Mink\Exception\ElementNotFoundException, Behat\Gherkin\Node\PyStringNode, - Behat\Gherkin\Node\TableNode; + Behat\Gherkin\Node\TableNode, + Behat\Mink\Element\NodeElement; // PHPUnit @@ -355,31 +356,21 @@ class CmsUiContext extends BehatContext )); // Traverse up to field holder - $containers = array(); + $container = null; foreach($formFields as $formField) { - do { - $container = $formField->getParent(); - $containerClasses = explode(' ', $container->getAttribute('class')); - $containers[] = $container; - } while( - $container - && in_array('field', $containerClasses) - && $container->getTagName() != 'form' - ); + $container = $this->findParentByClass($formField, 'field'); + if($container) break; // Default to first visible container } - assertGreaterThan(0, count($containers), 'Chosen.js field container not found'); + assertNotNull($container, 'Chosen.js field container not found'); - // Default to first visible container - $container = $containers[0]; - // Click on newly expanded list element, indirectly setting the dropdown value $linkEl = $container->find('xpath', './/a[./@href]'); assertNotNull($linkEl, 'Chosen.js link element not found'); $this->getSession()->wait(100); // wait for dropdown overlay to appear $linkEl->click(); - if(in_array('treedropdown', $containerClasses)) { + if(in_array('treedropdown', explode(' ', $container->getAttribute('class')))) { // wait for ajax dropdown to load $this->getSession()->wait( 5000, @@ -390,7 +381,6 @@ class CmsUiContext extends BehatContext $this->getSession()->wait(300); } - $listEl = $container->find('xpath', sprintf('.//li[contains(normalize-space(string(.)), \'%s\')]', $value)); if(null === $listEl) { throw new \InvalidArgumentException(sprintf( @@ -399,7 +389,12 @@ class CmsUiContext extends BehatContext )); } - $listEl->find('xpath', './/a')->click(); + $listLinkEl = $listEl->find('xpath', './/a'); + if($listLinkEl) { + $listLinkEl->click(); + } else { + $listEl->click(); + } } /** @@ -413,4 +408,24 @@ class CmsUiContext extends BehatContext { return str_replace('\\"', '"', $argument); } + + /** + * Returns the closest parent element having a specific class attribute. + * + * @param NodeElement $el + * @param String $class + * @return Element|null + */ + protected function findParentByClass(NodeElement $el, $class) { + $container = $el->getParent(); + while($container && $container->getTagName() != 'body' + ) { + if($container->isVisible() && in_array($class, explode(' ', $container->getAttribute('class')))) { + return $container; + } + $container = $container->getParent(); + } + + return null; + } }