View source
<?php
namespace Zumba\Mink\Driver;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\DriverException;
class PhantomJSDriver extends BasePhantomJSDriver {
use SessionTrait;
use NavigationTrait;
use CookieTrait;
use HeadersTrait;
use JavascriptTrait;
use MouseTrait;
use PageContentTrait;
use KeyboardTrait;
use FormManipulationTrait;
use WindowTrait;
public function setBasicAuth($user, $password) {
$this->browser
->setHttpAuth($user, $password);
}
public function getTagName($xpath) {
$elements = $this
->findElement($xpath, 1);
return $this->browser
->tagName($elements["page_id"], $elements["ids"][0]);
}
public function getAttribute($xpath, $name) {
$elements = $this
->findElement($xpath, 1);
return $this->browser
->attribute($elements["page_id"], $elements["ids"][0], $name);
}
public function isVisible($xpath) {
$elements = $this
->findElement($xpath, 1);
return $this->browser
->isVisible($elements["page_id"], $elements["ids"][0]);
}
public function dragTo($sourceXpath, $destinationXpath) {
$sourceElement = $this
->findElement($sourceXpath, 1);
$destinationElement = $this
->findElement($destinationXpath, 1);
$this->browser
->drag($sourceElement["page_id"], $sourceElement["ids"][0], $destinationElement["ids"][0]);
}
public function attachFile($xpath, $path) {
if (!file_exists($path)) {
throw new DriverException("Wow there the file does not exist, you can not upload it");
}
if (($realPath = realpath($path)) === false) {
throw new DriverException("Wow there the file does not exist, you can not upload it");
}
$element = $this
->findElement($xpath, 1);
$tagName = $this
->getTagName($xpath);
if ($tagName != "input") {
throw new DriverException("The element is not an input element, you can not attach a file to it");
}
$attributes = $this
->getBrowser()
->attributes($element["page_id"], $element["ids"][0]);
if (!isset($attributes["type"]) || $attributes["type"] != "file") {
throw new DriverException("The element is not an input file type element, you can not attach a file to it");
}
$this->browser
->selectFile($element["page_id"], $element["ids"][0], $realPath);
}
public function switchToIFrame($name = null) {
if ($name === null) {
$this->browser
->popFrame();
return;
}
else {
$this->browser
->pushFrame($name);
}
}
public function focus($xpath) {
$element = $this
->findElement($xpath, 1);
$this->browser
->trigger($element["page_id"], $element["ids"][0], "focus");
}
public function blur($xpath) {
$element = $this
->findElement($xpath, 1);
$this->browser
->trigger($element["page_id"], $element["ids"][0], "blur");
}
public function find($xpath) {
$elements = $this->browser
->find("xpath", $xpath);
$nodeElements = array();
if (!isset($elements["ids"])) {
return null;
}
foreach ($elements["ids"] as $i => $elementId) {
$nodeElements[] = new NodeElement(sprintf('(%s)[%d]', $xpath, $i + 1), $this->session);
}
return $nodeElements;
}
}