annotate vendor/jcalderonzumba/mink-phantomjs-driver/src/FormManipulationTrait.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 namespace Zumba\Mink\Driver;
Chris@0 4
Chris@0 5 use Behat\Mink\Exception\DriverException;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Trait FormManipulationTrait
Chris@0 9 * @package Zumba\Mink\Driver
Chris@0 10 */
Chris@0 11 trait FormManipulationTrait {
Chris@0 12
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Returns the value of a given xpath element
Chris@0 16 * @param string $xpath
Chris@0 17 * @return string
Chris@0 18 * @throws DriverException
Chris@0 19 */
Chris@0 20 public function getValue($xpath) {
Chris@0 21 $this->findElement($xpath, 1);
Chris@0 22 $javascript = $this->javascriptTemplateRender("get_value.js.twig", array("xpath" => $xpath));
Chris@0 23 return $this->browser->evaluate($javascript);
Chris@0 24 }
Chris@0 25
Chris@0 26 /**
Chris@0 27 * @param string $xpath
Chris@0 28 * @param string $value
Chris@0 29 * @throws DriverException
Chris@0 30 */
Chris@0 31 public function setValue($xpath, $value) {
Chris@0 32 $this->findElement($xpath, 1);
Chris@0 33 //This stuff is BECAUSE the way the driver works for setting values when being checkboxes, radios, etc.
Chris@0 34 if (is_bool($value)) {
Chris@0 35 $value = $this->boolToString($value);
Chris@0 36 }
Chris@0 37
Chris@0 38 $javascript = $this->javascriptTemplateRender("set_value.js.twig", array("xpath" => $xpath, "value" => json_encode($value)));
Chris@0 39 $this->browser->evaluate($javascript);
Chris@0 40 }
Chris@0 41
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Submits a form given an xpath selector
Chris@0 45 * @param string $xpath
Chris@0 46 * @throws DriverException
Chris@0 47 */
Chris@0 48 public function submitForm($xpath) {
Chris@0 49 $element = $this->findElement($xpath, 1);
Chris@0 50 $tagName = $this->browser->tagName($element["page_id"], $element["ids"][0]);
Chris@0 51 if (strcmp(strtolower($tagName), "form") !== 0) {
Chris@0 52 throw new DriverException("Can not submit something that is not a form");
Chris@0 53 }
Chris@0 54 $this->browser->trigger($element["page_id"], $element["ids"][0], "submit");
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * Helper method needed for twig and javascript stuff
Chris@0 59 * @param $boolValue
Chris@0 60 * @return string
Chris@0 61 */
Chris@0 62 protected function boolToString($boolValue) {
Chris@0 63 if ($boolValue === true) {
Chris@0 64 return "1";
Chris@0 65 }
Chris@0 66 return "0";
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Selects an option
Chris@0 71 * @param string $xpath
Chris@0 72 * @param string $value
Chris@0 73 * @param bool $multiple
Chris@0 74 * @return bool
Chris@0 75 * @throws DriverException
Chris@0 76 */
Chris@0 77 public function selectOption($xpath, $value, $multiple = false) {
Chris@0 78 $element = $this->findElement($xpath, 1);
Chris@0 79 $tagName = strtolower($this->browser->tagName($element["page_id"], $element["ids"][0]));
Chris@0 80 $attributes = $this->browser->attributes($element["page_id"], $element["ids"][0]);
Chris@0 81
Chris@0 82 if (!in_array($tagName, array("input", "select"))) {
Chris@0 83 throw new DriverException(sprintf('Impossible to select an option on the element with XPath "%s" as it is not a select or radio input', $xpath));
Chris@0 84 }
Chris@0 85
Chris@0 86 if ($tagName === "input" && $attributes["type"] != "radio") {
Chris@0 87 throw new DriverException(sprintf('Impossible to select an option on the element with XPath "%s" as it is not a select or radio input', $xpath));
Chris@0 88 }
Chris@0 89
Chris@0 90 return $this->browser->selectOption($element["page_id"], $element["ids"][0], $value, $multiple);
Chris@0 91 }
Chris@0 92
Chris@0 93 /**
Chris@0 94 * Check control over an input element of radio or checkbox type
Chris@0 95 * @param $xpath
Chris@0 96 * @return bool
Chris@0 97 * @throws DriverException
Chris@0 98 */
Chris@0 99 protected function inputCheckableControl($xpath) {
Chris@0 100 $element = $this->findElement($xpath, 1);
Chris@0 101 $tagName = strtolower($this->browser->tagName($element["page_id"], $element["ids"][0]));
Chris@0 102 $attributes = $this->browser->attributes($element["page_id"], $element["ids"][0]);
Chris@0 103 if ($tagName != "input") {
Chris@0 104 throw new DriverException("Can not check when the element is not of the input type");
Chris@0 105 }
Chris@0 106 if (!in_array($attributes["type"], array("checkbox", "radio"))) {
Chris@0 107 throw new DriverException("Can not check when the element is not checkbox or radio");
Chris@0 108 }
Chris@0 109 return true;
Chris@0 110 }
Chris@0 111
Chris@0 112 /**
Chris@0 113 * We click on the checkbox or radio when possible and needed
Chris@0 114 * @param string $xpath
Chris@0 115 * @throws DriverException
Chris@0 116 */
Chris@0 117 public function check($xpath) {
Chris@0 118 $this->inputCheckableControl($xpath);
Chris@0 119 $javascript = $this->javascriptTemplateRender("check_element.js.twig", array("xpath" => $xpath, "check" => "true"));
Chris@0 120 $this->browser->evaluate($javascript);
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * We click on the checkbox or radio when possible and needed
Chris@0 125 * @param string $xpath
Chris@0 126 * @throws DriverException
Chris@0 127 */
Chris@0 128 public function uncheck($xpath) {
Chris@0 129 $this->inputCheckableControl($xpath);
Chris@0 130 $javascript = $this->javascriptTemplateRender("check_element.js.twig", array("xpath" => $xpath, "check" => "false"));
Chris@0 131 $this->browser->evaluate($javascript);
Chris@0 132 }
Chris@0 133
Chris@0 134 /**
Chris@0 135 * Checks if the radio or checkbox is checked
Chris@0 136 * @param string $xpath
Chris@0 137 * @return bool
Chris@0 138 * @throws DriverException
Chris@0 139 */
Chris@0 140 public function isChecked($xpath) {
Chris@0 141 $this->findElement($xpath, 1);
Chris@0 142 $javascript = $this->javascriptTemplateRender("is_checked.js.twig", array("xpath" => $xpath));
Chris@0 143 $checked = $this->browser->evaluate($javascript);
Chris@0 144
Chris@0 145 if ($checked === null) {
Chris@0 146 throw new DriverException("Can not check when the element is not checkbox or radio");
Chris@0 147 }
Chris@0 148
Chris@0 149 return $checked;
Chris@0 150 }
Chris@0 151
Chris@0 152 /**
Chris@0 153 * Checks if the option is selected or not
Chris@0 154 * @param string $xpath
Chris@0 155 * @return bool
Chris@0 156 * @throws DriverException
Chris@0 157 */
Chris@0 158 public function isSelected($xpath) {
Chris@0 159 $elements = $this->findElement($xpath, 1);
Chris@0 160 $javascript = $this->javascriptTemplateRender("is_selected.js.twig", array("xpath" => $xpath));
Chris@0 161 $tagName = $this->browser->tagName($elements["page_id"], $elements["ids"][0]);
Chris@0 162 if (strcmp(strtolower($tagName), "option") !== 0) {
Chris@0 163 throw new DriverException("Can not assert on element that is not an option");
Chris@0 164 }
Chris@0 165
Chris@0 166 return $this->browser->evaluate($javascript);
Chris@0 167 }
Chris@0 168 }