MDL-38689 behat: Allowing more than one selector type argument
[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
30use Behat\Mink\Exception\ExpectationException as ExpectationException;
31
32/**
33 * Cross component steps definitions.
34 *
35 * Basic web application definitions from MinkExtension and
36 * BehatchExtension. Definitions modified according to our needs
37 * when necessary and including only the ones we need to avoid
38 * overlapping and confusion.
39 *
40 * @package core
41 * @category test
42 * @copyright 2012 David Monllaó
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
45class behat_general extends behat_base {
46
47 /**
48 * Opens Moodle homepage.
49 *
786ea937
DM
50 * @Given /^I am on homepage$/
51 */
52 public function i_am_on_homepage() {
40923977 53 $this->getSession()->visit($this->locate_path('/'));
786ea937
DM
54 }
55
56 /**
57 * Clicks link with specified id|title|alt|text.
58 *
786ea937 59 * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
1f9ffbdb 60 * @throws ElementNotFoundException Thrown by behat_base::find
40923977 61 * @param string $link
786ea937
DM
62 */
63 public function click_link($link) {
1f9ffbdb
DM
64
65 $linknode = $this->find_link($link);
66 $linknode->click();
786ea937
DM
67 }
68
69 /**
70 * Waits X seconds. Required after an action that requires data from an AJAX request.
71 *
72 * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
73 * @param int $seconds
74 */
75 public function i_wait_seconds($seconds) {
76 $this->getSession()->wait($seconds * 1000, false);
77 }
78
79 /**
80 * Waits until the page is completely loaded. This step is auto-executed after every step.
81 *
82 * @Given /^I wait until the page is ready$/
83 */
84 public function wait_until_the_page_is_ready() {
85 $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")');
86 }
87
88 /**
40923977 89 * Generic mouse over action. Mouse over a element of the specified type.
786ea937 90 *
40923977
DM
91 * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
92 * @param string $element Element we look for
93 * @param string $selectortype The type of what we look for
786ea937 94 */
40923977 95 public function i_hover($element, $selectortype) {
1f9ffbdb 96
40923977
DM
97 // Gets the node based on the requested selector type and locator.
98 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
99 $node->mouseOver();
100 }
101
40923977
DM
102 /**
103 * Generic click action. Click on the element of the specified type.
104 *
105 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
106 * @param string $element Element we look for
107 * @param string $selectortype The type of what we look for
108 */
109 public function i_click_on($element, $selectortype) {
110
111 // Gets the node based on the requested selector type and locator.
112 $node = $this->get_selected_node($selectortype, $element);
113 $node->click();
114 }
115
072f67fc
DM
116 /**
117 * Click on the element of the specified type which is located inside the second element.
118 *
119 * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
120 * @param string $element Element we look for
121 * @param string $selectortype The type of what we look for
122 * @param string $nodeelement Element we look in
123 * @param string $nodeselectortype The type of selector where we look in
124 */
125 public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
126
127 $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
128 $node->click();
129 }
130
786ea937
DM
131 /**
132 * Checks, that page contains specified text.
133 *
134 * @see Behat\MinkExtension\Context\MinkContext
135 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
40923977 136 * @param string $text
786ea937
DM
137 */
138 public function assert_page_contains_text($text) {
02ca6219 139 $this->assertSession()->pageTextContains($text);
786ea937
DM
140 }
141
142 /**
143 * Checks, that page doesn't contain specified text.
144 *
145 * @see Behat\MinkExtension\Context\MinkContext
146 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
40923977 147 * @param string $text
786ea937
DM
148 */
149 public function assert_page_not_contains_text($text) {
02ca6219 150 $this->assertSession()->pageTextNotContains($text);
786ea937
DM
151 }
152
153 /**
40923977 154 * Checks, that element with specified CSS selector or XPath contains specified text.
786ea937 155 *
40923977
DM
156 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
157 * @param string $text
158 * @param string $element Element we look in.
159 * @param string $selectortype The type of element where we are looking in.
786ea937 160 */
40923977
DM
161 public function assert_element_contains_text($text, $element, $selectortype) {
162
163 // Transforming from steps definitions selector/locator format to Mink format.
072f67fc 164 list($selector, $locator) = $this->transform_text_selector($selectortype, $element);
40923977 165 $this->assertSession()->elementTextContains($selector, $locator, $text);
786ea937
DM
166 }
167
168 /**
40923977 169 * Checks, that element with specified CSS selector or XPath doesn't contain specified text.
786ea937 170 *
40923977
DM
171 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
172 * @param string $text
173 * @param string $element Element we look in.
174 * @param string $selectortype The type of element where we are looking in.
786ea937 175 */
40923977
DM
176 public function assert_element_not_contains_text($text, $element, $selectortype) {
177
178 // Transforming from steps definitions selector/locator format to mink format.
072f67fc 179 list($selector, $locator) = $this->transform_text_selector($selectortype, $element);
40923977 180 $this->assertSession()->elementTextNotContains($selector, $locator, $text);
786ea937
DM
181 }
182
183 /**
40923977 184 * Checks, that element of specified type is disabled.
786ea937 185 *
40923977 186 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
1f9ffbdb 187 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
188 * @param string $element Element we look in
189 * @param string $selectortype The type of element where we are looking in.
786ea937 190 */
40923977 191 public function the_element_should_be_disabled($element, $selectortype) {
786ea937 192
40923977
DM
193 // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
194 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
195
196 if (!$node->hasAttribute('disabled')) {
197 throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
198 }
199 }
200
201 /**
40923977 202 * Checks, that element of specified type is enabled.
786ea937 203 *
40923977 204 * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
1f9ffbdb 205 * @throws ExpectationException Thrown by behat_base::find
40923977
DM
206 * @param string $element Element we look on
207 * @param string $selectortype The type of where we look
786ea937 208 */
40923977 209 public function the_element_should_be_enabled($element, $selectortype) {
1f9ffbdb 210
40923977
DM
211 // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement.
212 $node = $this->get_selected_node($selectortype, $element);
786ea937
DM
213
214 if ($node->hasAttribute('disabled')) {
215 throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
216 }
217 }
218
219}