MDL-38547 behat: coding style fixes.
[moodle.git] / lib / tests / behat / behat_general.php
CommitLineData
786ea937
DM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle 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.
8//
9// Moodle 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.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * General use steps definitions.
19 *
20 * @package core
21 * @category test
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28require_once(__DIR__ . '/../../behat/behat_base.php');
29
ca4f33a7
DM
30use Behat\Mink\Exception\ExpectationException as ExpectationException,
31 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
786ea937
DM
32
33/**
34 * Cross component steps definitions.
35 *
36 * Basic web application definitions from MinkExtension and
37 * BehatchExtension. Definitions modified according to our needs
38 * when necessary and including only the ones we need to avoid
39 * overlapping and confusion.
40 *
41 * @package core
42 * @category test
43 * @copyright 2012 David MonllaĆ³
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 */
46class behat_general extends behat_base {
47
48 /**
49 * Opens Moodle homepage.
50 *
786ea937
DM
51 * @Given /^I am on homepage$/
52 */
53 public function i_am_on_homepage() {
40923977 54 $this->getSession()->visit($this->locate_path('/'));
786ea937
DM
55 }
56
18c84063
DM
57 /**
58 * Reloads the current page.
59 *
60 * @Given /^I reload the page$/
61 */
62 public function reload() {
63 $this->getSession()->reload();
64 }
65
786ea937
DM
66 /**
67 * Clicks link with specified id|title|alt|text.
68 *
786ea937 69 * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
1f9ffbdb 70 * @throws ElementNotFoundException Thrown by behat_base::find
40923977 71 * @param string $link
786ea937
DM
72 */
73 public function click_link($link) {
1f9ffbdb
DM
74
75 $linknode = $this->find_link($link);
76 $linknode->click();
786ea937
DM
77 }
78
79 /**
80 * Waits X seconds. Required after an action that requires data from an AJAX request.
81 *
82 * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
83 * @param int $seconds
84 */
85 public function i_wait_seconds($seconds) {
86 $this->getSession()->wait($seconds * 1000, false);
87 }
88
89 /**
90 * Waits until the page is completely loaded. This step is auto-executed after every step.
91 *
92 * @Given /^I wait until the page is ready$/
93 */
94 public function wait_until_the_page_is_ready() {
95 $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")');
96 }
97
98 /**
40923977 99 * Generic mouse over action. Mouse over a element of the specified type.
786ea937 100 *
40923977
DM
101 * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
102 * @param string $element Element we look for
103 * @param string $selectortype The type of what we look for
786ea937 104 */
40923977 105 public function i_hover($element, $selectortype) {
1f9ffbdb 106
40923977
DM
107 // Gets the node based on the requested selector type and locator.
108 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
109 $node->mouseOver();
110 }
111
40923977
DM
112 /**
113 * Generic click action. Click on the element of the specified type.
114 *
115 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
116 * @param string $element Element we look for
117 * @param string $selectortype The type of what we look for
118 */
119 public function i_click_on($element, $selectortype) {
120
121 // Gets the node based on the requested selector type and locator.
122 $node = $this->get_selected_node($selectortype, $element);
123 $node->click();
124 }
125
072f67fc
DM
126 /**
127 * Click on the element of the specified type which is located inside the second element.
128 *
129 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
130 * @param string $element Element we look for
131 * @param string $selectortype The type of what we look for
132 * @param string $nodeelement Element we look in
133 * @param string $nodeselectortype The type of selector where we look in
134 */
135 public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
136
137 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
138 $node->click();
139 }
140
786ea937
DM
141 /**
142 * Checks, that page contains specified text.
143 *
144 * @see Behat\MinkExtension\Context\MinkContext
145 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
40923977 146 * @param string $text
786ea937
DM
147 */
148 public function assert_page_contains_text($text) {
02ca6219 149 $this->assertSession()->pageTextContains($text);
786ea937
DM
150 }
151
152 /**
153 * Checks, that page doesn't contain specified text.
154 *
155 * @see Behat\MinkExtension\Context\MinkContext
156 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
40923977 157 * @param string $text
786ea937
DM
158 */
159 public function assert_page_not_contains_text($text) {
02ca6219 160 $this->assertSession()->pageTextNotContains($text);
786ea937
DM
161 }
162
163 /**
40923977 164 * Checks, that element with specified CSS selector or XPath contains specified text.
786ea937 165 *
40923977
DM
166 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
167 * @param string $text
168 * @param string $element Element we look in.
169 * @param string $selectortype The type of element where we are looking in.
786ea937 170 */
40923977
DM
171 public function assert_element_contains_text($text, $element, $selectortype) {
172
173 // Transforming from steps definitions selector/locator format to Mink format.
072f67fc 174 list($selector, $locator) = $this->transform_text_selector($selectortype, $element);
40923977 175 $this->assertSession()->elementTextContains($selector, $locator, $text);
786ea937
DM
176 }
177
178 /**
40923977 179 * Checks, that element with specified CSS selector or XPath doesn't contain specified text.
786ea937 180 *
40923977
DM
181 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
182 * @param string $text
183 * @param string $element Element we look in.
184 * @param string $selectortype The type of element where we are looking in.
786ea937 185 */
40923977
DM
186 public function assert_element_not_contains_text($text, $element, $selectortype) {
187
188 // Transforming from steps definitions selector/locator format to mink format.
072f67fc 189 list($selector, $locator) = $this->transform_text_selector($selectortype, $element);
40923977 190 $this->assertSession()->elementTextNotContains($selector, $locator, $text);
786ea937
DM
191 }
192
193 /**
40923977 194 * Checks, that element of specified type is disabled.
786ea937 195 *
40923977 196 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
1f9ffbdb 197 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
198 * @param string $element Element we look in
199 * @param string $selectortype The type of element where we are looking in.
786ea937 200 */
40923977 201 public function the_element_should_be_disabled($element, $selectortype) {
786ea937 202
40923977
DM
203 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
204 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
205
206 if (!$node->hasAttribute('disabled')) {
207 throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
208 }
209 }
210
211 /**
40923977 212 * Checks, that element of specified type is enabled.
786ea937 213 *
40923977 214 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
1f9ffbdb 215 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
216 * @param string $element Element we look on
217 * @param string $selectortype The type of where we look
786ea937 218 */
40923977 219 public function the_element_should_be_enabled($element, $selectortype) {
1f9ffbdb 220
40923977
DM
221 // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement.
222 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
223
224 if ($node->hasAttribute('disabled')) {
225 throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
226 }
227 }
228
ca4f33a7 229 /**
62eb5c46
EL
230 * Checks the provided element and selector type exists in the current page.
231 *
232 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7
DM
233 *
234 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exists$/
235 * @throws ElementNotFoundException Thrown by behat_base::find
236 * @param string $element The locator of the specified selector
237 * @param string $selectortype The selector type
238 */
239 public function should_exists($element, $selectortype) {
240
241 // Getting Mink selector and locator.
242 list($selector, $locator) = $this->transform_selector($selectortype, $element);
243
244 // Will throw an ElementNotFoundException if it does not exist.
245 $this->find($selector, $locator);
246 }
247
248 /**
62eb5c46
EL
249 * Checks that the provided element and selector type not exists in the current page.
250 *
251 * This step is for advanced users, use it if you don't find anything else suitable for what you need.
ca4f33a7
DM
252 *
253 * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exists$/
254 * @throws ExpectationException
255 * @param string $element The locator of the specified selector
256 * @param string $selectortype The selector type
257 */
258 public function should_not_exists($element, $selectortype) {
259
260 try {
261 $this->should_exists($element, $selectortype);
262 throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession());
62eb5c46 263 } catch (ElementNotFoundException $e) {
ca4f33a7
DM
264 // It passes.
265 return;
266 }
267 }
268
786ea937 269}