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\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<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();
$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);
}
}
/**