Permalink
Browse files

BUG Fixed dropdown step definition for "preview" dropdowns

Broke after I optimized it to work with a TreeDropdownField
which assumes <li><a> structures that thie "preview" dropdowns
don't have. I also failed at the recursion assignment, causing
infinite loops...
  • Loading branch information...
1 parent 59fb88b commit ff5624c57eb67858c6e2a75da85ce26f0adb3152 @chillu chillu committed Jun 7, 2013
Showing with 33 additions and 18 deletions.
  1. +33 −18 tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsUiContext.php
@@ -9,7 +9,8 @@
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 @@ public function theIFillInTheDropdownWith($field, $value)
));
// 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 @@ public function theIFillInTheDropdownWith($field, $value)
$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 @@ public function theIFillInTheDropdownWith($field, $value)
));
}
- $listEl->find('xpath', './/a')->click();
+ $listLinkEl = $listEl->find('xpath', './/a');
+ if($listLinkEl) {
+ $listLinkEl->click();
+ } else {
+ $listEl->click();
+ }
}
/**
@@ -413,4 +408,24 @@ protected function fixStepArgument($argument)
{
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;
+ }
}

0 comments on commit ff5624c

Please sign in to comment.