annotate vendor/jcalderonzumba/mink-phantomjs-driver/src/PhantomJSDriver.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Zumba\Mink\Driver;
Chris@0 4
Chris@0 5 use Behat\Mink\Element\NodeElement;
Chris@0 6 use Behat\Mink\Exception\DriverException;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Class PhantomJSDriver
Chris@0 10 * @package Behat\Mink\Driver
Chris@0 11 */
Chris@0 12 class PhantomJSDriver extends BasePhantomJSDriver {
Chris@0 13
Chris@0 14 use SessionTrait;
Chris@0 15 use NavigationTrait;
Chris@0 16 use CookieTrait;
Chris@0 17 use HeadersTrait;
Chris@0 18 use JavascriptTrait;
Chris@0 19 use MouseTrait;
Chris@0 20 use PageContentTrait;
Chris@0 21 use KeyboardTrait;
Chris@0 22 use FormManipulationTrait;
Chris@0 23 use WindowTrait;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Sets the basic auth user and password
Chris@0 27 * @param string $user
Chris@0 28 * @param string $password
Chris@0 29 */
Chris@0 30 public function setBasicAuth($user, $password) {
Chris@0 31 $this->browser->setHttpAuth($user, $password);
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Gets the tag name of a given xpath
Chris@0 36 * @param string $xpath
Chris@0 37 * @return string
Chris@0 38 * @throws DriverException
Chris@0 39 */
Chris@0 40 public function getTagName($xpath) {
Chris@0 41 $elements = $this->findElement($xpath, 1);
Chris@0 42 return $this->browser->tagName($elements["page_id"], $elements["ids"][0]);
Chris@0 43 }
Chris@0 44
Chris@0 45 /**
Chris@0 46 * Gets the attribute value of a given element and name
Chris@0 47 * @param string $xpath
Chris@0 48 * @param string $name
Chris@0 49 * @return string
Chris@0 50 * @throws DriverException
Chris@0 51 */
Chris@0 52 public function getAttribute($xpath, $name) {
Chris@0 53 $elements = $this->findElement($xpath, 1);
Chris@0 54 return $this->browser->attribute($elements["page_id"], $elements["ids"][0], $name);
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * Check if element given by xpath is visible or not
Chris@0 59 * @param string $xpath
Chris@0 60 * @return bool
Chris@0 61 * @throws DriverException
Chris@0 62 */
Chris@0 63 public function isVisible($xpath) {
Chris@0 64 $elements = $this->findElement($xpath, 1);
Chris@0 65 return $this->browser->isVisible($elements["page_id"], $elements["ids"][0]);
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * Drags one element to another
Chris@0 70 * @param string $sourceXpath
Chris@0 71 * @param string $destinationXpath
Chris@0 72 * @throws DriverException
Chris@0 73 */
Chris@0 74 public function dragTo($sourceXpath, $destinationXpath) {
Chris@0 75 $sourceElement = $this->findElement($sourceXpath, 1);
Chris@0 76 $destinationElement = $this->findElement($destinationXpath, 1);
Chris@0 77 $this->browser->drag($sourceElement["page_id"], $sourceElement["ids"][0], $destinationElement["ids"][0]);
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * Upload a file to the browser
Chris@0 82 * @param string $xpath
Chris@0 83 * @param string $path
Chris@0 84 * @throws DriverException
Chris@0 85 */
Chris@0 86 public function attachFile($xpath, $path) {
Chris@0 87 if (!file_exists($path)) {
Chris@0 88 throw new DriverException("Wow there the file does not exist, you can not upload it");
Chris@0 89 }
Chris@0 90
Chris@0 91 if (($realPath = realpath($path)) === false) {
Chris@0 92 throw new DriverException("Wow there the file does not exist, you can not upload it");
Chris@0 93 }
Chris@0 94
Chris@0 95 $element = $this->findElement($xpath, 1);
Chris@0 96 $tagName = $this->getTagName($xpath);
Chris@0 97 if ($tagName != "input") {
Chris@0 98 throw new DriverException("The element is not an input element, you can not attach a file to it");
Chris@0 99 }
Chris@0 100
Chris@0 101 $attributes = $this->getBrowser()->attributes($element["page_id"], $element["ids"][0]);
Chris@0 102 if (!isset($attributes["type"]) || $attributes["type"] != "file") {
Chris@0 103 throw new DriverException("The element is not an input file type element, you can not attach a file to it");
Chris@0 104 }
Chris@0 105
Chris@0 106 $this->browser->selectFile($element["page_id"], $element["ids"][0], $realPath);
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * Puts the browser control inside the IFRAME
Chris@0 111 * You own the control, make sure to go back to the parent calling this method with null
Chris@0 112 * @param string $name
Chris@0 113 */
Chris@0 114 public function switchToIFrame($name = null) {
Chris@0 115 //TODO: check response of the calls
Chris@0 116 if ($name === null) {
Chris@0 117 $this->browser->popFrame();
Chris@0 118 return;
Chris@0 119 } else {
Chris@0 120 $this->browser->pushFrame($name);
Chris@0 121 }
Chris@0 122 }
Chris@0 123
Chris@0 124 /**
Chris@0 125 * Focus on an element
Chris@0 126 * @param string $xpath
Chris@0 127 * @throws DriverException
Chris@0 128 */
Chris@0 129 public function focus($xpath) {
Chris@0 130 $element = $this->findElement($xpath, 1);
Chris@0 131 $this->browser->trigger($element["page_id"], $element["ids"][0], "focus");
Chris@0 132 }
Chris@0 133
Chris@0 134 /**
Chris@0 135 * Blur on element
Chris@0 136 * @param string $xpath
Chris@0 137 * @throws DriverException
Chris@0 138 */
Chris@0 139 public function blur($xpath) {
Chris@0 140 $element = $this->findElement($xpath, 1);
Chris@0 141 $this->browser->trigger($element["page_id"], $element["ids"][0], "blur");
Chris@0 142 }
Chris@0 143
Chris@0 144 /**
Chris@0 145 * Finds elements with specified XPath query.
Chris@0 146 * @param string $xpath
Chris@0 147 * @return NodeElement[]
Chris@0 148 * @throws DriverException When the operation cannot be done
Chris@0 149 */
Chris@0 150 public function find($xpath) {
Chris@0 151 $elements = $this->browser->find("xpath", $xpath);
Chris@0 152 $nodeElements = array();
Chris@0 153
Chris@0 154 if (!isset($elements["ids"])) {
Chris@0 155 return null;
Chris@0 156 }
Chris@0 157
Chris@0 158 foreach ($elements["ids"] as $i => $elementId) {
Chris@0 159 $nodeElements[] = new NodeElement(sprintf('(%s)[%d]', $xpath, $i + 1), $this->session);
Chris@0 160 }
Chris@0 161 return $nodeElements;
Chris@0 162 }
Chris@0 163
Chris@0 164 }