Completed
Pull Request — master (#50)
by Juan Francisco
03:55
created

PhantomJSDriver::findElementXpaths()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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