PageRenderTime 70ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/tests/behat/behat_general.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 364 lines | 126 code | 45 blank | 193 comment | 4 complexity | d55639fe6e192e506ccbc6de491f010d MD5 | raw file
  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. * General use steps definitions.
  18. *
  19. * @package core
  20. * @category test
  21. * @copyright 2012 David MonllaĆ³
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  25. require_once(__DIR__ . '/../../behat/behat_base.php');
  26. use Behat\Mink\Exception\ExpectationException as ExpectationException,
  27. Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
  28. /**
  29. * Cross component steps definitions.
  30. *
  31. * Basic web application definitions from MinkExtension and
  32. * BehatchExtension. Definitions modified according to our needs
  33. * when necessary and including only the ones we need to avoid
  34. * overlapping and confusion.
  35. *
  36. * @package core
  37. * @category test
  38. * @copyright 2012 David MonllaĆ³
  39. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40. */
  41. class behat_general extends behat_base {
  42. /**
  43. * Opens Moodle homepage.
  44. *
  45. * @Given /^I am on homepage$/
  46. */
  47. public function i_am_on_homepage() {
  48. $this->getSession()->visit($this->locate_path('/'));
  49. }
  50. /**
  51. * Reloads the current page.
  52. *
  53. * @Given /^I reload the page$/
  54. */
  55. public function reload() {
  56. $this->getSession()->reload();
  57. }
  58. /**
  59. * Switches to the specified window. Useful when interacting with popup windows.
  60. *
  61. * @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" window$/
  62. * @param string $windowname
  63. */
  64. public function switch_to_window($windowname) {
  65. $this->getSession()->switchToWindow($windowname);
  66. }
  67. /**
  68. * Switches to the main Moodle window. Useful when you finish interacting with popup windows.
  69. *
  70. * @Given /^I switch to the main window$/
  71. */
  72. public function switch_to_the_main_window() {
  73. $this->getSession()->switchToWindow();
  74. }
  75. /**
  76. * Clicks link with specified id|title|alt|text.
  77. *
  78. * @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
  79. * @throws ElementNotFoundException Thrown by behat_base::find
  80. * @param string $link
  81. */
  82. public function click_link($link) {
  83. $linknode = $this->find_link($link);
  84. $linknode->click();
  85. }
  86. /**
  87. * Waits X seconds. Required after an action that requires data from an AJAX request.
  88. *
  89. * @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
  90. * @param int $seconds
  91. */
  92. public function i_wait_seconds($seconds) {
  93. $this->getSession()->wait($seconds * 1000, false);
  94. }
  95. /**
  96. * Waits until the page is completely loaded. This step is auto-executed after every step.
  97. *
  98. * @Given /^I wait until the page is ready$/
  99. */
  100. public function wait_until_the_page_is_ready() {
  101. $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")');
  102. }
  103. /**
  104. * Generic mouse over action. Mouse over a element of the specified type.
  105. *
  106. * @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
  107. * @param string $element Element we look for
  108. * @param string $selectortype The type of what we look for
  109. */
  110. public function i_hover($element, $selectortype) {
  111. // Gets the node based on the requested selector type and locator.
  112. $node = $this->get_selected_node($selectortype, $element);
  113. $node->mouseOver();
  114. }
  115. /**
  116. * Generic click action. Click on the element of the specified type.
  117. *
  118. * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
  119. * @param string $element Element we look for
  120. * @param string $selectortype The type of what we look for
  121. */
  122. public function i_click_on($element, $selectortype) {
  123. // Gets the node based on the requested selector type and locator.
  124. $node = $this->get_selected_node($selectortype, $element);
  125. $node->click();
  126. }
  127. /**
  128. * Click on the element of the specified type which is located inside the second element.
  129. *
  130. * @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
  131. * @param string $element Element we look for
  132. * @param string $selectortype The type of what we look for
  133. * @param string $nodeelement Element we look in
  134. * @param string $nodeselectortype The type of selector where we look in
  135. */
  136. public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
  137. $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
  138. $node->click();
  139. }
  140. /**
  141. * Click on the specified element inside a table row containing the specified text.
  142. *
  143. * @Given /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" in the "(?P<row_text_string>(?:[^"]|\\")*)" table row$/
  144. * @throws ElementNotFoundException
  145. * @param string $element Element we look for
  146. * @param string $selectortype The type of what we look for
  147. * @param string $tablerowtext The table row text
  148. */
  149. public function i_click_on_in_the_table_row($element, $selectortype, $tablerowtext) {
  150. // The table row container.
  151. $nocontainerexception = new ElementNotFoundException($this->getSession(), '"' . $tablerowtext . '" row text ');
  152. $tablerowtext = str_replace("'", "\'", $tablerowtext);
  153. $rownode = $this->find('xpath', "//tr[contains(., '" . $tablerowtext . "')]", $nocontainerexception);
  154. // Looking for the element DOM node inside the specified row.
  155. list($selector, $locator) = $this->transform_selector($selectortype, $element);
  156. $elementnode = $this->find($selector, $locator, false, $rownode);
  157. $elementnode->click();
  158. }
  159. /**
  160. * Checks, that page contains specified text.
  161. *
  162. * @see Behat\MinkExtension\Context\MinkContext
  163. * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
  164. * @param string $text
  165. */
  166. public function assert_page_contains_text($text) {
  167. $this->assertSession()->pageTextContains($text);
  168. }
  169. /**
  170. * Checks, that page doesn't contain specified text.
  171. *
  172. * @see Behat\MinkExtension\Context\MinkContext
  173. * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
  174. * @param string $text
  175. */
  176. public function assert_page_not_contains_text($text) {
  177. $this->assertSession()->pageTextNotContains($text);
  178. }
  179. /**
  180. * Checks, that element with specified CSS selector or XPath contains specified text.
  181. *
  182. * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
  183. * @param string $text
  184. * @param string $element Element we look in.
  185. * @param string $selectortype The type of element where we are looking in.
  186. */
  187. public function assert_element_contains_text($text, $element, $selectortype) {
  188. // Transforming from steps definitions selector/locator format to Mink format.
  189. list($selector, $locator) = $this->transform_text_selector($selectortype, $element);
  190. $this->assertSession()->elementTextContains($selector, $locator, $text);
  191. }
  192. /**
  193. * Checks, that element with specified CSS selector or XPath doesn't contain specified text.
  194. *
  195. * @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
  196. * @param string $text
  197. * @param string $element Element we look in.
  198. * @param string $selectortype The type of element where we are looking in.
  199. */
  200. public function assert_element_not_contains_text($text, $element, $selectortype) {
  201. // Transforming from steps definitions selector/locator format to mink format.
  202. list($selector, $locator) = $this->transform_text_selector($selectortype, $element);
  203. $this->assertSession()->elementTextNotContains($selector, $locator, $text);
  204. }
  205. /**
  206. * Checks, that the first specified element appears before the second one.
  207. *
  208. * @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
  209. * @throws ExpectationException
  210. * @param string $preelement The locator of the preceding element
  211. * @param string $preselectortype The locator of the preceding element
  212. * @param string $postelement The locator of the latest element
  213. * @param string $postselectortype The selector type of the latest element
  214. */
  215. public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) {
  216. // We allow postselectortype as a non-text based selector.
  217. list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
  218. list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
  219. $prexpath = $this->find($preselector, $prelocator)->getXpath();
  220. $postxpath = $this->find($postselector, $postlocator)->getXpath();
  221. // Using following xpath axe to find it.
  222. $msg = '"'.$preelement.'" "'.$preselectortype.'" does not appear before "'.$postelement.'" "'.$postselectortype.'"';
  223. $xpath = $prexpath.'/following::*[contains(., '.$postxpath.')]';
  224. if (!$this->getSession()->getDriver()->find($xpath)) {
  225. throw new ExpectationException($msg, $this->getSession());
  226. }
  227. }
  228. /**
  229. * Checks, that the first specified element appears after the second one.
  230. *
  231. * @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
  232. * @throws ExpectationException
  233. * @param string $postelement The locator of the latest element
  234. * @param string $postselectortype The selector type of the latest element
  235. * @param string $preelement The locator of the preceding element
  236. * @param string $preselectortype The locator of the preceding element
  237. */
  238. public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) {
  239. // We allow postselectortype as a non-text based selector.
  240. list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
  241. list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
  242. $postxpath = $this->find($postselector, $postlocator)->getXpath();
  243. $prexpath = $this->find($preselector, $prelocator)->getXpath();
  244. // Using preceding xpath axe to find it.
  245. $msg = '"'.$postelement.'" "'.$postselectortype.'" does not appear after "'.$preelement.'" "'.$preselectortype.'"';
  246. $xpath = $postxpath.'/preceding::*[contains(., '.$prexpath.')]';
  247. if (!$this->getSession()->getDriver()->find($xpath)) {
  248. throw new ExpectationException($msg, $this->getSession());
  249. }
  250. }
  251. /**
  252. * Checks, that element of specified type is disabled.
  253. *
  254. * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
  255. * @throws ExpectationException Thrown by behat_base::find
  256. * @param string $element Element we look in
  257. * @param string $selectortype The type of element where we are looking in.
  258. */
  259. public function the_element_should_be_disabled($element, $selectortype) {
  260. // Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
  261. $node = $this->get_selected_node($selectortype, $element);
  262. if (!$node->hasAttribute('disabled')) {
  263. throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
  264. }
  265. }
  266. /**
  267. * Checks, that element of specified type is enabled.
  268. *
  269. * @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
  270. * @throws ExpectationException Thrown by behat_base::find
  271. * @param string $element Element we look on
  272. * @param string $selectortype The type of where we look
  273. */
  274. public function the_element_should_be_enabled($element, $selectortype) {
  275. // Transforming from steps definitions selector/locator format to mink format and getting the NodeElement.
  276. $node = $this->get_selected_node($selectortype, $element);
  277. if ($node->hasAttribute('disabled')) {
  278. throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
  279. }
  280. }
  281. /**
  282. * Checks the provided element and selector type exists in the current page.
  283. *
  284. * This step is for advanced users, use it if you don't find anything else suitable for what you need.
  285. *
  286. * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exists$/
  287. * @throws ElementNotFoundException Thrown by behat_base::find
  288. * @param string $element The locator of the specified selector
  289. * @param string $selectortype The selector type
  290. */
  291. public function should_exists($element, $selectortype) {
  292. // Getting Mink selector and locator.
  293. list($selector, $locator) = $this->transform_selector($selectortype, $element);
  294. // Will throw an ElementNotFoundException if it does not exist.
  295. $this->find($selector, $locator);
  296. }
  297. /**
  298. * Checks that the provided element and selector type not exists in the current page.
  299. *
  300. * This step is for advanced users, use it if you don't find anything else suitable for what you need.
  301. *
  302. * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exists$/
  303. * @throws ExpectationException
  304. * @param string $element The locator of the specified selector
  305. * @param string $selectortype The selector type
  306. */
  307. public function should_not_exists($element, $selectortype) {
  308. try {
  309. $this->should_exists($element, $selectortype);
  310. throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession());
  311. } catch (ElementNotFoundException $e) {
  312. // It passes.
  313. return;
  314. }
  315. }
  316. }