Chris@0: findElement($xpath, 1); Chris@0: $javascript = $this->javascriptTemplateRender("get_value.js.twig", array("xpath" => $xpath)); Chris@0: return $this->browser->evaluate($javascript); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $xpath Chris@0: * @param string $value Chris@0: * @throws DriverException Chris@0: */ Chris@0: public function setValue($xpath, $value) { Chris@0: $this->findElement($xpath, 1); Chris@0: //This stuff is BECAUSE the way the driver works for setting values when being checkboxes, radios, etc. Chris@0: if (is_bool($value)) { Chris@0: $value = $this->boolToString($value); Chris@0: } Chris@0: Chris@0: $javascript = $this->javascriptTemplateRender("set_value.js.twig", array("xpath" => $xpath, "value" => json_encode($value))); Chris@0: $this->browser->evaluate($javascript); Chris@0: } Chris@0: Chris@0: Chris@0: /** Chris@0: * Submits a form given an xpath selector Chris@0: * @param string $xpath Chris@0: * @throws DriverException Chris@0: */ Chris@0: public function submitForm($xpath) { Chris@0: $element = $this->findElement($xpath, 1); Chris@0: $tagName = $this->browser->tagName($element["page_id"], $element["ids"][0]); Chris@0: if (strcmp(strtolower($tagName), "form") !== 0) { Chris@0: throw new DriverException("Can not submit something that is not a form"); Chris@0: } Chris@0: $this->browser->trigger($element["page_id"], $element["ids"][0], "submit"); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Helper method needed for twig and javascript stuff Chris@0: * @param $boolValue Chris@0: * @return string Chris@0: */ Chris@0: protected function boolToString($boolValue) { Chris@0: if ($boolValue === true) { Chris@0: return "1"; Chris@0: } Chris@0: return "0"; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Selects an option Chris@0: * @param string $xpath Chris@0: * @param string $value Chris@0: * @param bool $multiple Chris@0: * @return bool Chris@0: * @throws DriverException Chris@0: */ Chris@0: public function selectOption($xpath, $value, $multiple = false) { Chris@0: $element = $this->findElement($xpath, 1); Chris@0: $tagName = strtolower($this->browser->tagName($element["page_id"], $element["ids"][0])); Chris@0: $attributes = $this->browser->attributes($element["page_id"], $element["ids"][0]); Chris@0: Chris@0: if (!in_array($tagName, array("input", "select"))) { Chris@0: 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: } Chris@0: Chris@0: if ($tagName === "input" && $attributes["type"] != "radio") { Chris@0: 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: } Chris@0: Chris@0: return $this->browser->selectOption($element["page_id"], $element["ids"][0], $value, $multiple); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check control over an input element of radio or checkbox type Chris@0: * @param $xpath Chris@0: * @return bool Chris@0: * @throws DriverException Chris@0: */ Chris@0: protected function inputCheckableControl($xpath) { Chris@0: $element = $this->findElement($xpath, 1); Chris@0: $tagName = strtolower($this->browser->tagName($element["page_id"], $element["ids"][0])); Chris@0: $attributes = $this->browser->attributes($element["page_id"], $element["ids"][0]); Chris@0: if ($tagName != "input") { Chris@0: throw new DriverException("Can not check when the element is not of the input type"); Chris@0: } Chris@0: if (!in_array($attributes["type"], array("checkbox", "radio"))) { Chris@0: throw new DriverException("Can not check when the element is not checkbox or radio"); Chris@0: } Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * We click on the checkbox or radio when possible and needed Chris@0: * @param string $xpath Chris@0: * @throws DriverException Chris@0: */ Chris@0: public function check($xpath) { Chris@0: $this->inputCheckableControl($xpath); Chris@0: $javascript = $this->javascriptTemplateRender("check_element.js.twig", array("xpath" => $xpath, "check" => "true")); Chris@0: $this->browser->evaluate($javascript); Chris@0: } Chris@0: Chris@0: /** Chris@0: * We click on the checkbox or radio when possible and needed Chris@0: * @param string $xpath Chris@0: * @throws DriverException Chris@0: */ Chris@0: public function uncheck($xpath) { Chris@0: $this->inputCheckableControl($xpath); Chris@0: $javascript = $this->javascriptTemplateRender("check_element.js.twig", array("xpath" => $xpath, "check" => "false")); Chris@0: $this->browser->evaluate($javascript); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if the radio or checkbox is checked Chris@0: * @param string $xpath Chris@0: * @return bool Chris@0: * @throws DriverException Chris@0: */ Chris@0: public function isChecked($xpath) { Chris@0: $this->findElement($xpath, 1); Chris@0: $javascript = $this->javascriptTemplateRender("is_checked.js.twig", array("xpath" => $xpath)); Chris@0: $checked = $this->browser->evaluate($javascript); Chris@0: Chris@0: if ($checked === null) { Chris@0: throw new DriverException("Can not check when the element is not checkbox or radio"); Chris@0: } Chris@0: Chris@0: return $checked; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if the option is selected or not Chris@0: * @param string $xpath Chris@0: * @return bool Chris@0: * @throws DriverException Chris@0: */ Chris@0: public function isSelected($xpath) { Chris@0: $elements = $this->findElement($xpath, 1); Chris@0: $javascript = $this->javascriptTemplateRender("is_selected.js.twig", array("xpath" => $xpath)); Chris@0: $tagName = $this->browser->tagName($elements["page_id"], $elements["ids"][0]); Chris@0: if (strcmp(strtolower($tagName), "option") !== 0) { Chris@0: throw new DriverException("Can not assert on element that is not an option"); Chris@0: } Chris@0: Chris@0: return $this->browser->evaluate($javascript); Chris@0: } Chris@0: }