Skip to content

Commit

Permalink
MDL-67657 behat: Convert existing uses of executeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jul 16, 2020
1 parent 6d5d193 commit cd6eadd
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 53 deletions.
26 changes: 15 additions & 11 deletions lib/behat/behat_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ protected function is_in_app() : bool {
}

// Check on page to see if it's an app page. Safest way is to look for added JavaScript.
return $this->getSession()->evaluateScript('typeof window.behat') === 'object';
return $this->evaluate_script('return typeof window.behat') === 'object';
}

/**
Expand Down Expand Up @@ -771,14 +771,18 @@ protected function resize_window($windowsize, $viewport = false) {
// The window inner height will be as specified, which means the available viewport will
// actually be smaller if there is a horizontal scrollbar. We assume that horizontal
// scrollbars are rare so this doesn't matter.
$offset = $this->getSession()->getDriver()->evaluateScript(
'return (function() { var before = document.body.style.overflowY;' .
'document.body.style.overflowY = "scroll";' .
'var result = {};' .
'result.x = window.outerWidth - document.body.offsetWidth;' .
'result.y = window.outerHeight - window.innerHeight;' .
'document.body.style.overflowY = before;' .
'return result; })();');
$js = <<<EOF
return (function() {
var before = document.body.style.overflowY;
document.body.style.overflowY = "scroll";
var result = {};
result.x = window.outerWidth - document.body.offsetWidth;
result.y = window.outerHeight - window.innerHeight;
document.body.style.overflowY = before;
return result;
})();
EOF;
$offset = $this->evaluate_script($js);
$width += $offset['x'];
$height += $offset['y'];
}
Expand Down Expand Up @@ -827,8 +831,8 @@ public static function wait_for_pending_js_in_session(Session $session) {
} else {
return "incomplete"
}
}());'));
$pending = $session->evaluateScript($jscode);
})()'));
$pending = self::evaluate_script_in_session($session, $jscode);
} catch (NoSuchWindow $nsw) {
// We catch an exception here, in case we just closed the window we were interacting with.
// No javascript is running if there is no window right?
Expand Down
18 changes: 10 additions & 8 deletions lib/behat/form_field/behat_form_editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ public function set_value($value) {
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 . '");
(function() {
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);
behat_base::execute_script_in_session($this->session, $js);
} else {
parent::set_value($value);
}
Expand Down Expand Up @@ -88,7 +90,7 @@ public function select_text() {
r.selectNodeContents(e);
s.setSingleRange(r);
}()); ';
$this->session->executeScript($js);
behat_base::execute_script_in_session($this->session, $js);
}

/**
Expand Down
12 changes: 7 additions & 5 deletions lib/behat/form_field/behat_form_passwordunmask.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ public function set_value($value) {
if ($this->running_javascript()) {
$id = $this->field->getAttribute('id');
$js = <<<JS
require(["jquery"], function($) {
var wrapper = $(document.getElementById("{$id}")).closest('[data-passwordunmask="wrapper"]');
wrapper.find('[data-passwordunmask="edit"]').trigger("click");
});
(function() {
require(["jquery"], function($) {
var wrapper = $(document.getElementById("{$id}")).closest('[data-passwordunmask="wrapper"]');
wrapper.find('[data-passwordunmask="edit"]').trigger("click");
});
})();
JS;
$this->session->executeScript($js);
behat_base::execute_script_in_session($this->session, $js);
}

$this->field->setValue($value);
Expand Down
18 changes: 8 additions & 10 deletions lib/tests/behat/behat_app.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ function($context, $args) {
}, false, 60);

// Run the scripts to install Moodle 'pending' checks.
$this->getSession()->executeScript(
file_get_contents(__DIR__ . '/app_behat_runtime.js'));
$this->execute_script(file_get_contents(__DIR__ . '/app_behat_runtime.js'));

// Wait until the site login field appears OR the main page.
$situation = $this->spin(
Expand Down Expand Up @@ -373,8 +372,7 @@ function($context, $args) {
*/
public function i_press_the_standard_button_in_the_app(string $button) {
$this->spin(function($context, $args) use ($button) {
$result = $this->getSession()->evaluateScript('return window.behat.pressStandard("' .
$button . '");');
$result = $this->evaluate_script("return window.behat.pressStandard('{$button}');");
if ($result !== 'OK') {
throw new DriverException('Error pressing standard button - ' . $result);
}
Expand All @@ -391,7 +389,7 @@ public function i_press_the_standard_button_in_the_app(string $button) {
*/
public function i_close_the_popup_in_the_app() {
$this->spin(function($context, $args) {
$result = $this->getSession()->evaluateScript('return window.behat.closePopup();');
$result = $this->evaluate_script("return window.behat.closePopup();");
if ($result !== 'OK') {
throw new DriverException('Error closing popup - ' . $result);
}
Expand Down Expand Up @@ -449,7 +447,7 @@ protected function press(string $text, string $near = '') {
} else {
$nearbit = '';
}
$result = $context->getSession()->evaluateScript('return window.behat.press("' .
$result = $this->evaluate_script('return window.behat.press("' .
addslashes_js($text) . '"' . $nearbit .');');
if ($result !== 'OK') {
throw new DriverException('Error pressing item - ' . $result);
Expand All @@ -472,7 +470,7 @@ protected function press(string $text, string $near = '') {
*/
public function i_set_the_field_in_the_app(string $field, string $value) {
$this->spin(function($context, $args) use ($field, $value) {
$result = $this->getSession()->evaluateScript('return window.behat.setField("' .
$result = $this->evaluate_script('return window.behat.setField("' .
addslashes_js($field) . '", "' . addslashes_js($value) . '");');
if ($result !== 'OK') {
throw new DriverException('Error setting field - ' . $result);
Expand All @@ -494,7 +492,7 @@ public function i_set_the_field_in_the_app(string $field, string $value) {
*/
public function the_header_should_be_in_the_app(string $text) {
$result = $this->spin(function($context, $args) {
$result = $this->getSession()->evaluateScript('return window.behat.getHeader();');
$result = $this->evaluate_script('return window.behat.getHeader();');
if (substr($result, 0, 3) !== 'OK:') {
throw new DriverException('Error getting header - ' . $result);
}
Expand Down Expand Up @@ -536,7 +534,7 @@ public function i_close_the_browser_tab_opened_by_the_app() {
if (count($names) !== 2) {
throw new DriverException('Expected to see 2 tabs open, not ' . count($names));
}
$this->getSession()->getDriver()->executeScript('window.close()');
$this->execute_script('window.close()');
$this->getSession()->switchToWindow($names[0]);
}

Expand All @@ -548,6 +546,6 @@ public function i_close_the_browser_tab_opened_by_the_app() {
* @throws DriverException If the navigator.online mode is not available
*/
public function i_switch_offline_mode(string $offline) {
$this->getSession()->evaluateScript('appProvider.setForceOffline(' . $offline . ');');
$this->execute_script('appProvider.setForceOffline(' . $offline . ');');
}
}
22 changes: 11 additions & 11 deletions lib/tests/behat/behat_general.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ public function switch_to_window($windowname) {
// unnamed window (presumably the main window) to some other named
// window, then we first set the main window name to a conventional
// value that we can later use this name to switch back.
$this->getSession()->executeScript(
'if (window.name == "") window.name = "' . self::MAIN_WINDOW_NAME . '"');
$this->execute_script('if (window.name == "") window.name = "' . self::MAIN_WINDOW_NAME . '"');

$this->getSession()->switchToWindow($windowname);
}
Expand Down Expand Up @@ -258,7 +257,7 @@ public function i_close_all_opened_windows() {
$names = $this->getSession()->getWindowNames();
for ($index = 1; $index < count($names); $index ++) {
$this->getSession()->switchToWindow($names[$index]);
$this->getSession()->executeScript("window.open('', '_self').close();");
$this->execute_script("window.open('', '_self').close();");
}
$names = $this->getSession()->getWindowNames();
if (count($names) !== 1) {
Expand Down Expand Up @@ -924,7 +923,7 @@ protected function check_element_order(
return a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING;
})()
EOF;
$ok = $this->getSession()->getDriver()->evaluateScript($js);
$ok = $this->evaluate_script($js);
} else {

// Using following xpath axe to find it.
Expand Down Expand Up @@ -1609,11 +1608,12 @@ public function i_start_watching_to_see_if_a_new_page_loads() {

$this->pageloaddetectionrunning = true;

$session->executeScript(
'var span = document.createElement("span");
span.setAttribute("data-rel", "' . self::PAGE_LOAD_DETECTION_STRING . '");
span.setAttribute("style", "display: none;");
document.body.appendChild(span);');
$this->execute_script(
'var span = document.createElement("span");
span.setAttribute("data-rel", "' . self::PAGE_LOAD_DETECTION_STRING . '");
span.setAttribute("style", "display: none;");
document.body.appendChild(span);'
);
}

/**
Expand Down Expand Up @@ -1811,7 +1811,7 @@ public function the_focused_element_is($not, $nodeelement, $nodeselectortype) {
$xpath = addslashes_js($element->getXpath());
$script = 'return (function() { return document.activeElement === document.evaluate("' . $xpath . '",
document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; })(); ';
$targetisfocused = $this->getSession()->evaluateScript($script);
$targetisfocused = $this->evaluate_script($script);
if ($not == ' not') {
if ($targetisfocused) {
throw new ExpectationException("$nodeelement $nodeselectortype is focused", $this->getSession());
Expand Down Expand Up @@ -1843,7 +1843,7 @@ public function the_focused_element_is_in_the($not, $element, $selectortype, $no
$xpath = addslashes_js($element->getXpath());
$script = 'return (function() { return document.activeElement === document.evaluate("' . $xpath . '",
document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; })(); ';
$targetisfocused = $this->getSession()->evaluateScript($script);
$targetisfocused = $this->evaluate_script($script);
if ($not == ' not') {
if ($targetisfocused) {
throw new ExpectationException("$nodeelement $nodeselectortype is focused", $this->getSession());
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/behat/behat_navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public function get_expand_navbar_step() {
)";

// Adding an extra click we need to show the 'Log in' link.
if (!$this->getSession()->getDriver()->evaluateScript($navbuttonjs)) {
if (!$this->evaluate_script($navbuttonjs)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ public function i_draw_on_the_pdf() {
var event = { clientX: 100, clientY: 250, preventDefault: function() {} };
instance.edit_start(event);
}()); ';
$this->getSession()->executeScript($js);
$this->execute_script($js);
sleep(1);
$js = ' (function() {
var instance = M.assignfeedback_editpdf.instance;
var event = { clientX: 150, clientY: 275, preventDefault: function() {} };
instance.edit_move(event);
}()); ';
$this->getSession()->executeScript($js);
$this->execute_script($js);
sleep(1);
$js = ' (function() {
var instance = M.assignfeedback_editpdf.instance;
var event = { clientX: 200, clientY: 300, preventDefault: function() {} };
instance.edit_end(event);
}()); ';
$this->getSession()->executeScript($js);
$this->execute_script($js);
sleep(1);
}

Expand Down
6 changes: 3 additions & 3 deletions question/type/ddmarker/tests/behat/behat_qtype_ddmarker.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public function i_drag_to_in_the_drag_and_drop_markers_question($marker, $coordi
// DOM node so that its centre is over the centre of anothe DOM node.
// Therefore to make it drag to the specified place, we have to add
// a target div.
$session = $this->getSession();
$session->executeScript("
$this->execute_script("
(function() {
if (document.getElementById('target-{$x}-{$y}')) {
return;
Expand All @@ -86,7 +85,8 @@ public function i_drag_to_in_the_drag_and_drop_markers_question($marker, $coordi
target.style.setProperty('top', yadjusted + 'px');
target.style.setProperty('width', '1px');
target.style.setProperty('height', '1px');
}())");
}())"
);

$generalcontext = behat_context_helper::get('behat_general');
$generalcontext->i_drag_and_i_drop_it_in($this->marker_xpath($marker),
Expand Down
2 changes: 1 addition & 1 deletion search/tests/behat/behat_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function i_search_for_using_the_header_global_search_box($query) {
$this->execute('behat_forms::i_set_the_field_to', ['q', $query]);

// Submit the form.
$this->getSession()->executeScript('document.querySelector(".search-input-form.expanded").submit();');
$this->execute_script('return document.querySelector(".search-input-form.expanded").submit();');
}

/**
Expand Down

0 comments on commit cd6eadd

Please sign in to comment.