BUG Fix behat tests unable to capture HTML editor fields

This commit is contained in:
Damian Mooyman 2016-11-29 14:49:39 +13:00
parent 6552dce70e
commit 9ec1d35f2b
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A

View File

@ -11,7 +11,8 @@ use Behat\Behat\Context\ClosuredContextInterface,
Behat\Gherkin\Node\PyStringNode, Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode, Behat\Gherkin\Node\TableNode,
Behat\MinkExtension\Context\MinkContext as MinkContext; Behat\MinkExtension\Context\MinkContext as MinkContext;
use Behat\Mink\Element\NodeElement;
use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\DomCrawler\Crawler;
// PHPUnit // PHPUnit
@ -74,13 +75,9 @@ class CmsFormsContext extends BehatContext {
* @When /^I fill in "(?P<value>(?:[^"]|\\")*)" for the "(?P<field>(?:[^"]|\\")*)" HTML field$/ * @When /^I fill in "(?P<value>(?:[^"]|\\")*)" for the "(?P<field>(?:[^"]|\\")*)" HTML field$/
*/ */
public function stepIFillInTheHtmlFieldWith($field, $value) { public function stepIFillInTheHtmlFieldWith($field, $value) {
$field = $this->fixStepArgument($field); $inputField = $this->getHtmlField($field);
$value = $this->fixStepArgument($value); $value = $this->fixStepArgument($value);
$page = $this->getSession()->getPage();
$inputField = $page->findField($field);
assertNotNull($inputField, sprintf('HTML field "%s" not found', $field));
$this->getSession()->evaluateScript(sprintf( $this->getSession()->evaluateScript(sprintf(
"jQuery('#%s').entwine('ss').getEditor().setContent('%s')", "jQuery('#%s').entwine('ss').getEditor().setContent('%s')",
$inputField->getAttribute('id'), $inputField->getAttribute('id'),
@ -92,13 +89,9 @@ class CmsFormsContext extends BehatContext {
* @When /^I append "(?P<value>(?:[^"]|\\")*)" to the "(?P<field>(?:[^"]|\\")*)" HTML field$/ * @When /^I append "(?P<value>(?:[^"]|\\")*)" to the "(?P<field>(?:[^"]|\\")*)" HTML field$/
*/ */
public function stepIAppendTotheHtmlField($field, $value) { public function stepIAppendTotheHtmlField($field, $value) {
$field = $this->fixStepArgument($field); $inputField = $this->getHtmlField($field);
$value = $this->fixStepArgument($value); $value = $this->fixStepArgument($value);
$page = $this->getSession()->getPage();
$inputField = $page->findField($field);
assertNotNull($inputField, sprintf('HTML field "%s" not found', $field));
$this->getSession()->evaluateScript(sprintf( $this->getSession()->evaluateScript(sprintf(
"jQuery('#%s').entwine('ss').getEditor().insertContent('%s')", "jQuery('#%s').entwine('ss').getEditor().insertContent('%s')",
$inputField->getAttribute('id'), $inputField->getAttribute('id'),
@ -110,11 +103,7 @@ class CmsFormsContext extends BehatContext {
* @Then /^the "(?P<locator>(?:[^"]|\\")*)" HTML field should(?P<negative> not? |\s*)contain "(?P<html>.*)"$/ * @Then /^the "(?P<locator>(?:[^"]|\\")*)" HTML field should(?P<negative> not? |\s*)contain "(?P<html>.*)"$/
*/ */
public function theHtmlFieldShouldContain($locator, $negative, $html) { public function theHtmlFieldShouldContain($locator, $negative, $html) {
$locator = $this->fixStepArgument($locator); $element = $this->getHtmlField($locator);
$page = $this->getSession()->getPage();
$element = $page->findField($locator);
assertNotNull($element, sprintf('HTML field "%s" not found', $locator));
$actual = $element->getValue(); $actual = $element->getValue();
$regex = '/'.preg_quote($html, '/').'/ui'; $regex = '/'.preg_quote($html, '/').'/ui';
$failed = false; $failed = false;
@ -154,10 +143,7 @@ class CmsFormsContext extends BehatContext {
* @Given /^"(?P<text>([^"]*))" in the "(?P<field>(?:[^"]|\\")*)" HTML field should(?P<negate>(?: not)?) be (?P<formatting>(.*))$/ * @Given /^"(?P<text>([^"]*))" in the "(?P<field>(?:[^"]|\\")*)" HTML field should(?P<negate>(?: not)?) be (?P<formatting>(.*))$/
*/ */
public function stepContentInHtmlFieldShouldHaveFormatting($text, $field, $negate, $formatting) { public function stepContentInHtmlFieldShouldHaveFormatting($text, $field, $negate, $formatting) {
$field = $this->fixStepArgument($field); $inputField = $this->getHtmlField($field);
$page = $this->getSession()->getPage();
$inputField = $page->findField($field);
assertNotNull($inputField, sprintf('HTML field "%s" not found', $field));
$crawler = new Crawler($inputField->getValue()); $crawler = new Crawler($inputField->getValue());
$matchedNode = null; $matchedNode = null;
@ -192,10 +178,7 @@ class CmsFormsContext extends BehatContext {
* @When /^I select "(?P<text>([^"]*))" in the "(?P<field>(?:[^"]|\\")*)" HTML field$/ * @When /^I select "(?P<text>([^"]*))" in the "(?P<field>(?:[^"]|\\")*)" HTML field$/
*/ */
public function stepIHighlightTextInHtmlField($text, $field) { public function stepIHighlightTextInHtmlField($text, $field) {
$field = $this->fixStepArgument($field); $inputField = $this->getHtmlField($field);
$page = $this->getSession()->getPage();
$inputField = $page->findField($field);
assertNotNull($inputField, sprintf('HTML field "%s" not found', $field));
$inputFieldId = $inputField->getAttribute('id'); $inputFieldId = $inputField->getAttribute('id');
$text = addcslashes($text, "'"); $text = addcslashes($text, "'");
@ -266,4 +249,19 @@ JS;
} }
} }
/**
* Locate a HTML editor field
*
* @param string $locator Raw html field identifier as passed from
* @return NodeElement
*/
protected function getHtmlField($locator)
{
$locator = $this->fixStepArgument($locator);
$page = $this->getSession()->getPage();
$element = $page->find('css', 'textarea.htmleditor[name=\'' . $locator . '\']');
assertNotNull($element, sprintf('HTML field "%s" not found', $locator));
return $element;
}
} }