View source
<?php
namespace Zumba\Mink\Driver;
use Behat\Mink\Exception\DriverException;
trait FormManipulationTrait {
public function getValue($xpath) {
$this
->findElement($xpath, 1);
$javascript = $this
->javascriptTemplateRender("get_value.js.twig", array(
"xpath" => $xpath,
));
return $this->browser
->evaluate($javascript);
}
public function setValue($xpath, $value) {
$this
->findElement($xpath, 1);
if (is_bool($value)) {
$value = $this
->boolToString($value);
}
$javascript = $this
->javascriptTemplateRender("set_value.js.twig", array(
"xpath" => $xpath,
"value" => json_encode($value),
));
$this->browser
->evaluate($javascript);
}
public function submitForm($xpath) {
$element = $this
->findElement($xpath, 1);
$tagName = $this->browser
->tagName($element["page_id"], $element["ids"][0]);
if (strcmp(strtolower($tagName), "form") !== 0) {
throw new DriverException("Can not submit something that is not a form");
}
$this->browser
->trigger($element["page_id"], $element["ids"][0], "submit");
}
protected function boolToString($boolValue) {
if ($boolValue === true) {
return "1";
}
return "0";
}
public function selectOption($xpath, $value, $multiple = false) {
$element = $this
->findElement($xpath, 1);
$tagName = strtolower($this->browser
->tagName($element["page_id"], $element["ids"][0]));
$attributes = $this->browser
->attributes($element["page_id"], $element["ids"][0]);
if (!in_array($tagName, array(
"input",
"select",
))) {
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));
}
if ($tagName === "input" && $attributes["type"] != "radio") {
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));
}
return $this->browser
->selectOption($element["page_id"], $element["ids"][0], $value, $multiple);
}
protected function inputCheckableControl($xpath) {
$element = $this
->findElement($xpath, 1);
$tagName = strtolower($this->browser
->tagName($element["page_id"], $element["ids"][0]));
$attributes = $this->browser
->attributes($element["page_id"], $element["ids"][0]);
if ($tagName != "input") {
throw new DriverException("Can not check when the element is not of the input type");
}
if (!in_array($attributes["type"], array(
"checkbox",
"radio",
))) {
throw new DriverException("Can not check when the element is not checkbox or radio");
}
return true;
}
public function check($xpath) {
$this
->inputCheckableControl($xpath);
$javascript = $this
->javascriptTemplateRender("check_element.js.twig", array(
"xpath" => $xpath,
"check" => "true",
));
$this->browser
->evaluate($javascript);
}
public function uncheck($xpath) {
$this
->inputCheckableControl($xpath);
$javascript = $this
->javascriptTemplateRender("check_element.js.twig", array(
"xpath" => $xpath,
"check" => "false",
));
$this->browser
->evaluate($javascript);
}
public function isChecked($xpath) {
$this
->findElement($xpath, 1);
$javascript = $this
->javascriptTemplateRender("is_checked.js.twig", array(
"xpath" => $xpath,
));
$checked = $this->browser
->evaluate($javascript);
if ($checked === null) {
throw new DriverException("Can not check when the element is not checkbox or radio");
}
return $checked;
}
public function isSelected($xpath) {
$elements = $this
->findElement($xpath, 1);
$javascript = $this
->javascriptTemplateRender("is_selected.js.twig", array(
"xpath" => $xpath,
));
$tagName = $this->browser
->tagName($elements["page_id"], $elements["ids"][0]);
if (strcmp(strtolower($tagName), "option") !== 0) {
throw new DriverException("Can not assert on element that is not an option");
}
return $this->browser
->evaluate($javascript);
}
}