[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 * Generic moodleforms field. 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 28 use Behat\Mink\Session as Session, 29 Behat\Mink\Element\NodeElement as NodeElement; 30 31 /** 32 * Representation of a form field. 33 * 34 * Basically an interface with Mink session. 35 * 36 * @package core_form 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_form_field { 42 43 /** 44 * @var Session Behat session. 45 */ 46 protected $session; 47 48 /** 49 * @var NodeElement The field DOM node to interact with. 50 */ 51 protected $field; 52 53 /** 54 * @var string The field's locator. 55 */ 56 protected $fieldlocator = false; 57 58 59 /** 60 * General constructor with the node and the session to interact with. 61 * 62 * @param Session $session Reference to Mink session to traverse/modify the page DOM. 63 * @param NodeElement $fieldnode The field DOM node 64 * @return void 65 */ 66 public function __construct(Session $session, NodeElement $fieldnode) { 67 $this->session = $session; 68 $this->field = $fieldnode; 69 } 70 71 /** 72 * Sets the value to a field. 73 * 74 * @param string $value 75 * @return void 76 */ 77 public function set_value($value) { 78 // We delegate to the best guess, if we arrived here 79 // using the generic behat_form_field is because we are 80 // dealing with a fgroup element. 81 $instance = $this->guess_type(); 82 return $instance->set_value($value); 83 } 84 85 /** 86 * Returns the current value of the select element. 87 * 88 * @return string 89 */ 90 public function get_value() { 91 // We delegate to the best guess, if we arrived here 92 // using the generic behat_form_field is because we are 93 // dealing with a fgroup element. 94 $instance = $this->guess_type(); 95 return $instance->get_value(); 96 } 97 98 /** 99 * Generic match implementation 100 * 101 * Will work well with text-based fields, extension required 102 * for most of the other cases. 103 * 104 * @param string $expectedvalue 105 * @return bool The provided value matches the field value? 106 */ 107 public function matches($expectedvalue) { 108 // We delegate to the best guess, if we arrived here 109 // using the generic behat_form_field is because we are 110 // dealing with a fgroup element. 111 $instance = $this->guess_type(); 112 return $instance->matches($expectedvalue); 113 } 114 115 /** 116 * Guesses the element type we are dealing with in case is not a text-based element. 117 * 118 * This class is the generic field type, behat_field_manager::get_form_field() 119 * should be able to find the appropiate class for the field type, but 120 * in cases like moodle form group elements we can not find the type of 121 * the field through the DOM so we also need to take care of the 122 * different field types from here. If we need to deal with more complex 123 * moodle form elements we will need to refactor this simple HTML elements 124 * guess method. 125 * 126 * @return behat_form_field 127 */ 128 private function guess_type() { 129 global $CFG; 130 131 // We default to the text-based field if nothing was detected. 132 if (!$type = behat_field_manager::guess_field_type($this->field, $this->session)) { 133 $type = 'text'; 134 } 135 136 $classname = 'behat_form_' . $type; 137 $classpath = $CFG->dirroot . '/lib/behat/form_field/' . $classname . '.php'; 138 require_once($classpath); 139 return new $classname($this->session, $this->field); 140 } 141 142 /** 143 * Returns whether the scenario is running in a browser that can run Javascript or not. 144 * 145 * @return bool 146 */ 147 protected function running_javascript() { 148 return get_class($this->session->getDriver()) !== 'Behat\Mink\Driver\GoutteDriver'; 149 } 150 151 /** 152 * Gets the field internal id used by selenium wire protocol. 153 * 154 * Only available when running_javascript(). 155 * 156 * @throws coding_exception 157 * @return int 158 */ 159 protected function get_internal_field_id() { 160 161 if (!$this->running_javascript()) { 162 throw new coding_exception('You can only get an internal ID using the selenium driver.'); 163 } 164 165 return $this->session->getDriver()->getWebDriverSession()->element('xpath', $this->field->getXPath())->getID(); 166 } 167 168 /** 169 * Checks if the provided text matches the field value. 170 * 171 * @param string $expectedvalue 172 * @return bool 173 */ 174 protected function text_matches($expectedvalue) { 175 if (trim($expectedvalue) != trim($this->get_value())) { 176 return false; 177 } 178 return true; 179 } 180 181 /** 182 * Gets the field locator. 183 * 184 * Defaults to the field label but you can 185 * specify other locators if you are interested. 186 * 187 * Public visibility as in most cases will be hard to 188 * use this method in a generic way, as fields can 189 * be selected using multiple ways (label, id, name...). 190 * 191 * @throws coding_exception 192 * @param string $locatortype 193 * @return string 194 */ 195 protected function get_field_locator($locatortype = false) { 196 197 if (!empty($this->fieldlocator)) { 198 return $this->fieldlocator; 199 } 200 201 $fieldid = $this->field->getAttribute('id'); 202 203 // Defaults to label. 204 if ($locatortype == 'label' || $locatortype == false) { 205 206 $labelnode = $this->session->getPage()->find('xpath', '//label[@for="' . $fieldid . '"]'); 207 208 // Exception only if $locatortype was specified. 209 if (!$labelnode && $locatortype == 'label') { 210 throw new coding_exception('Field with "' . $fieldid . '" id does not have a label.'); 211 } 212 213 $this->fieldlocator = $labelnode->getText(); 214 } 215 216 // Let's look for the name as a second option (more popular than 217 // id's when pointing to fields). 218 if (($locatortype == 'name' || $locatortype == false) && 219 empty($this->fieldlocator)) { 220 221 $name = $this->field->getAttribute('name'); 222 223 // Exception only if $locatortype was specified. 224 if (!$name && $locatortype == 'name') { 225 throw new coding_exception('Field with "' . $fieldid . '" id does not have a name attribute.'); 226 } 227 228 $this->fieldlocator = $name; 229 } 230 231 // Otherwise returns the id if no specific locator type was provided. 232 if (empty($this->fieldlocator)) { 233 $this->fieldlocator = $fieldid; 234 } 235 236 return $this->fieldlocator; 237 } 238 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |