Behat: More robust "field should contain" logic

This commit is contained in:
Ingo Schommer 2013-06-07 15:56:51 +02:00
parent 924664527b
commit fd6060e7be

View File

@ -6,8 +6,9 @@ use Behat\Behat\Context\ClosuredContextInterface,
Behat\Behat\Context\TranslatedContextInterface, Behat\Behat\Context\TranslatedContextInterface,
Behat\Behat\Context\BehatContext, Behat\Behat\Context\BehatContext,
Behat\Behat\Context\Step, Behat\Behat\Context\Step,
Behat\Behat\Exception\PendingException; Behat\Behat\Exception\PendingException,
use Behat\Gherkin\Node\PyStringNode, Behat\Mink\Exception\ElementHtmlException,
Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode; Behat\Gherkin\Node\TableNode;
// PHPUnit // PHPUnit
@ -88,19 +89,25 @@ class CmsFormsContext extends BehatContext
} }
/** /**
* @Then /^the "(?P<field>([^"]*))" HTML field should contain "(?P<value>([^"]*))"$/ * @Then /^the "(?P<locator>([^"]*))" HTML field should contain "(?P<html>([^"]*))"$/
*/ */
public function theHtmlFieldShouldContain($field, $value) public function theHtmlFieldShouldContain($locator, $html)
{ {
$page = $this->getSession()->getPage(); $page = $this->getSession()->getPage();
$inputField = $page->findField($field); $element = $page->findField($locator);
assertNotNull($inputField, sprintf('HTML field "%s" not found', $field)); assertNotNull($element, sprintf('HTML field "%s" not found', $locator));
$this->getMainContext()->assertSession()->elementContains( $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', 'named',
$field, $locator
str_replace('\\"', '"', $value) // emulates fixStepArgument()
); );
throw new ElementHtmlException($message, $this->getSession(), $element);
}
} }
/** /**