weekly release 2.5dev
[moodle.git] / lib / behat / behat_base.php
CommitLineData
d4322e38
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 * Base class of all steps definitions.
19 *
20 * This script is only called from Behat as part of it's integration
21 * in Moodle.
22 *
23 * @package core
24 * @category test
25 * @copyright 2012 David MonllaĆ³
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
30
31/**
32 * Steps definitions base class.
33 *
34 * To extend by the steps definitions of the different Moodle components.
35 *
36 * It can not contain steps definitions to avoid duplicates, only utility
37 * methods shared between steps.
38 *
39 * @package core
40 * @category test
41 * @copyright 2012 David MonllaĆ³
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class behat_base extends Behat\MinkExtension\Context\RawMinkContext {
45
46 /**
47 * The timeout for each Behat step (load page, wait for an element to load...).
48 */
49 const TIMEOUT = 6;
50
786ea937
DM
51 /**
52 * Returns fixed step argument (with \\" replaced back to ").
53 *
54 * \\ is the chars combination to add when you
55 * want to escape the " character that is used as var
56 * delimiter.
57 *
58 * @see Behat\MinkExtension\Context\MinkContext
59 * @param string $argument
60 * @return string
61 */
62 protected function fixStepArgument($argument) {
63 return str_replace('\\"', '"', $argument);
64 }
65
66 /**
67 * Locates url, based on provided path.
68 * Override to provide custom routing mechanism.
69 *
70 * @see Behat\MinkExtension\Context\MinkContext
71 * @param string $path
72 * @return string
73 */
74 protected function locatePath($path) {
75 $startUrl = rtrim($this->getMinkParameter('base_url'), '/') . '/';
76 return 0 !== strpos($path, 'http') ? $startUrl . ltrim($path, '/') : $path;
77 }
78
79 /**
80 * Executes the passed closure until returns true or time outs.
81 *
82 * In most cases the document.readyState === 'complete' will be enough, but sometimes JS
83 * requires more time to be completely loaded or an element to be visible or whatever is required to
84 * perform some action on an element; this method receives a closure which should contain the
85 * required statements to ensure the step definition actions and assertions have all their needs
86 * satisfied and executes it until they are satisfied or it timeouts. Redirects the return of the
87 * closure to the caller.
88 *
89 * The closures requirements to work well with this spin method are:
90 * - Must return false, null or '' if something goes wrong
91 * - Must return something != false if finishes as expected, this will be the (mixed) value
92 * returned by spin()
93 *
94 * Requires the exception to provide more accurate feedback to tests writers.
95 *
96 * @throws Exception If it timeouts without receiving something != false from the closure
97 * @param Closure $lambda The function to execute.
98 * @param Exception $exception The exception to throw in case it time outs.
99 * @param array $args Arguments to pass to the closure
100 * @return mixed The value returned by the closure
101 */
102 protected function spin($lambda, $exception, $args, $timeout = false) {
103
104 // Using default timeout which is pretty high.
105 if (!$timeout) {
106 $timeout = self::TIMEOUT;
107 }
108
109 for ($i = 0; $i < $timeout; $i++) {
110
111 // We catch the exception thrown by the step definition to execute it again.
112 try {
113
114 // We don't check with !== because most of the time closures will return
115 // direct Behat methods returns and we are not sure it will be always (bool)false.
116 if ($return = $lambda($this, $args)) {
117 return $return;
118 }
119 } catch(Exception $e) {
120 // We wait until no exception is thrown or timeout expires.
121 continue;
122 }
123
124 sleep(1);
125 }
126
127 // Throwing exception to the user.
128 throw $exception;
129 }
130
d4322e38 131}