From 9ec1d35f2bd09bee50d3a3629d9589f8871abd98 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Tue, 29 Nov 2016 14:49:39 +1300 Subject: [PATCH] BUG Fix behat tests unable to capture HTML editor fields --- .../Test/Behaviour/CmsFormsContext.php | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 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 bacd1d11a..07604d3ff 100644 --- a/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php +++ b/tests/behat/features/bootstrap/SilverStripe/Framework/Test/Behaviour/CmsFormsContext.php @@ -11,7 +11,8 @@ use Behat\Behat\Context\ClosuredContextInterface, Behat\Gherkin\Node\PyStringNode, Behat\Gherkin\Node\TableNode, Behat\MinkExtension\Context\MinkContext as MinkContext; - + +use Behat\Mink\Element\NodeElement; use Symfony\Component\DomCrawler\Crawler; // PHPUnit @@ -74,13 +75,9 @@ class CmsFormsContext extends BehatContext { * @When /^I fill in "(?P(?:[^"]|\\")*)" for the "(?P(?:[^"]|\\")*)" HTML field$/ */ public function stepIFillInTheHtmlFieldWith($field, $value) { - $field = $this->fixStepArgument($field); + $inputField = $this->getHtmlField($field); $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( "jQuery('#%s').entwine('ss').getEditor().setContent('%s')", $inputField->getAttribute('id'), @@ -92,13 +89,9 @@ class CmsFormsContext extends BehatContext { * @When /^I append "(?P(?:[^"]|\\")*)" to the "(?P(?:[^"]|\\")*)" HTML field$/ */ public function stepIAppendTotheHtmlField($field, $value) { - $field = $this->fixStepArgument($field); + $inputField = $this->getHtmlField($field); $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( "jQuery('#%s').entwine('ss').getEditor().insertContent('%s')", $inputField->getAttribute('id'), @@ -110,11 +103,7 @@ class CmsFormsContext extends BehatContext { * @Then /^the "(?P(?:[^"]|\\")*)" HTML field should(?P not? |\s*)contain "(?P.*)"$/ */ public function theHtmlFieldShouldContain($locator, $negative, $html) { - $locator = $this->fixStepArgument($locator); - $page = $this->getSession()->getPage(); - $element = $page->findField($locator); - assertNotNull($element, sprintf('HTML field "%s" not found', $locator)); - + $element = $this->getHtmlField($locator); $actual = $element->getValue(); $regex = '/'.preg_quote($html, '/').'/ui'; $failed = false; @@ -154,10 +143,7 @@ class CmsFormsContext extends BehatContext { * @Given /^"(?P([^"]*))" in the "(?P(?:[^"]|\\")*)" HTML field should(?P(?: not)?) be (?P(.*))$/ */ public function stepContentInHtmlFieldShouldHaveFormatting($text, $field, $negate, $formatting) { - $field = $this->fixStepArgument($field); - $page = $this->getSession()->getPage(); - $inputField = $page->findField($field); - assertNotNull($inputField, sprintf('HTML field "%s" not found', $field)); + $inputField = $this->getHtmlField($field); $crawler = new Crawler($inputField->getValue()); $matchedNode = null; @@ -192,10 +178,7 @@ class CmsFormsContext extends BehatContext { * @When /^I select "(?P([^"]*))" in the "(?P(?:[^"]|\\")*)" HTML field$/ */ public function stepIHighlightTextInHtmlField($text, $field) { - $field = $this->fixStepArgument($field); - $page = $this->getSession()->getPage(); - $inputField = $page->findField($field); - assertNotNull($inputField, sprintf('HTML field "%s" not found', $field)); + $inputField = $this->getHtmlField($field); $inputFieldId = $inputField->getAttribute('id'); $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; + } + }