MDL-37912 behat: Use text area if there is no html editor
[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) {
63 $link = $this->fixStepArgument($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 /**
89 * Mouse over a CSS element.
90 *
786ea937 91 * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)"$/
1f9ffbdb 92 * @throws ExpectationException Thrown by behat_base::find
786ea937
DM
93 * @param string $element
94 */
95 public function i_hover($element) {
1f9ffbdb
DM
96
97 $exception = new ExpectationException(
98 'The hovered element "' . $element . '" was not found anywhere in the page', $this->getSession()
99 );
100
101 $node = $this->find('css', $element, $exception);
786ea937
DM
102 $node->mouseOver();
103 }
104
105 /**
106 * Checks, that page contains specified text.
107 *
108 * @see Behat\MinkExtension\Context\MinkContext
109 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
110 */
111 public function assert_page_contains_text($text) {
112 $this->assertSession()->pageTextContains($this->fixStepArgument($text));
113 }
114
115 /**
116 * Checks, that page doesn't contain specified text.
117 *
118 * @see Behat\MinkExtension\Context\MinkContext
119 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
120 */
121 public function assert_page_not_contains_text($text) {
122 $this->assertSession()->pageTextNotContains($this->fixStepArgument($text));
123 }
124
125 /**
126 * Checks, that element with specified CSS contains specified text.
127 *
128 * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" element$/
129 */
9776ddee 130 public function assert_element_contains_text($text, $element) {
786ea937
DM
131 $this->assertSession()->elementTextContains('css', $element, $this->fixStepArgument($text));
132 }
133
134 /**
135 * Checks, that element with specified CSS doesn't contain specified text.
136 *
137 * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" element$/
138 */
9776ddee 139 public function assert_element_not_contains_text($text, $element) {
786ea937
DM
140 $this->assertSession()->elementTextNotContains('css', $element, $this->fixStepArgument($text));
141 }
142
143 /**
144 * Checks, that element with given CSS is disabled.
145 *
786ea937 146 * @Then /^the element "(?P<element_string>(?:[^"]|\\")*)" should be disabled$/
1f9ffbdb 147 * @throws ExpectationException Thrown by behat_base::find
786ea937
DM
148 * @param string $element
149 */
150 public function the_element_should_be_disabled($element) {
151
1f9ffbdb
DM
152 $exception = new ExpectationException('There is no "' . $element . '" element', $this->getSession());
153 $node = $this->find('css', $element, $exception);
786ea937
DM
154
155 if (!$node->hasAttribute('disabled')) {
156 throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
157 }
158 }
159
160 /**
161 * Checks, that element with given CSS is enabled.
162 *
786ea937 163 * @Then /^the element "(?P<element_string>(?:[^"]|\\")*)" should be enabled$/
1f9ffbdb 164 * @throws ExpectationException Thrown by behat_base::find
786ea937
DM
165 * @param string $element
166 */
167 public function the_element_should_be_enabled($element) {
1f9ffbdb
DM
168
169 $exception = new ExpectationException('There is no "' . $element . '" element', $this->getSession());
170 $node = $this->find('css', $element, $exception);
786ea937
DM
171
172 if ($node->hasAttribute('disabled')) {
173 throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
174 }
175 }
176
177}