2 // This file is part of Stack - http://stack.bham.ac.uk/
4 // Stack is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Stack is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Stack. If not, see <http://www.gnu.org/licenses/>.
18 * Behat steps definitions for drag and drop markers.
20 * @package qtype_ddmarker
22 * @copyright 2015 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
31 * Steps definitions related with the drag and drop markers question type.
33 * @copyright 2015 The Open University
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class behat_qtype_ddmarker extends behat_base {
39 * Get the xpath for a given drag item.
40 * @param string $dragitem the text of the item to drag.
41 * @return string the xpath expression.
43 protected function marker_xpath($marker, $item = 0) {
44 return '//span[contains(@class, " dragitem ") and contains(@class, " item' . $item .
45 '") and span[@class = "markertext" and contains(normalize-space(.), "' .
46 $this->escape($marker) . '")]]';
49 protected function parse_marker_name($marker) {
51 if (preg_match('~,(\d+)$~', $marker, $matches)) {
53 $marker = substr($marker, 0, -1 - strlen($item));
55 return array($marker, $item);
59 * Drag the drag item with the given text to the given space.
61 * @param string $marker the marker to drag. The label, optionally followed by ,<instance number> (int) if relevant.
62 * @param string $coordinates the position to drag the marker to, 'x,y'.
64 * @Given /^I drag "(?P<marker>[^"]*)" to "(?P<coordinates>\d+,\d+)" in the drag and drop markers question$/
66 public function i_drag_to_in_the_drag_and_drop_markers_question($marker, $coordinates) {
67 list($marker, $item) = $this->parse_marker_name($marker);
68 list($x, $y) = explode(',', $coordinates);
70 // This is a bit nasty, but Behat (indeed Selenium) will only drag on
71 // DOM node so that its centre is over the centre of anothe DOM node.
72 // Therefore to make it drag to the specified place, we have to add
74 $session = $this->getSession();
75 $session->evaluateScript("
77 if (document.getElementById('target-{$x}-{$y}')) {
80 var image = document.querySelector('.dropbackground');
81 var target = document.createElement('div');
82 target.setAttribute('id', 'target-{$x}-{$y}');
83 var container = document.querySelector('.droparea');
84 container.style.setProperty('position', 'relative');
85 container.insertBefore(target, image);
86 var xadjusted = {$x} + (container.offsetWidth - image.offsetWidth) / 2
87 target.style.setProperty('position', 'absolute');
88 target.style.setProperty('left', xadjusted + 'px');
89 target.style.setProperty('top', '{$y}px');
90 target.style.setProperty('width', '1px');
91 target.style.setProperty('height', '1px');
94 $generalcontext = behat_context_helper::get('behat_general');
95 $generalcontext->i_drag_and_i_drop_it_in($this->marker_xpath($marker, $item),
96 'xpath_element', "#target-{$x}-{$y}", 'css_element');
100 * Type some characters while focussed on a given drop box.
102 * @param string $direction the direction key to press.
104 * @param string $marker the marker to drag. The label, optionally followed by ,<instance number> (int) if relevant.
106 * @Given /^I type "(?P<direction>up|down|left|right)" "(?P<repeats>\d+)" times on marker "(?P<marker>[^"]*)" in the drag and drop markers question$/
108 public function i_type_on_marker_in_the_drag_and_drop_markers_question($direction, $repeats, $marker) {
115 list($marker, $item) = $this->parse_marker_name($marker);
116 $node = $this->get_selected_node('xpath_element', $this->marker_xpath($marker, $item));
117 $this->ensure_node_is_visible($node);
118 for ($i = 0; $i < $repeats; $i++) {
119 $node->keyDown($keycodes[$direction]);
120 $node->keyPress($keycodes[$direction]);
121 $node->keyUp($keycodes[$direction]);