Commit 4f33131f authored by Son Nguyen's avatar Son Nguyen Committed by Robert Lyon

Bug 1550519. Add some step definitions for behat tests

- Then I should see images in the block :title
- Then I should not see images in the block :title

Fix the step "I set the field ..." to assign value with double quote

behatnotneeded - the tests are in the child patch

Change-Id: I70a8fcbf46cfbff8db6d2e257b82ef0a10bfeef4
parent 5361e0ee
......@@ -408,6 +408,7 @@ class BehatForms extends BehatBase {
protected function set_field_value($fieldlocator, $value) {
$fieldlocator = $this->unescapeDoubleQuotes($fieldlocator);
$value = $this->escapeDoubleQuotes($value);
$field = BehatFieldManager::get_form_field_from_label($fieldlocator, $this);
$field->set_value($value);
}
......@@ -506,4 +507,45 @@ class BehatForms extends BehatBase {
}
}
/**
* Fills in WYSIWYG editor with specified id. (If no ID is specified, uses the first
* TinyMCE editor on the page.
*
* TODO: Is there a better, more human-readable way we could specify the TinyMCE editor
* to use? Like, tie it to the label for the pieform element?
*
* @Given /^(?:|I )fill in "(?P<text>[^"]*)" in WYSIWYG editor(?:| "(?P<iframe>[^"]*)")$/
*/
public function iFillInInWYSIWYGEditor($text, $iframe = null) {
if ($iframe == null) {
// Use the first TinyMCE iframe on the page
// TODO: May have to change this when upgrading TinyMCE. Is there a TinyMCE
// Javascript API we could use instead?
$iframe = $this->find('css', '.mce-edit-area > iframe')->getAttribute('id');
}
// switchToIFrame($iframe) seems not to work using current selenium webdriver
// Use javascript to update the tinyMCE editor
if ($this->find('xpath', "//iframe[@id='" . $iframe . "']")) {
$editorid = substr($iframe, 0, -4); // remove '_ifr'
$this->getSession()->executeScript("tinymce.get('" . $editorid . "').setContent('" . $text . "');");
}
else {
throw new \NotFoundException("Iframe with id '$iframe'");
}
}
/**
* Click a button in the TinyMCE editor toolbar
*
* @Given I click the :action button in the editor
*/
public function i_click_button_editor_toolbar($action) {
$exception = new ElementNotFoundException($this->getSession(), 'button', null, 'the action button "' . $action . '" in the editor toolbar');
$actionbutton = $this->find('css', "div.wysiwyg div[aria-label='" . $action . "'] > button", $exception);
$this->ensure_node_is_visible($actionbutton);
$actionbutton->click();
}
}
......@@ -908,34 +908,6 @@ class BehatGeneral extends BehatBase {
}
}
/**
* Fills in WYSIWYG editor with specified id. (If no ID is specified, uses the first
* TinyMCE editor on the page.
*
* TODO: Is there a better, more human-readable way we could specify the TinyMCE editor
* to use? Like, tie it to the label for the pieform element?
*
* @Given /^(?:|I )fill in "(?P<text>[^"]*)" in WYSIWYG editor(?:| "(?P<iframe>[^"]*)")$/
*/
public function iFillInInWYSIWYGEditor($text, $iframe = null) {
if ($iframe == null) {
// Use the first TinyMCE iframe on the page
// TODO: May have to change this when upgrading TinyMCE. Is there a TinyMCE
// Javascript API we could use instead?
$iframe = $this->find('css', '.mce-edit-area > iframe')->getAttribute('id');
}
// switchToIFrame($iframe) seems not to work using current selenium webdriver
// Use javascript to update the tinyMCE editor
if ($this->find('xpath', "//iframe[@id='" . $iframe . "']")) {
$editorid = substr($iframe, 0, -4); // remove '_ifr'
$this->getSession()->executeScript("tinymce.get('" . $editorid . "').setContent('" . $text . "');");
}
else {
throw new \NotFoundException("Iframe with id '$iframe'");
}
}
/**
* Visit a Mahara portfolio Page with the specified title
*
......@@ -1137,4 +1109,89 @@ JS;
}
}
/**
* Check if images exist in the block given its title
*
* @Then I should see images in the block :blocktitle
*
*/
public function i_should_see_images_block($blocktitle) {
// Find the block.
$blocktitleliteral = $this->escaper->escapeLiteral($blocktitle);
$xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' column-content ')]" .
"/div[contains(@id,'blockinstance_')" .
" and contains(h3, " . $blocktitleliteral . ")]//img";
// Wait until it finds the text inside the block title.
try {
$blockimages = $this->find_all('xpath', $xpath);
}
catch (ElementNotFoundException $e) {
throw new ExpectationException('The block with title ' . $blocktitleliteral . ' was not found', $this->getSession());
}
// If we are not running javascript we have enough with the
// element existing as we can't check if it is visible.
if (!$this->running_javascript()) {
return;
}
// We also check the element visibility when running JS tests.
$this->spin(
function($context, $args) {
foreach ($args['nodes'] as $node) {
if ($node->isVisible()) {
return true;
}
}
throw new ExpectationException('The block with title ' . $args['text'] . ' was not visible', $context->getSession());
},
array('nodes' => $blockimages, 'text' => $blocktitleliteral)
);
}
/**
* Check if images does not exist in the block given its title
*
* @Then I should not see images in the block :blocktitle
*
*/
public function i_should_not_see_images_block($blocktitle) {
// Find the block.
$blocktitleliteral = $this->escaper->escapeLiteral($blocktitle);
$xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' column-content ')]" .
"/div[contains(@id,'blockinstance_')" .
" and contains(h3, " . $blocktitleliteral . ")]" .
"[count(descendant::img) = 0]";
// Wait until it finds the text inside the block title.
try {
$blockimages = $this->find_all('xpath', $xpath);
}
catch (ElementNotFoundException $e) {
throw new ExpectationException('The block with title ' . $blocktitleliteral . ' was not found', $this->getSession());
}
// If we are not running javascript we have enough with the
// element existing as we can't check if it is visible.
if (!$this->running_javascript()) {
return;
}
// We also check the element visibility when running JS tests.
$this->spin(
function($context, $args) {
foreach ($args['nodes'] as $node) {
if ($node->isVisible()) {
return true;
}
}
throw new ExpectationException('The block with title ' . $args['text'] . ' was not visible', $context->getSession());
},
array('nodes' => $blockimages, 'text' => $blocktitleliteral)
);
}
}
......@@ -14,7 +14,7 @@ use Behat\Mink\Element\NodeElement as NodeElement;
require_once(__DIR__ . '/BehatFormTextarea.php');
/**
* Moodle editor field.
* Mahara editor field using TinyMCE.
*
*/
class BehatFormEditor extends BehatFormTextarea {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment