WebDriver   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 196
Duplicated Lines 8.16 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 7
dl 16
loc 196
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getBrowser() 0 4 1
A getPlatform() 0 4 1
A setCommandExecutor() 0 9 1
A logFind() 0 18 2
A logElement() 0 18 2
A setRemoteExecuteMethod() 0 4 1
A setLogger() 0 4 1
A findElements() 9 9 2
A findElement() 7 7 1
A elementExists() 0 9 2
A elementAttached() 0 9 2
A elementDisplayed() 0 9 2
A byXpath() 0 4 1
A byId() 0 4 1
A byCssSelector() 0 4 1
A __destruct() 0 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Magium\WebDriver;
4
5
use Facebook\WebDriver\Exception\WebDriverException;
6
use Facebook\WebDriver\Remote\DriverCommand;
7
use Facebook\WebDriver\Remote\HttpCommandExecutor;
8
use Facebook\WebDriver\Remote\RemoteWebDriver;
9
use Facebook\WebDriver\Remote\WebDriverCommand;
10
use Facebook\WebDriver\WebDriverBy;
11
use Facebook\WebDriver\WebDriverCommandExecutor;
12
use Facebook\WebDriver\WebDriverElement;
13
use Magium\Util\Log\LoggerAware;
14
use Magium\Util\Log\LoggerInterface;
15
16
17
class WebDriver extends RemoteWebDriver implements LoggerAware
18
{
19
    const INSTRUCTION_MOUSE_MOVETO = 'mouseMoveTo';
20
    const INSTRUCTION_MOUSE_CLICK  = 'mouseClick';
21
22
    const BY_XPATH = 'byXpath';
23
    const BY_ID    = 'byId';
24
    const BY_CSS_SELECTOR = 'byCssSelector';
25
26
    /**
27
     * @var \Magium\Util\Log\LoggerInterface
28
     */
29
30
    protected $logger;
31
32
    protected $browser;
33
    protected $platform;
34
35
    /**
36
     * @return mixed
37
     */
38
    public function getBrowser()
39
    {
40
        return $this->browser;
41
    }
42
43
    /**
44
     * @return mixed
45
     */
46
    public function getPlatform()
47
    {
48
        return $this->platform;
49
    }
50
51
    public function setCommandExecutor(WebDriverCommandExecutor $executor)
52
    {
53
        $command = new WebDriverCommand($this->getSessionID(), DriverCommand::GET_CAPABILITIES, []);
54
        $result = $executor->execute($command);
55
        $values = $result->getValue();
56
        $this->browser = $values['browserName'];
57
        $this->platform = $values['platform'];
58
        return parent::setCommandExecutor($executor);
0 ignored issues
show
Deprecated Code introduced by
The method Facebook\WebDriver\Remot...r::setCommandExecutor() has been deprecated with message: To be removed in the future. Executor should be passed in the constructor.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59
    }
60
61
62
    public function logFind($by, $selector)
63
    {
64
        if ($this->logger instanceof \Magium\Util\Log\LoggerInterface) {
65
            $this->logger->debug(
66
                sprintf(
67
                    'By: %s %s',
68
                    $by,
69
                    $selector
70
                ),
71
                [
72
                    'type' => 'webdriver-activity',
73
                    'activity'  => 'select',
74
                    'by'        => $by,
75
                    'selector'  => $selector
76
                ]
77
            );
78
        }
79
    }
80
81
    public function logElement(WebDriverElement $element)
82
    {
83
        if ($this->logger instanceof LoggerInterface) {
84
            $this->logger->debug(
85
                sprintf(
86
                    'Returned element: %s ID: %s',
87
                    get_class($element),
88
                    $element->getID()
89
                ),
90
                [
91
                    'type'      => 'webdriver-activity',
92
                    'activity'  => 'element-return',
93
                    'class'      => get_class($element),
94
                    'id'        => $element->getID()
95
                ]
96
            );
97
        }
98
    }
99
100
    public function setRemoteExecuteMethod(LoggingRemoteExecuteMethod $method)
101
    {
102
        $this->executeMethod = $method;
103
    }
104
105
    public function setLogger(LoggerInterface $logger)
106
    {
107
        $this->logger = $logger;
108
    }
109
110
    /**
111
     * @param WebDriverBy $by
112
     * @return \Facebook\WebDriver\Remote\RemoteWebElement[]
113
     */
114
115 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...
116
    {
117
        $this->logFind($by->getMechanism(), $by->getValue());
118
        $elements = parent::findElements($by);
119
        foreach ($elements as $element) {
120
            $this->logElement($element);
121
        }
122
        return $elements;
123
    }
124
125
    /**
126
     * @param WebDriverBy $by
127
     * @return \Facebook\WebDriver\Remote\RemoteWebElement
128
     */
129
130 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...
131
    {
132
        $this->logFind($by->getMechanism(), $by->getValue());
133
        $element = parent::findElement($by);
134
        $this->logElement($element);
135
        return $element;
136
    }
137
138
    public function elementExists($selector, $by = 'byId')
139
    {
140
        try {
141
            $this->$by($selector);
142
            return true;
143
        } catch (\Exception $e) {
144
            return false;
145
        }
146
    }
147
148
    public function elementAttached(WebDriverElement $element)
149
    {
150
        try {
151
            $element->isDisplayed();
152
            return true;
153
        } catch (\Exception $e) {
154
            return false;
155
        }
156
    }
157
158
    public function elementDisplayed($selector, $by = 'byId')
159
    {
160
        try {
161
            $element = $this->$by($selector);
162
            return $element->isDisplayed();
163
        } catch (\Exception $e) {
164
            return false;
165
        }
166
    }
167
168
    /**
169
     * 
170
     * @param string $xpath
171
     * @return \Facebook\WebDriver\Remote\RemoteWebElement
172
     */
173
    
174
    public function byXpath($xpath)
175
    {
176
        return $this->findElement(WebDriverBy::xpath($xpath));
177
    }
178
    
179
    /**
180
     * 
181
     * @param string $id
182
     * @return \Facebook\WebDriver\Remote\RemoteWebElement
183
     */
184
    
185
    public function byId($id)
186
    {
187
        return $this->findElement(WebDriverBy::id($id));
188
    }
189
    
190
    /**
191
     * 
192
     * @param string $selector
193
     * @return \Facebook\WebDriver\Remote\RemoteWebElement
194
     */
195
    
196
    public function byCssSelector($selector)
197
    {
198
        return $this->findElement(WebDriverBy::cssSelector($selector));
199
    }
200
    
201
    public function __destruct()
202
    {
203
        try {
204
            if ($this->getCommandExecutor() instanceof HttpCommandExecutor) {
205
                $this->quit();
206
            }
207
        } catch (WebDriverException $e) {
208
            // Not a problem.  It just means that the WebDriver session was closed somewhere else
209
        }
210
    }
211
212
}
213