From fd6060e7be2ddd4f863e48d486384baa38a63e12 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 7 Jun 2013 15:56:51 +0200 Subject: [PATCH] Behat: More robust "field should contain" logic --- .../Test/Behaviour/CmsFormsContext.php | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php b/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php index 8183a7948..fe3381c23 100644 --- a/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php +++ b/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.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\ElementHtmlException, + Behat\Gherkin\Node\PyStringNode, Behat\Gherkin\Node\TableNode; // PHPUnit @@ -88,19 +89,25 @@ class CmsFormsContext extends BehatContext } /** - * @Then /^the "(?P([^"]*))" HTML field should contain "(?P([^"]*))"$/ + * @Then /^the "(?P([^"]*))" HTML field should contain "(?P([^"]*))"$/ */ - public function theHtmlFieldShouldContain($field, $value) + public function theHtmlFieldShouldContain($locator, $html) { $page = $this->getSession()->getPage(); - $inputField = $page->findField($field); - assertNotNull($inputField, sprintf('HTML field "%s" not found', $field)); + $element = $page->findField($locator); + assertNotNull($element, sprintf('HTML field "%s" not found', $locator)); - $this->getMainContext()->assertSession()->elementContains( - 'named', - $field, - str_replace('\\"', '"', $value) // emulates fixStepArgument() - ); + $actual = $element->getAttribute('value'); + $regex = '/'.preg_quote($html, '/').'/ui'; + if (!preg_match($regex, $actual)) { + $message = sprintf( + 'The string "%s" was not found in the HTML of the element matching %s "%s".', + $html, + 'named', + $locator + ); + throw new ElementHtmlException($message, $this->getSession(), $element); + } } /**