annotate vendor/behat/mink/src/Element/NodeElement.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Mink package.
Chris@0 5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
Chris@0 6 *
Chris@0 7 * For the full copyright and license information, please view the LICENSE
Chris@0 8 * file that was distributed with this source code.
Chris@0 9 */
Chris@0 10
Chris@0 11 namespace Behat\Mink\Element;
Chris@0 12
Chris@0 13 use Behat\Mink\Session;
Chris@0 14 use Behat\Mink\Exception\ElementNotFoundException;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Page element node.
Chris@0 18 *
Chris@0 19 * @author Konstantin Kudryashov <ever.zet@gmail.com>
Chris@0 20 */
Chris@0 21 class NodeElement extends TraversableElement
Chris@0 22 {
Chris@0 23 private $xpath;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Initializes node element.
Chris@0 27 *
Chris@0 28 * @param string $xpath element xpath
Chris@0 29 * @param Session $session session instance
Chris@0 30 */
Chris@0 31 public function __construct($xpath, Session $session)
Chris@0 32 {
Chris@0 33 $this->xpath = $xpath;
Chris@0 34
Chris@0 35 parent::__construct($session);
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Returns XPath for handled element.
Chris@0 40 *
Chris@0 41 * @return string
Chris@0 42 */
Chris@0 43 public function getXpath()
Chris@0 44 {
Chris@0 45 return $this->xpath;
Chris@0 46 }
Chris@0 47
Chris@0 48 /**
Chris@0 49 * Returns parent element to the current one.
Chris@0 50 *
Chris@0 51 * @return NodeElement
Chris@0 52 */
Chris@0 53 public function getParent()
Chris@0 54 {
Chris@0 55 return $this->find('xpath', '..');
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Returns current node tag name.
Chris@0 60 *
Chris@0 61 * The value is always returned in lowercase to allow an easy comparison.
Chris@0 62 *
Chris@0 63 * @return string
Chris@0 64 */
Chris@0 65 public function getTagName()
Chris@0 66 {
Chris@0 67 return strtolower($this->getDriver()->getTagName($this->getXpath()));
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * Returns the value of the form field or option element.
Chris@0 72 *
Chris@0 73 * For checkbox fields, the value is a boolean indicating whether the checkbox is checked.
Chris@0 74 * For radio buttons, the value is the value of the selected button in the radio group
Chris@0 75 * or null if no button is selected.
Chris@0 76 * For single select boxes, the value is the value of the selected option.
Chris@0 77 * For multiple select boxes, the value is an array of selected option values.
Chris@0 78 * for file inputs, the return value is undefined given that browsers don't allow accessing
Chris@0 79 * the value of file inputs for security reasons. Some drivers may allow accessing the
Chris@0 80 * path of the file set in the field, but this is not required if it cannot be implemented.
Chris@0 81 * For textarea elements and all textual fields, the value is the content of the field.
Chris@0 82 * Form option elements, the value is the value of the option (the value attribute or the text
Chris@0 83 * content if the attribute is not set).
Chris@0 84 *
Chris@0 85 * Calling this method on other elements than form fields or option elements is not allowed.
Chris@0 86 *
Chris@0 87 * @return string|bool|array
Chris@0 88 */
Chris@0 89 public function getValue()
Chris@0 90 {
Chris@0 91 return $this->getDriver()->getValue($this->getXpath());
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * Sets the value of the form field.
Chris@0 96 *
Chris@0 97 * Calling this method on other elements than form fields is not allowed.
Chris@0 98 *
Chris@0 99 * @param string|bool|array $value
Chris@0 100 *
Chris@0 101 * @see NodeElement::getValue for the format of the value for each type of field
Chris@0 102 */
Chris@0 103 public function setValue($value)
Chris@0 104 {
Chris@0 105 $this->getDriver()->setValue($this->getXpath(), $value);
Chris@0 106 }
Chris@0 107
Chris@0 108 /**
Chris@0 109 * Checks whether element has attribute with specified name.
Chris@0 110 *
Chris@0 111 * @param string $name
Chris@0 112 *
Chris@0 113 * @return Boolean
Chris@0 114 */
Chris@0 115 public function hasAttribute($name)
Chris@0 116 {
Chris@0 117 return null !== $this->getDriver()->getAttribute($this->getXpath(), $name);
Chris@0 118 }
Chris@0 119
Chris@0 120 /**
Chris@0 121 * Returns specified attribute value.
Chris@0 122 *
Chris@0 123 * @param string $name
Chris@0 124 *
Chris@0 125 * @return string|null
Chris@0 126 */
Chris@0 127 public function getAttribute($name)
Chris@0 128 {
Chris@0 129 return $this->getDriver()->getAttribute($this->getXpath(), $name);
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * Checks whether an element has a named CSS class.
Chris@0 134 *
Chris@0 135 * @param string $className Name of the class
Chris@0 136 *
Chris@0 137 * @return bool
Chris@0 138 */
Chris@0 139 public function hasClass($className)
Chris@0 140 {
Chris@0 141 if ($this->hasAttribute('class')) {
Chris@0 142 return in_array($className, preg_split('/\s+/', $this->getAttribute('class')));
Chris@0 143 }
Chris@0 144
Chris@0 145 return false;
Chris@0 146 }
Chris@0 147
Chris@0 148 /**
Chris@0 149 * Clicks current node.
Chris@0 150 */
Chris@0 151 public function click()
Chris@0 152 {
Chris@0 153 $this->getDriver()->click($this->getXpath());
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Presses current button.
Chris@0 158 */
Chris@0 159 public function press()
Chris@0 160 {
Chris@0 161 $this->click();
Chris@0 162 }
Chris@0 163
Chris@0 164 /**
Chris@0 165 * Double-clicks current node.
Chris@0 166 */
Chris@0 167 public function doubleClick()
Chris@0 168 {
Chris@0 169 $this->getDriver()->doubleClick($this->getXpath());
Chris@0 170 }
Chris@0 171
Chris@0 172 /**
Chris@0 173 * Right-clicks current node.
Chris@0 174 */
Chris@0 175 public function rightClick()
Chris@0 176 {
Chris@0 177 $this->getDriver()->rightClick($this->getXpath());
Chris@0 178 }
Chris@0 179
Chris@0 180 /**
Chris@0 181 * Checks current node if it's a checkbox field.
Chris@0 182 */
Chris@0 183 public function check()
Chris@0 184 {
Chris@0 185 $this->getDriver()->check($this->getXpath());
Chris@0 186 }
Chris@0 187
Chris@0 188 /**
Chris@0 189 * Unchecks current node if it's a checkbox field.
Chris@0 190 */
Chris@0 191 public function uncheck()
Chris@0 192 {
Chris@0 193 $this->getDriver()->uncheck($this->getXpath());
Chris@0 194 }
Chris@0 195
Chris@0 196 /**
Chris@0 197 * Checks whether current node is checked if it's a checkbox or radio field.
Chris@0 198 *
Chris@0 199 * Calling this method on any other elements is not allowed.
Chris@0 200 *
Chris@0 201 * @return Boolean
Chris@0 202 */
Chris@0 203 public function isChecked()
Chris@0 204 {
Chris@0 205 return (Boolean) $this->getDriver()->isChecked($this->getXpath());
Chris@0 206 }
Chris@0 207
Chris@0 208 /**
Chris@0 209 * Selects specified option for select field or specified radio button in the group.
Chris@0 210 *
Chris@0 211 * If the current node is a select box, this selects the option found by its value or
Chris@0 212 * its text.
Chris@0 213 * If the current node is a radio button, this selects the radio button with the given
Chris@0 214 * value in the radio button group of the current node.
Chris@0 215 *
Chris@0 216 * Calling this method on any other elements is not allowed.
Chris@0 217 *
Chris@0 218 * @param string $option
Chris@0 219 * @param Boolean $multiple whether the option should be added to the selection for multiple selects
Chris@0 220 *
Chris@0 221 * @throws ElementNotFoundException when the option is not found in the select box
Chris@0 222 */
Chris@0 223 public function selectOption($option, $multiple = false)
Chris@0 224 {
Chris@0 225 if ('select' !== $this->getTagName()) {
Chris@0 226 $this->getDriver()->selectOption($this->getXpath(), $option, $multiple);
Chris@0 227
Chris@0 228 return;
Chris@0 229 }
Chris@0 230
Chris@0 231 $opt = $this->find('named', array('option', $option));
Chris@0 232
Chris@0 233 if (null === $opt) {
Chris@0 234 throw new ElementNotFoundException($this->getDriver(), 'select option', 'value|text', $option);
Chris@0 235 }
Chris@0 236
Chris@0 237 $this->getDriver()->selectOption($this->getXpath(), $opt->getValue(), $multiple);
Chris@0 238 }
Chris@0 239
Chris@0 240 /**
Chris@0 241 * Checks whether current node is selected if it's a option field.
Chris@0 242 *
Chris@0 243 * Calling this method on any other elements is not allowed.
Chris@0 244 *
Chris@0 245 * @return Boolean
Chris@0 246 */
Chris@0 247 public function isSelected()
Chris@0 248 {
Chris@0 249 return (Boolean) $this->getDriver()->isSelected($this->getXpath());
Chris@0 250 }
Chris@0 251
Chris@0 252 /**
Chris@0 253 * Attach file to current node if it's a file input.
Chris@0 254 *
Chris@0 255 * Calling this method on any other elements than file input is not allowed.
Chris@0 256 *
Chris@0 257 * @param string $path path to file (local)
Chris@0 258 */
Chris@0 259 public function attachFile($path)
Chris@0 260 {
Chris@0 261 $this->getDriver()->attachFile($this->getXpath(), $path);
Chris@0 262 }
Chris@0 263
Chris@0 264 /**
Chris@0 265 * Checks whether current node is visible on page.
Chris@0 266 *
Chris@0 267 * @return Boolean
Chris@0 268 */
Chris@0 269 public function isVisible()
Chris@0 270 {
Chris@0 271 return (Boolean) $this->getDriver()->isVisible($this->getXpath());
Chris@0 272 }
Chris@0 273
Chris@0 274 /**
Chris@0 275 * Simulates a mouse over on the element.
Chris@0 276 */
Chris@0 277 public function mouseOver()
Chris@0 278 {
Chris@0 279 $this->getDriver()->mouseOver($this->getXpath());
Chris@0 280 }
Chris@0 281
Chris@0 282 /**
Chris@0 283 * Drags current node onto other node.
Chris@0 284 *
Chris@0 285 * @param ElementInterface $destination other node
Chris@0 286 */
Chris@0 287 public function dragTo(ElementInterface $destination)
Chris@0 288 {
Chris@0 289 $this->getDriver()->dragTo($this->getXpath(), $destination->getXpath());
Chris@0 290 }
Chris@0 291
Chris@0 292 /**
Chris@0 293 * Brings focus to element.
Chris@0 294 */
Chris@0 295 public function focus()
Chris@0 296 {
Chris@0 297 $this->getDriver()->focus($this->getXpath());
Chris@0 298 }
Chris@0 299
Chris@0 300 /**
Chris@0 301 * Removes focus from element.
Chris@0 302 */
Chris@0 303 public function blur()
Chris@0 304 {
Chris@0 305 $this->getDriver()->blur($this->getXpath());
Chris@0 306 }
Chris@0 307
Chris@0 308 /**
Chris@0 309 * Presses specific keyboard key.
Chris@0 310 *
Chris@0 311 * @param string|int $char could be either char ('b') or char-code (98)
Chris@0 312 * @param string $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
Chris@0 313 */
Chris@0 314 public function keyPress($char, $modifier = null)
Chris@0 315 {
Chris@0 316 $this->getDriver()->keyPress($this->getXpath(), $char, $modifier);
Chris@0 317 }
Chris@0 318
Chris@0 319 /**
Chris@0 320 * Pressed down specific keyboard key.
Chris@0 321 *
Chris@0 322 * @param string|int $char could be either char ('b') or char-code (98)
Chris@0 323 * @param string $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
Chris@0 324 */
Chris@0 325 public function keyDown($char, $modifier = null)
Chris@0 326 {
Chris@0 327 $this->getDriver()->keyDown($this->getXpath(), $char, $modifier);
Chris@0 328 }
Chris@0 329
Chris@0 330 /**
Chris@0 331 * Pressed up specific keyboard key.
Chris@0 332 *
Chris@0 333 * @param string|int $char could be either char ('b') or char-code (98)
Chris@0 334 * @param string $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
Chris@0 335 */
Chris@0 336 public function keyUp($char, $modifier = null)
Chris@0 337 {
Chris@0 338 $this->getDriver()->keyUp($this->getXpath(), $char, $modifier);
Chris@0 339 }
Chris@0 340
Chris@0 341 /**
Chris@0 342 * Submits the form.
Chris@0 343 *
Chris@0 344 * Calling this method on anything else than form elements is not allowed.
Chris@0 345 */
Chris@0 346 public function submit()
Chris@0 347 {
Chris@0 348 $this->getDriver()->submitForm($this->getXpath());
Chris@0 349 }
Chris@0 350 }