MDL-37657 behat: General steps definitions
[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 *
50 * @see Behat\MinkExtension\Context\MinkContext
51 * @Given /^I am on homepage$/
52 */
53 public function i_am_on_homepage() {
54 $this->getSession()->visit($this->locatePath('/'));
55 }
56
57 /**
58 * Clicks link with specified id|title|alt|text.
59 *
60 * @see Behat\MinkExtension\Context\MinkContext
61 * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
62 */
63 public function click_link($link) {
64 $link = $this->fixStepArgument($link);
65 $this->getSession()->getPage()->clickLink($link);
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 *
90 * @throws ExpectationException
91 * @see Sanpi/Behatch/Context/BrowserContext.php
92 * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)"$/
93 * @param string $element
94 */
95 public function i_hover($element) {
96 $node = $this->getSession()->getPage()->find('css', $element);
97 if ($node === null) {
98 throw new ExpectationException('The hovered element "' . $element . '" was not found anywhere in the page', $this->getSession());
99 }
100 $node->mouseOver();
101 }
102
103 /**
104 * Checks, that page contains specified text.
105 *
106 * @see Behat\MinkExtension\Context\MinkContext
107 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
108 */
109 public function assert_page_contains_text($text) {
110 $this->assertSession()->pageTextContains($this->fixStepArgument($text));
111 }
112
113 /**
114 * Checks, that page doesn't contain specified text.
115 *
116 * @see Behat\MinkExtension\Context\MinkContext
117 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
118 */
119 public function assert_page_not_contains_text($text) {
120 $this->assertSession()->pageTextNotContains($this->fixStepArgument($text));
121 }
122
123 /**
124 * Checks, that element with specified CSS contains specified text.
125 *
126 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" element$/
127 */
128 public function assert_element_contains_text($element, $text) {
129 $element = $this->fixStepArgument($element);
130 $this->assertSession()->elementTextContains('css', $element, $this->fixStepArgument($text));
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 */
138 public function assert_element_not_contains_text($element, $text) {
139 $element = $this->fixStepArgument($element);
140 $this->assertSession()->elementTextNotContains('css', $element, $this->fixStepArgument($text));
141 }
142
143 /**
144 * Checks, that element with given CSS is disabled.
145 *
146 * @throws ExpectationException
147 * @see Sanpi/Behatch/Context/BrowserContext
148 * @Then /^the element "(?P<element_string>(?:[^"]|\\")*)" should be disabled$/
149 * @param string $element
150 */
151 public function the_element_should_be_disabled($element) {
152
153 $element = $this->fixStepArgument($element);
154
155 $node = $this->getSession()->getPage()->find('css', $element);
156 if ($node == null) {
157 throw new ExpectationException('There is no "' . $element . '" element', $this->getSession());
158 }
159
160 if (!$node->hasAttribute('disabled')) {
161 throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
162 }
163 }
164
165 /**
166 * Checks, that element with given CSS is enabled.
167 *
168 * @throws ExpectationException
169 * @see Sanpi/Behatch/Context/BrowserContext.php
170 * @Then /^the element "(?P<element_string>(?:[^"]|\\")*)" should be enabled$/
171 * @param string $element
172 */
173 public function the_element_should_be_enabled($element) {
174 $node = $this->getSession()->getPage()->find('css', $element);
175 if ($node == null) {
176 throw new ExpectationException('There is no "' . $element . '" element', $this->getSession());
177 }
178
179 if ($node->hasAttribute('disabled')) {
180 throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
181 }
182 }
183
184}