Completed
Branch master (a7a328)
by Kevin
04:49 queued 02:08
created

WebDriver::elementClickable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
nc 3
cc 3
eloc 5
nop 1
1
<?php
2
3
namespace Magium\WebDriver;
4
5
use Facebook\WebDriver\Exception\WebDriverException;
6
use Facebook\WebDriver\Remote\HttpCommandExecutor;
7
use Facebook\WebDriver\Remote\RemoteExecuteMethod;
8
use Facebook\WebDriver\Remote\RemoteWebDriver;
9
use Facebook\WebDriver\WebDriverBy;
10
use Facebook\WebDriver\WebDriverElement;
11
use Magium\Util\Log\Logger;
12
use Magium\Util\Log\LoggerAware;
13
14
15
class WebDriver extends RemoteWebDriver implements LoggerAware
16
{
17
    const INSTRUCTION_MOUSE_MOVETO = 'mouseMoveTo';
18
    const INSTRUCTION_MOUSE_CLICK  = 'mouseClick';
19
20
    const BY_XPATH = 'byXpath';
21
    const BY_ID    = 'byId';
22
    const BY_CSS_SELECTOR = 'byCssSelector';
23
24
    /**
25
     * @var Logger
26
     */
27
28
    protected $logger;
29
30
31
    public function logFind($by, $selector)
32
    {
33
        if ($this->logger instanceof Logger) {
34
            $this->logger->debug(
35
                sprintf(
36
                    'By: %s %s',
37
                    $by,
38
                    $selector
39
                ),
40
                [
41
                    'type' => 'webdriver-activity',
42
                    'activity'  => 'select',
43
                    'by'        => $by,
44
                    'selector'  => $selector
45
                ]
46
            );
47
        }
48
    }
49
50
    public function logElement(WebDriverElement $element)
51
    {
52
        if ($this->logger instanceof Logger) {
53
            $this->logger->debug(
54
                sprintf(
55
                    'Returned element: %s ID: %s',
56
                    get_class($element),
57
                    $element->getID()
58
                ),
59
                [
60
                    'type'      => 'webdriver-activity',
61
                    'activity'  => 'element-return',
62
                    'type'      => get_class($element),
63
                    'id'        => $element->getID()
64
                ]
65
            );
66
        }
67
    }
68
69
    public function setRemoteExecuteMethod(LoggingRemoteExecuteMethod $method)
70
    {
71
        $this->executeMethod = $method;
72
    }
73
74
    public function setLogger(Logger $logger)
75
    {
76
        $this->logger = $logger;
77
    }
78
79 View Code Duplication
    public function findElements(WebDriverBy $by)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        $this->logFind($by->getMechanism(), $by->getValue());
82
        $elements = parent::findElements($by);
83
        foreach ($elements as $element) {
84
            $this->logElement($element);
85
        }
86
        return $elements;
87
    }
88
89 View Code Duplication
    public function findElement(WebDriverBy $by)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $this->logFind($by->getMechanism(), $by->getValue());
92
        $element = parent::findElement($by);
93
        $this->logElement($element);
94
        return $element;
95
    }
96
97
    public function elementExists($selector, $by = 'byId')
98
    {
99
        try {
100
            $this->$by($selector);
101
            return true;
102
        } catch (\Exception $e) {
103
            return false;
104
        }
105
    }
106
107
    public function elementAttached(WebDriverElement $element)
108
    {
109
        try {
110
            $element->isDisplayed();
111
            return true;
112
        } catch (\Exception $e) {
113
            return false;
114
        }
115
    }
116
117
    public function elementDisplayed($selector, $by = 'byId')
118
    {
119
        try {
120
            $element = $this->$by($selector);
121
            return $element->isDisplayed();
122
        } catch (\Exception $e) {
123
            return false;
124
        }
125
    }
126
127
    /**
128
     * 
129
     * @param string $xpath
130
     * @return \Facebook\WebDriver\Remote\RemoteWebElement
131
     */
132
    
133
    public function byXpath($xpath)
134
    {
135
        return $this->findElement(WebDriverBy::xpath($xpath));
136
    }
137
    
138
    /**
139
     * 
140
     * @param string $id
141
     * @return \Facebook\WebDriver\Remote\RemoteWebElement
142
     */
143
    
144
    public function byId($id)
145
    {
146
        return $this->findElement(WebDriverBy::id($id));
147
    }
148
    
149
    /**
150
     * 
151
     * @param string $selector
152
     * @return \Facebook\WebDriver\Remote\RemoteWebElement
153
     */
154
    
155
    public function byCssSelector($selector)
156
    {
157
        return $this->findElement(WebDriverBy::cssSelector($selector));
158
    }
159
    
160
    public function __destruct()
161
    {
162
        try {
163
            if ($this->getCommandExecutor() instanceof HttpCommandExecutor) {
164
                $this->quit();
165
            }
166
        } catch (WebDriverException $e) {
167
            // Not a problem.  It just means that the WebDriver session was closed somewhere else
168
        }
169
    }
170
171
}