PhantomJSDriver::dragTo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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