MDL-39348 behat: Compatibility with Safari and IE

Also changing from SeleniumDriver checking
to GoutteDriver as is less probably to change
goutte than to change selenium.
This commit is contained in:
David Monllao
2013-05-03 10:43:06 +08:00
parent c59e52f561
commit 28abad1ab2
5 changed files with 62 additions and 9 deletions
+1 -1
View File
@@ -411,7 +411,7 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext {
* @return boolean
*/
protected function running_javascript() {
return get_class($this->getSession()->getDriver()) === 'Moodle\BehatExtension\Driver\MoodleSelenium2Driver';
return get_class($this->getSession()->getDriver()) !== 'Behat\Mink\Driver\GoutteDriver';
}
}
+15 -4
View File
@@ -112,7 +112,7 @@ class behat_form_field {
global $CFG;
// Textareas are considered text based elements.
$tagname = $this->field->getTagName();
$tagname = strtolower($this->field->getTagName());
if ($tagname == 'textarea') {
return false;
}
@@ -131,11 +131,13 @@ class behat_form_field {
default:
return false;
}
}
// Select tag.
if ($tagname == 'select') {
} else if ($tagname == 'select') {
// Select tag.
$classname = 'behat_form_select';
} else {
return false;
}
$classpath = $CFG->dirroot . '/lib/behat/form_field/' . $classname . '.php';
@@ -143,4 +145,13 @@ class behat_form_field {
return new $classname($this->session, $this->field);
}
/**
* Returns whether the scenario is running in a browser that can run Javascript or not.
*
* @return bool
*/
protected function running_javascript() {
return get_class($this->session->getDriver()) !== 'Behat\Mink\Driver\GoutteDriver';
}
}
@@ -45,6 +45,26 @@ class behat_form_select extends behat_form_field {
*/
public function set_value($value) {
$this->field->selectOption($value);
// Adding a click as Selenium requires it to fire some JS events.
if ($this->running_javascript()) {
// Single select needs an extra click in the option.
if (!$this->field->hasAttribute('multiple')) {
// Using the driver direcly because Element methods are messy when dealing
// with elements inside containers.
$optionxpath = $this->field->getXpath() .
"/descendant::option[(./@value = '" . $value . "' or contains(normalize-space(string(.)), '" . $value . "'))]";
$optionnodes = $this->session->getDriver()->find($optionxpath);
if ($optionnodes) {
current($optionnodes)->click();
}
} else {
// Multiple ones needs the click in the select.
$this->field->click();
}
}
}
/**
+10 -1
View File
@@ -178,7 +178,16 @@ class behat_forms extends behat_base {
// Adding a click as Selenium requires it to fire some JS events.
if ($this->running_javascript()) {
$selectnode->click();
if (!$selectnode->hasAttribute('multiple')) {
// Single select needs an extra click in the option.
$xpath = ".//option[(./@value = '" . $option . "' or contains(normalize-space(string(.)), '" . $option . "'))]";
$optionnode = $this->find('xpath', $xpath, false, $selectnode);
$optionnode->click();
} else {
// Multiple ones needs the click in the select.
$selectnode->click();
}
}
}
+16 -3
View File
@@ -170,9 +170,22 @@ class behat_hooks extends behat_base {
return;
}
// Wait until the page is ready.
try {
$this->getSession()->wait(self::TIMEOUT * 1000, '(document.readyState === "complete")');
// Wait until the page is ready.
// We are already checking that we use a JS browser, this could
// change in case we use another JS driver.
try {
// Safari and Internet Explorer requires time between steps,
// otherwise Selenium tries to click in the previous page's DOM.
if ($this->getSession()->getDriver()->getBrowserName() == 'safari' ||
$this->getSession()->getDriver()->getBrowserName() == 'internet explorer') {
$this->getSession()->wait(self::TIMEOUT * 1000, false);
} else {
// With other browsers we just wait for the DOM ready.
$this->getSession()->wait(self::TIMEOUT * 1000, '(document.readyState === "complete")');
}
} catch (NoSuchWindow $e) {
// If we were interacting with a popup window it will not exists after closing it.
}