MDL-25505 behat: Adding a time gap between two cron runs.
[moodle.git] / lib / behat / form_field / behat_form_select.php
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/>.
17 /**
18  * Single select form field class.
19  *
20  * @package    core_form
21  * @category   test
22  * @copyright  2012 David Monllaó
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__  . '/behat_form_field.php');
30 /**
31  * Single select form field.
32  *
33  * @package    core_form
34  * @category   test
35  * @copyright  2012 David Monllaó
36  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class behat_form_select extends behat_form_field {
40     /**
41      * Sets the value of a single select.
42      *
43      * Seems an easy select, but there are lots of combinations
44      * of browsers and operative systems and each one manages the
45      * autosubmits and the multiple option selects in a diferent way.
46      *
47      * @param string $value
48      * @return void
49      */
50     public function set_value($value) {
52         // In some browsers we select an option and it triggers all the
53         // autosubmits and works as expected but not in all of them, so we
54         // try to catch all the possibilities to make this function work as
55         // expected.
57         // Get the internal id of the element we are going to click.
58         // This kind of internal IDs are only available in the selenium wire
59         // protocol, so only available using selenium drivers, phantomjs and family.
60         if ($this->running_javascript()) {
61             $currentelementid = $this->get_internal_field_id();
62         }
64         // Here we select an option.
65         $this->field->selectOption($value);
67         // With JS disabled this is enough and we finish here.
68         if (!$this->running_javascript()) {
69             return;
70         }
72         // With JS enabled we add more clicks as some selenium
73         // drivers requires it to fire JS events.
75         // In some browsers the selectOption actions can perform a form submit or reload page
76         // so we need to ensure the element is still available to continue interacting
77         // with it. We don't wait here.
78         $selectxpath = $this->field->getXpath();
79         if (!$this->session->getDriver()->find($selectxpath)) {
80             return;
81         }
83         // We also check the selenium internal element id, if it have changed
84         // we are dealing with an autosubmit that was already executed, and we don't to
85         // execute anything else as the action we wanted was already performed.
86         if ($currentelementid != $this->get_internal_field_id()) {
87             return;
88         }
90         // We also check that the option is still there. We neither wait.
91         $valueliteral = $this->session->getSelectorsHandler()->xpathLiteral($value);
92         $optionxpath = $selectxpath . "/descendant::option[(./@value=$valueliteral or normalize-space(.)=$valueliteral)]";
93         if (!$this->session->getDriver()->find($optionxpath)) {
94             return;
95         }
97         // Wrapped in try & catch as the element may disappear if an AJAX request was submitted.
98         try {
99             $multiple = $this->field->hasAttribute('multiple');
100         } catch (Exception $e) {
101             // We do not specify any specific Exception type as there are
102             // different exceptions that can be thrown by the driver and
103             // we can not control them all, also depending on the selenium
104             // version the exception type can change.
105             return;
106         }
108         // Wait for all the possible AJAX requests that have been
109         // already triggered by selectOption() to be finished.
110         $this->session->wait(behat_base::TIMEOUT * 1000, behat_base::PAGE_READY_JS);
112         // Single select sometimes needs an extra click in the option.
113         if (!$multiple) {
115             // Using the driver direcly because Element methods are messy when dealing
116             // with elements inside containers.
117             $optionnodes = $this->session->getDriver()->find($optionxpath);
118             if ($optionnodes) {
119                 // Wrapped in a try & catch as we can fall into race conditions
120                 // and the element may not be there.
121                 try {
122                     current($optionnodes)->click();
123                 } catch (Exception $e) {
124                     // We continue and return as this means that the element is not there or it is not the same.
125                     return;
126                 }
127             }
129         } else {
131             // Wrapped in a try & catch as we can fall into race conditions
132             // and the element may not be there.
133             try {
134                 // Multiple ones needs the click in the select.
135                 $this->field->click();
136             } catch (Exception $e) {
137                 // We continue and return as this means that the element is not there or it is not the same.
138                 return;
139             }
141             // We ensure that the option is still there.
142             if (!$this->session->getDriver()->find($optionxpath)) {
143                 return;
144             }
146             // Wait for all the possible AJAX requests that have been
147             // already triggered by selectOption() to be finished.
148             $this->session->wait(behat_base::TIMEOUT * 1000, behat_base::PAGE_READY_JS);
150             // Wrapped in a try & catch as we can fall into race conditions
151             // and the element may not be there.
152             try {
153                 // Repeating the select as some drivers (chrome that I know) are moving
154                 // to another option after the general select field click above.
155                 $this->field->selectOption($value);
156             } catch (Exception $e) {
157                 // We continue and return as this means that the element is not there or it is not the same.
158                 return;
159             }
160         }
161     }
163     /**
164      * Returns the text of the current value.
165      *
166      * @return string
167      */
168     public function get_value() {
169         $selectedoption = $this->field->find('xpath', '//option[@selected="selected"]');
170         return $selectedoption->getText();
171     }