annotate vendor/jcalderonzumba/mink-phantomjs-driver/src/PhantomJSDriver.php @ 19:fa3358dc1485 tip

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