Commit 6df28ecf authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review
Browse files

Merge "Implement the step "I set the field ABC to XYZ" for HTML editor"

parents 1d5e99f2 78a363d8
......@@ -50,6 +50,11 @@ class BehatBase extends Behat\MinkExtension\Context\RawMinkContext {
*/
const EXTENDED_TIMEOUT = 10;
/**
* Number of retries to wait for the editor to be ready.
*/
const WAIT_FOR_EDITOR_RETRIES = 10;
/**
* The JS code to check that the page is ready.
*/
......
......@@ -27,21 +27,41 @@ class BehatFormEditor extends BehatFormTextarea {
*/
public function set_value($value) {
$editorid = $this->field->getAttribute('id');
if ($this->running_javascript()) {
$value = addslashes($value);
$js = '
var editor = Y.one(document.getElementById("'.$editorid.'editable"));
if (editor) {
editor.setHTML("' . $value . '");
}
editor = Y.one(document.getElementById("'.$editorid.'"));
editor.set("value", "' . $value . '");
';
$this->session->executeScript($js);
$lastexception = null;
// We want the editor to be ready, otherwise the value can not
// be set and an exception is thrown.
for ($i = 0; $i < BehatBase::WAIT_FOR_EDITOR_RETRIES; $i++) {
try {
// Get tinyMCE editor id if it exists.
if ($editorid = $this->get_editor_id()) {
// Set the value to the iframe and save it to the textarea.
$this->session->executeScript('
tinyMCE.get("' . $editorid . '").setContent("' . $value . '");
tinyMCE.get("' . $editorid . '").save();
');
}
else {
// Set the value to a textarea otherwise.
parent::set_value($value);
}
return;
}
catch (Exception $e) {
// Catching any kind of exception and ignoring it until times out.
$lastexception = $e;
// Waiting 0.1 seconds.
usleep(100000);
}
}
else {
parent::set_value($value);
// If it is not available we throw the last exception.
if (is_a($lastexception, 'Exception')) {
throw $lastexception;
}
}
......@@ -55,5 +75,87 @@ editor.set("value", "' . $value . '");
// A text editor may silently wrap the content in p tags (or not). Neither is an error.
return $this->text_matches($expectedvalue) || $this->text_matches('<p>' . $expectedvalue . '</p>');
}
/**
* Returns the field value.
*
* @return string
*/
public function get_value() {
// Can be be a string value or an exception depending whether the editor loads or not.
$lastoutcome = '';
// We want the editor to be ready to return the correct value, sometimes the
// page loads too fast and the returned value may be '' if the editor didn't
// have enough time to load completely despite having a different value.
for ($i = 0; $i < BehatBase::WAIT_FOR_EDITOR_RETRIES; $i++) {
try {
// Get tinyMCE editor id if it exists.
if ($editorid = $this->get_editor_id()) {
// Save the current iframe value in case default value has been edited.
$this->session->executeScript('tinyMCE.get("' . $editorid . '").save();');
}
$lastoutcome = $this->field->getValue();
// We only want to wait until it times out if the value is empty.
if ($lastoutcome != '') {
return $lastoutcome;
}
}
catch (Exception $e) {
// Catching any kind of exception and ignoring it until times out.
$lastoutcome = $e;
// Waiting 0.1 seconds.
usleep(100000);
}
}
// If it is not available we throw the last exception.
if (is_a($lastoutcome, 'Exception')) {
throw $lastoutcome;
}
// Return the value if there are no exceptions it will be '' at this point
return $lastoutcome;
}
/**
* Returns the tinyMCE editor id or false if it is not available.
*
* The editor availability depends on the driver running the tests; Goutte
* can not execute Javascript, also some Mahara settings disables the HTML
* editor.
*
* @return mixed The id of the editor of false if is not available
*/
protected function get_editor_id() {
// Non-JS drivers throws exceptions when running JS.
try {
$available = $this->session->evaluateScript('return (typeof tinyMCE != "undefined")');
// Also checking that it exist a tinyMCE editor for the requested field.
$editorid = $this->field->getAttribute('id');
$available = $this->session->evaluateScript('return (typeof tinyMCE.get("' . $editorid . '") != "undefined")');
}
catch (Exception $e) {
return false;
}
// No available if JS drivers returned false.
if ($available == false) {
return false;
}
return $editorid;
}
}
......@@ -23,27 +23,31 @@ Feature: Mahara users can have their profile
When I click on "Content"
Then I should see "Profile"
And the "firstname" field should contain "Emily"
When I fill in "firstname" with "Tiger"
When I fill in "lastname" with "Wood"
When I fill in "studentid" with "1234"
When I fill in "preferredname" with "Golf Legend"
When I click on "Contact information"
Then I should see "Email address"
And I set the following fields to these values:
| First name | Tiger |
| Last name | Wood |
| Student ID | 1234 |
| Display name | Golf Legend |
| Introduction | <p>This is my introduction.</p><p>My name is Wood</p> |
And I click on "Contact information"
And I should see "Email address"
And I should see "Official website address"
When I fill in "officialwebsite" with "www.catalyst.net.nz"
And I fill in "personalwebsite" with "www.stuff.co.nz"
When I fill in "blogaddress" with "www.blog.com"
When I fill in "profileform_address" with "150 Willis Street"
When I fill in "city" with "CBD"
When I fill in "homenumber" with "04928375"
When I fill in "businessnumber" with "040298375"
When I fill in "mobilenumber" with "0272093875482"
When I fill in "faxnumber" with "09237842"
When I click on "Social media"
Then I should see "New social media account"
When I click on "General"
Then I should see "Occupation"
And I set the following fields to these values:
| Official website address | www.catalyst.net.nz |
| Personal website address | www.stuff.co.nz |
| Blog address | www.blog.com |
| Postal address | 150 Willis Street, Te Aro |
| City/region | Wellington |
| Country | New Zealand |
| Home phone | +64-4-928375 |
| Business phone | +64-4-0298375 |
| Mobile phone | 0272093875482 |
| Fax number | 09237842 |
And I click on "Social media"
And I should see "New social media account"
And I click on "General"
And I should see "Occupation"
When I fill in "occupation" with "Software Engineer"
When I fill in "industry" with "it"
When I fill in "industry" with "IT"
When I press "Save profile"
Then I should see "Profile saved successfully"
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