3 namespace Zumba\Mink\Driver;
5 use Behat\Mink\Exception\DriverException;
8 * Class PhantomJSDriver
9 * @package Behat\Mink\Driver
11 class PhantomJSDriver extends BasePhantomJSDriver {
21 use FormManipulationTrait;
25 * Sets the basic auth user and password
27 * @param string $password
29 public function setBasicAuth($user, $password) {
30 $this->browser->setHttpAuth($user, $password);
34 * Gets the tag name of a given xpath
35 * @param string $xpath
37 * @throws DriverException
39 public function getTagName($xpath) {
40 $elements = $this->findElement($xpath, 1);
41 return $this->browser->tagName($elements["page_id"], $elements["ids"][0]);
45 * Gets the attribute value of a given element and name
46 * @param string $xpath
49 * @throws DriverException
51 public function getAttribute($xpath, $name) {
52 $elements = $this->findElement($xpath, 1);
53 return $this->browser->attribute($elements["page_id"], $elements["ids"][0], $name);
57 * Check if element given by xpath is visible or not
58 * @param string $xpath
60 * @throws DriverException
62 public function isVisible($xpath) {
63 $elements = $this->findElement($xpath, 1);
64 return $this->browser->isVisible($elements["page_id"], $elements["ids"][0]);
68 * Drags one element to another
69 * @param string $sourceXpath
70 * @param string $destinationXpath
71 * @throws DriverException
73 public function dragTo($sourceXpath, $destinationXpath) {
74 $sourceElement = $this->findElement($sourceXpath, 1);
75 $destinationElement = $this->findElement($destinationXpath, 1);
76 $this->browser->drag($sourceElement["page_id"], $sourceElement["ids"][0], $destinationElement["ids"][0]);
80 * Upload a file to the browser
81 * @param string $xpath
83 * @throws DriverException
85 public function attachFile($xpath, $path) {
86 if (!file_exists($path)) {
87 throw new DriverException("Wow there the file does not exist, you can not upload it");
90 if (($realPath = realpath($path)) === false) {
91 throw new DriverException("Wow there the file does not exist, you can not upload it");
94 $element = $this->findElement($xpath, 1);
95 $tagName = $this->getTagName($xpath);
96 if ($tagName != "input") {
97 throw new DriverException("The element is not an input element, you can not attach a file to it");
100 $attributes = $this->getBrowser()->attributes($element["page_id"], $element["ids"][0]);
101 if (!isset($attributes["type"]) || $attributes["type"] != "file") {
102 throw new DriverException("The element is not an input file type element, you can not attach a file to it");
105 $this->browser->selectFile($element["page_id"], $element["ids"][0], $realPath);
109 * Puts the browser control inside the IFRAME
110 * You own the control, make sure to go back to the parent calling this method with null
111 * @param string $name
113 public function switchToIFrame($name = null) {
114 //TODO: check response of the calls
115 if ($name === null) {
116 $this->browser->popFrame();
119 $this->browser->pushFrame($name);
124 * Focus on an element
125 * @param string $xpath
126 * @throws DriverException
128 public function focus($xpath) {
129 $element = $this->findElement($xpath, 1);
130 $this->browser->trigger($element["page_id"], $element["ids"][0], "focus");
135 * @param string $xpath
136 * @throws DriverException
138 public function blur($xpath) {
139 $element = $this->findElement($xpath, 1);
140 $this->browser->trigger($element["page_id"], $element["ids"][0], "blur");
144 * Finds elements with specified XPath query.
145 * @param string $xpath
147 * @throws DriverException When the operation cannot be done
149 protected function findElementXpaths($xpath) {
150 $elements = $this->browser->find("xpath", $xpath);
151 $nodeXPaths = array();
153 if (!isset($elements["ids"])) {
157 foreach ($elements["ids"] as $i => $elementId) {
158 $nodeXPaths[] = sprintf('(%s)[%d]', $xpath, $i + 1);