mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
364 lines
15 KiB
PHP
364 lines
15 KiB
PHP
<?php
|
|
// This file is part of Moodle - http://moodle.org/
|
|
//
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
* General use steps definitions.
|
|
*
|
|
* @package core
|
|
* @category test
|
|
* @copyright 2012 David Monllaó
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
|
|
|
|
require_once(__DIR__ . '/../../behat/behat_base.php');
|
|
|
|
use Behat\Mink\Exception\ExpectationException as ExpectationException,
|
|
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
|
|
|
|
/**
|
|
* Cross component steps definitions.
|
|
*
|
|
* Basic web application definitions from MinkExtension and
|
|
* BehatchExtension. Definitions modified according to our needs
|
|
* when necessary and including only the ones we need to avoid
|
|
* overlapping and confusion.
|
|
*
|
|
* @package core
|
|
* @category test
|
|
* @copyright 2012 David Monllaó
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
class behat_general extends behat_base {
|
|
|
|
/**
|
|
* Opens Moodle homepage.
|
|
*
|
|
* @Given /^I am on homepage$/
|
|
*/
|
|
public function i_am_on_homepage() {
|
|
$this->getSession()->visit($this->locate_path('/'));
|
|
}
|
|
|
|
/**
|
|
* Reloads the current page.
|
|
*
|
|
* @Given /^I reload the page$/
|
|
*/
|
|
public function reload() {
|
|
$this->getSession()->reload();
|
|
}
|
|
|
|
/**
|
|
* Switches to the specified window. Useful when interacting with popup windows.
|
|
*
|
|
* @Given /^I switch to "(?P<window_name_string>(?:[^"]|\\")*)" window$/
|
|
* @param string $windowname
|
|
*/
|
|
public function switch_to_window($windowname) {
|
|
$this->getSession()->switchToWindow($windowname);
|
|
}
|
|
|
|
/**
|
|
* Switches to the main Moodle window. Useful when you finish interacting with popup windows.
|
|
*
|
|
* @Given /^I switch to the main window$/
|
|
*/
|
|
public function switch_to_the_main_window() {
|
|
$this->getSession()->switchToWindow();
|
|
}
|
|
|
|
/**
|
|
* Clicks link with specified id|title|alt|text.
|
|
*
|
|
* @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
|
|
* @throws ElementNotFoundException Thrown by behat_base::find
|
|
* @param string $link
|
|
*/
|
|
public function click_link($link) {
|
|
|
|
$linknode = $this->find_link($link);
|
|
$linknode->click();
|
|
}
|
|
|
|
/**
|
|
* Waits X seconds. Required after an action that requires data from an AJAX request.
|
|
*
|
|
* @Then /^I wait "(?P<seconds_number>\d+)" seconds$/
|
|
* @param int $seconds
|
|
*/
|
|
public function i_wait_seconds($seconds) {
|
|
$this->getSession()->wait($seconds * 1000, false);
|
|
}
|
|
|
|
/**
|
|
* Waits until the page is completely loaded. This step is auto-executed after every step.
|
|
*
|
|
* @Given /^I wait until the page is ready$/
|
|
*/
|
|
public function wait_until_the_page_is_ready() {
|
|
$this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")');
|
|
}
|
|
|
|
/**
|
|
* Generic mouse over action. Mouse over a element of the specified type.
|
|
*
|
|
* @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
|
|
* @param string $element Element we look for
|
|
* @param string $selectortype The type of what we look for
|
|
*/
|
|
public function i_hover($element, $selectortype) {
|
|
|
|
// Gets the node based on the requested selector type and locator.
|
|
$node = $this->get_selected_node($selectortype, $element);
|
|
$node->mouseOver();
|
|
}
|
|
|
|
/**
|
|
* Generic click action. Click on the element of the specified type.
|
|
*
|
|
* @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
|
|
* @param string $element Element we look for
|
|
* @param string $selectortype The type of what we look for
|
|
*/
|
|
public function i_click_on($element, $selectortype) {
|
|
|
|
// Gets the node based on the requested selector type and locator.
|
|
$node = $this->get_selected_node($selectortype, $element);
|
|
$node->click();
|
|
}
|
|
|
|
/**
|
|
* Click on the element of the specified type which is located inside the second element.
|
|
*
|
|
* @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
|
|
* @param string $element Element we look for
|
|
* @param string $selectortype The type of what we look for
|
|
* @param string $nodeelement Element we look in
|
|
* @param string $nodeselectortype The type of selector where we look in
|
|
*/
|
|
public function i_click_on_in_the($element, $selectortype, $nodeelement, $nodeselectortype) {
|
|
|
|
$node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
|
|
$node->click();
|
|
}
|
|
|
|
/**
|
|
* Click on the specified element inside a table row containing the specified text.
|
|
*
|
|
* @Given /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" in the "(?P<row_text_string>(?:[^"]|\\")*)" table row$/
|
|
* @throws ElementNotFoundException
|
|
* @param string $element Element we look for
|
|
* @param string $selectortype The type of what we look for
|
|
* @param string $tablerowtext The table row text
|
|
*/
|
|
public function i_click_on_in_the_table_row($element, $selectortype, $tablerowtext) {
|
|
|
|
// The table row container.
|
|
$nocontainerexception = new ElementNotFoundException($this->getSession(), '"' . $tablerowtext . '" row text ');
|
|
$tablerowtext = str_replace("'", "\'", $tablerowtext);
|
|
$rownode = $this->find('xpath', "//tr[contains(., '" . $tablerowtext . "')]", $nocontainerexception);
|
|
|
|
// Looking for the element DOM node inside the specified row.
|
|
list($selector, $locator) = $this->transform_selector($selectortype, $element);
|
|
$elementnode = $this->find($selector, $locator, false, $rownode);
|
|
$elementnode->click();
|
|
}
|
|
|
|
/**
|
|
* Checks, that page contains specified text.
|
|
*
|
|
* @see Behat\MinkExtension\Context\MinkContext
|
|
* @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
|
|
* @param string $text
|
|
*/
|
|
public function assert_page_contains_text($text) {
|
|
$this->assertSession()->pageTextContains($text);
|
|
}
|
|
|
|
/**
|
|
* Checks, that page doesn't contain specified text.
|
|
*
|
|
* @see Behat\MinkExtension\Context\MinkContext
|
|
* @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
|
|
* @param string $text
|
|
*/
|
|
public function assert_page_not_contains_text($text) {
|
|
$this->assertSession()->pageTextNotContains($text);
|
|
}
|
|
|
|
/**
|
|
* Checks, that element with specified CSS selector or XPath contains specified text.
|
|
*
|
|
* @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
|
|
* @param string $text
|
|
* @param string $element Element we look in.
|
|
* @param string $selectortype The type of element where we are looking in.
|
|
*/
|
|
public function assert_element_contains_text($text, $element, $selectortype) {
|
|
|
|
// Transforming from steps definitions selector/locator format to Mink format.
|
|
list($selector, $locator) = $this->transform_text_selector($selectortype, $element);
|
|
$this->assertSession()->elementTextContains($selector, $locator, $text);
|
|
}
|
|
|
|
/**
|
|
* Checks, that element with specified CSS selector or XPath doesn't contain specified text.
|
|
*
|
|
* @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
|
|
* @param string $text
|
|
* @param string $element Element we look in.
|
|
* @param string $selectortype The type of element where we are looking in.
|
|
*/
|
|
public function assert_element_not_contains_text($text, $element, $selectortype) {
|
|
|
|
// Transforming from steps definitions selector/locator format to mink format.
|
|
list($selector, $locator) = $this->transform_text_selector($selectortype, $element);
|
|
$this->assertSession()->elementTextNotContains($selector, $locator, $text);
|
|
}
|
|
|
|
/**
|
|
* Checks, that the first specified element appears before the second one.
|
|
*
|
|
* @Given /^"(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear before "(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
|
|
* @throws ExpectationException
|
|
* @param string $preelement The locator of the preceding element
|
|
* @param string $preselectortype The locator of the preceding element
|
|
* @param string $postelement The locator of the latest element
|
|
* @param string $postselectortype The selector type of the latest element
|
|
*/
|
|
public function should_appear_before($preelement, $preselectortype, $postelement, $postselectortype) {
|
|
|
|
// We allow postselectortype as a non-text based selector.
|
|
list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
|
|
list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
|
|
|
|
$prexpath = $this->find($preselector, $prelocator)->getXpath();
|
|
$postxpath = $this->find($postselector, $postlocator)->getXpath();
|
|
|
|
// Using following xpath axe to find it.
|
|
$msg = '"'.$preelement.'" "'.$preselectortype.'" does not appear before "'.$postelement.'" "'.$postselectortype.'"';
|
|
$xpath = $prexpath.'/following::*[contains(., '.$postxpath.')]';
|
|
if (!$this->getSession()->getDriver()->find($xpath)) {
|
|
throw new ExpectationException($msg, $this->getSession());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks, that the first specified element appears after the second one.
|
|
*
|
|
* @Given /^"(?P<following_element_string>(?:[^"]|\\")*)" "(?P<selector1_string>(?:[^"]|\\")*)" should appear after "(?P<preceding_element_string>(?:[^"]|\\")*)" "(?P<selector2_string>(?:[^"]|\\")*)"$/
|
|
* @throws ExpectationException
|
|
* @param string $postelement The locator of the latest element
|
|
* @param string $postselectortype The selector type of the latest element
|
|
* @param string $preelement The locator of the preceding element
|
|
* @param string $preselectortype The locator of the preceding element
|
|
*/
|
|
public function should_appear_after($postelement, $postselectortype, $preelement, $preselectortype) {
|
|
|
|
// We allow postselectortype as a non-text based selector.
|
|
list($postselector, $postlocator) = $this->transform_selector($postselectortype, $postelement);
|
|
list($preselector, $prelocator) = $this->transform_selector($preselectortype, $preelement);
|
|
|
|
$postxpath = $this->find($postselector, $postlocator)->getXpath();
|
|
$prexpath = $this->find($preselector, $prelocator)->getXpath();
|
|
|
|
// Using preceding xpath axe to find it.
|
|
$msg = '"'.$postelement.'" "'.$postselectortype.'" does not appear after "'.$preelement.'" "'.$preselectortype.'"';
|
|
$xpath = $postxpath.'/preceding::*[contains(., '.$prexpath.')]';
|
|
if (!$this->getSession()->getDriver()->find($xpath)) {
|
|
throw new ExpectationException($msg, $this->getSession());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks, that element of specified type is disabled.
|
|
*
|
|
* @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
|
|
* @throws ExpectationException Thrown by behat_base::find
|
|
* @param string $element Element we look in
|
|
* @param string $selectortype The type of element where we are looking in.
|
|
*/
|
|
public function the_element_should_be_disabled($element, $selectortype) {
|
|
|
|
// Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
|
|
$node = $this->get_selected_node($selectortype, $element);
|
|
|
|
if (!$node->hasAttribute('disabled')) {
|
|
throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks, that element of specified type is enabled.
|
|
*
|
|
* @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
|
|
* @throws ExpectationException Thrown by behat_base::find
|
|
* @param string $element Element we look on
|
|
* @param string $selectortype The type of where we look
|
|
*/
|
|
public function the_element_should_be_enabled($element, $selectortype) {
|
|
|
|
// Transforming from steps definitions selector/locator format to mink format and getting the NodeElement.
|
|
$node = $this->get_selected_node($selectortype, $element);
|
|
|
|
if ($node->hasAttribute('disabled')) {
|
|
throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks the provided element and selector type exists in the current page.
|
|
*
|
|
* This step is for advanced users, use it if you don't find anything else suitable for what you need.
|
|
*
|
|
* @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exists$/
|
|
* @throws ElementNotFoundException Thrown by behat_base::find
|
|
* @param string $element The locator of the specified selector
|
|
* @param string $selectortype The selector type
|
|
*/
|
|
public function should_exists($element, $selectortype) {
|
|
|
|
// Getting Mink selector and locator.
|
|
list($selector, $locator) = $this->transform_selector($selectortype, $element);
|
|
|
|
// Will throw an ElementNotFoundException if it does not exist.
|
|
$this->find($selector, $locator);
|
|
}
|
|
|
|
/**
|
|
* Checks that the provided element and selector type not exists in the current page.
|
|
*
|
|
* This step is for advanced users, use it if you don't find anything else suitable for what you need.
|
|
*
|
|
* @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exists$/
|
|
* @throws ExpectationException
|
|
* @param string $element The locator of the specified selector
|
|
* @param string $selectortype The selector type
|
|
*/
|
|
public function should_not_exists($element, $selectortype) {
|
|
|
|
try {
|
|
$this->should_exists($element, $selectortype);
|
|
throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession());
|
|
} catch (ElementNotFoundException $e) {
|
|
// It passes.
|
|
return;
|
|
}
|
|
}
|
|
|
|
}
|