3 namespace Zumba\Mink\Driver;
5 use Behat\Mink\Exception\DriverException;
8 * Trait FormManipulationTrait
9 * @package Zumba\Mink\Driver
11 trait FormManipulationTrait {
15 * Returns the value of a given xpath element
16 * @param string $xpath
18 * @throws DriverException
20 public function getValue($xpath) {
21 $this->findElement($xpath, 1);
22 $javascript = $this->javascriptTemplateRender("get_value.js.twig", array("xpath" => $xpath));
23 return $this->browser->evaluate($javascript);
27 * @param string $xpath
28 * @param string $value
29 * @throws DriverException
31 public function setValue($xpath, $value) {
32 $this->findElement($xpath, 1);
33 //This stuff is BECAUSE the way the driver works for setting values when being checkboxes, radios, etc.
34 if (is_bool($value)) {
35 $value = $this->boolToString($value);
38 $javascript = $this->javascriptTemplateRender("set_value.js.twig", array("xpath" => $xpath, "value" => json_encode($value)));
39 $this->browser->evaluate($javascript);
44 * Submits a form given an xpath selector
45 * @param string $xpath
46 * @throws DriverException
48 public function submitForm($xpath) {
49 $element = $this->findElement($xpath, 1);
50 $tagName = $this->browser->tagName($element["page_id"], $element["ids"][0]);
51 if (strcmp(strtolower($tagName), "form") !== 0) {
52 throw new DriverException("Can not submit something that is not a form");
54 $this->browser->trigger($element["page_id"], $element["ids"][0], "submit");
58 * Helper method needed for twig and javascript stuff
62 protected function boolToString($boolValue) {
63 if ($boolValue === true) {
71 * @param string $xpath
72 * @param string $value
73 * @param bool $multiple
75 * @throws DriverException
77 public function selectOption($xpath, $value, $multiple = false) {
78 $element = $this->findElement($xpath, 1);
79 $tagName = strtolower($this->browser->tagName($element["page_id"], $element["ids"][0]));
80 $attributes = $this->browser->attributes($element["page_id"], $element["ids"][0]);
82 if (!in_array($tagName, array("input", "select"))) {
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));
86 if ($tagName === "input" && $attributes["type"] != "radio") {
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));
90 return $this->browser->selectOption($element["page_id"], $element["ids"][0], $value, $multiple);
94 * Check control over an input element of radio or checkbox type
97 * @throws DriverException
99 protected function inputCheckableControl($xpath) {
100 $element = $this->findElement($xpath, 1);
101 $tagName = strtolower($this->browser->tagName($element["page_id"], $element["ids"][0]));
102 $attributes = $this->browser->attributes($element["page_id"], $element["ids"][0]);
103 if ($tagName != "input") {
104 throw new DriverException("Can not check when the element is not of the input type");
106 if (!in_array($attributes["type"], array("checkbox", "radio"))) {
107 throw new DriverException("Can not check when the element is not checkbox or radio");
113 * We click on the checkbox or radio when possible and needed
114 * @param string $xpath
115 * @throws DriverException
117 public function check($xpath) {
118 $this->inputCheckableControl($xpath);
119 $javascript = $this->javascriptTemplateRender("check_element.js.twig", array("xpath" => $xpath, "check" => "true"));
120 $this->browser->evaluate($javascript);
124 * We click on the checkbox or radio when possible and needed
125 * @param string $xpath
126 * @throws DriverException
128 public function uncheck($xpath) {
129 $this->inputCheckableControl($xpath);
130 $javascript = $this->javascriptTemplateRender("check_element.js.twig", array("xpath" => $xpath, "check" => "false"));
131 $this->browser->evaluate($javascript);
135 * Checks if the radio or checkbox is checked
136 * @param string $xpath
138 * @throws DriverException
140 public function isChecked($xpath) {
141 $this->findElement($xpath, 1);
142 $javascript = $this->javascriptTemplateRender("is_checked.js.twig", array("xpath" => $xpath));
143 $checked = $this->browser->evaluate($javascript);
145 if ($checked === null) {
146 throw new DriverException("Can not check when the element is not checkbox or radio");
153 * Checks if the option is selected or not
154 * @param string $xpath
156 * @throws DriverException
158 public function isSelected($xpath) {
159 $elements = $this->findElement($xpath, 1);
160 $javascript = $this->javascriptTemplateRender("is_selected.js.twig", array("xpath" => $xpath));
161 $tagName = $this->browser->tagName($elements["page_id"], $elements["ids"][0]);
162 if (strcmp(strtolower($tagName), "option") !== 0) {
163 throw new DriverException("Can not assert on element that is not an option");
166 return $this->browser->evaluate($javascript);