MDL-25505 behat: Adding a time gap between two cron runs.
[moodle.git] / lib / behat / form_field / behat_form_select.php
CommitLineData
23ebc481
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 * 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 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28require_once(__DIR__ . '/behat_form_field.php');
29
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 */
38class behat_form_select extends behat_form_field {
39
40 /**
41 * Sets the value of a single select.
42 *
d1e55a47
DM
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 *
23ebc481
DM
47 * @param string $value
48 * @return void
49 */
50 public function set_value($value) {
28abad1a 51
d1e55a47
DM
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.
56
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.
28abad1a 60 if ($this->running_javascript()) {
d1e55a47
DM
61 $currentelementid = $this->get_internal_field_id();
62 }
28abad1a 63
d1e55a47
DM
64 // Here we select an option.
65 $this->field->selectOption($value);
1fb97157 66
d1e55a47
DM
67 // With JS disabled this is enough and we finish here.
68 if (!$this->running_javascript()) {
69 return;
70 }
38976081 71
d1e55a47
DM
72 // With JS enabled we add more clicks as some selenium
73 // drivers requires it to fire JS events.
38976081 74
d1e55a47
DM
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 }
28abad1a 82
d1e55a47
DM
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 }
89
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 }
96
405cdd04
DM
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 }
107
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);
111
d1e55a47 112 // Single select sometimes needs an extra click in the option.
405cdd04 113 if (!$multiple) {
d1e55a47
DM
114
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) {
fff500c7
DM
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 }
28abad1a 127 }
d1e55a47
DM
128
129 } else {
afe9f42a 130
fff500c7
DM
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 }
d1e55a47
DM
140
141 // We ensure that the option is still there.
142 if (!$this->session->getDriver()->find($optionxpath)) {
143 return;
144 }
145
afe9f42a
DM
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);
149
fff500c7
DM
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 }
28abad1a 160 }
23ebc481
DM
161 }
162
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 }
172}