From a0ac37fa1ead4c58e63fdb823b8f35402ed03cb7 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sat, 14 Sep 2013 19:27:19 +0200 Subject: [PATCH] More useful fillField() implementation Was defaulting to the first non-visible --- .../Context/SilverStripeContext.php | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php b/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php index a8acac7..25350a3 100644 --- a/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php +++ b/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php @@ -10,7 +10,8 @@ use Behat\Gherkin\Node\PyStringNode; use Behat\MinkExtension\Context\MinkContext; use Behat\Mink\Driver\GoutteDriver, Behat\Mink\Driver\Selenium2Driver, - Behat\Mink\Exception\UnsupportedDriverActionException; + Behat\Mink\Exception\UnsupportedDriverActionException, + Behat\Mink\Exception\ElementNotFoundException; use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface; @@ -278,4 +279,26 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex return new Step\Given($step); } + /** + * Fills in form field with specified id|name|label|value. + * Overwritten to select the first *visible* element, see https://github.com/Behat/Mink/issues/311 + */ + public function fillField($field, $value) + { + $value = $this->fixStepArgument($value); + $fields = $this->getSession()->getPage()->findAll('named', array( + 'field', $this->getSession()->getSelectorsHandler()->xpathLiteral($field) + )); + if($fields) foreach($fields as $field) { + if($field->isVisible()) { + $field->setValue($value); + return; + } + } + + throw new ElementNotFoundException( + $this->getSession(), 'form field', 'id|name|label|value', $field + ); + } + }