MDL-38025 behat: Replacing selector-based steps by human-friendly options
[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() {
53 $this->getSession()->visit($this->locatePath('/'));
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
786ea937
DM
61 */
62 public function click_link($link) {
1f9ffbdb
DM
63
64 $linknode = $this->find_link($link);
65 $linknode->click();
786ea937
DM
66 }
67
68 /**
69 * Waits X seconds. Required after an action that requires data from an AJAX request.
70 *
71 * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
72 * @param int $seconds
73 */
74 public function i_wait_seconds($seconds) {
75 $this->getSession()->wait($seconds * 1000, false);
76 }
77
78 /**
79 * Waits until the page is completely loaded. This step is auto-executed after every step.
80 *
81 * @Given /^I wait until the page is ready$/
82 */
83 public function wait_until_the_page_is_ready() {
84 $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")');
85 }
86
87 /**
88 * Mouse over a CSS element.
89 *
786ea937 90 * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)"$/
1f9ffbdb 91 * @throws ExpectationException Thrown by behat_base::find
786ea937
DM
92 * @param string $element
93 */
94 public function i_hover($element) {
1f9ffbdb
DM
95
96 $exception = new ExpectationException(
97 'The hovered element "' . $element . '" was not found anywhere in the page', $this->getSession()
98 );
99
100 $node = $this->find('css', $element, $exception);
786ea937
DM
101 $node->mouseOver();
102 }
103
104 /**
105 * Checks, that page contains specified text.
106 *
107 * @see Behat\MinkExtension\Context\MinkContext
108 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
109 */
110 public function assert_page_contains_text($text) {
02ca6219 111 $this->assertSession()->pageTextContains($text);
786ea937
DM
112 }
113
114 /**
115 * Checks, that page doesn't contain specified text.
116 *
117 * @see Behat\MinkExtension\Context\MinkContext
118 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
119 */
120 public function assert_page_not_contains_text($text) {
02ca6219 121 $this->assertSession()->pageTextNotContains($text);
786ea937
DM
122 }
123
124 /**
125 * Checks, that element with specified CSS contains specified text.
126 *
127 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" element$/
128 */
9776ddee 129 public function assert_element_contains_text($text, $element) {
02ca6219 130 $this->assertSession()->elementTextContains('css', $element, $text);
786ea937
DM
131 }
132
133 /**
134 * Checks, that element with specified CSS doesn't contain specified text.
135 *
136 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" element$/
137 */
9776ddee 138 public function assert_element_not_contains_text($text, $element) {
02ca6219 139 $this->assertSession()->elementTextNotContains('css', $element, $text);
786ea937
DM
140 }
141
142 /**
143 * Checks, that element with given CSS is disabled.
144 *
786ea937 145 * @Then /^the element "(?P<element_string>(?:[^"]|\\")*)" should be disabled$/
1f9ffbdb 146 * @throws ExpectationException Thrown by behat_base::find
786ea937
DM
147 * @param string $element
148 */
149 public function the_element_should_be_disabled($element) {
150
1f9ffbdb
DM
151 $exception = new ExpectationException('There is no "' . $element . '" element', $this->getSession());
152 $node = $this->find('css', $element, $exception);
786ea937
DM
153
154 if (!$node->hasAttribute('disabled')) {
155 throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
156 }
157 }
158
159 /**
160 * Checks, that element with given CSS is enabled.
161 *
786ea937 162 * @Then /^the element "(?P<element_string>(?:[^"]|\\")*)" should be enabled$/
1f9ffbdb 163 * @throws ExpectationException Thrown by behat_base::find
786ea937
DM
164 * @param string $element
165 */
166 public function the_element_should_be_enabled($element) {
1f9ffbdb
DM
167
168 $exception = new ExpectationException('There is no "' . $element . '" element', $this->getSession());
169 $node = $this->find('css', $element, $exception);
786ea937
DM
170
171 if ($node->hasAttribute('disabled')) {
172 throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
173 }
174 }
175
176}