FastSelectElement   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 60
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getOptions() 0 16 1
A clearSelectedOptions() 0 9 1
A getSelectedOptions() 0 19 1
1
<?php
2
3
namespace Magium\WebDriver;
4
5
class FastSelectElement
6
{
7
8
    protected $webDriver;
9
    protected $xpath;
10
11
    public function __construct(WebDriver $webDriver, $xpath)
12
    {
13
        $this->webDriver = $webDriver;
14
        $this->xpath = str_replace('"', '\"', $xpath);
15
    }
16
17
    public function getOptions()
18
    {
19
        $javascript = sprintf('
20
var select = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
21
var selectedOptions = [];
22
for (var i=0, n=select.options.length;i<n;i++) {
23
    selectedOptions.push({
24
        label: select.options[i].text,
25
        value: select.options[i].value
26
    });
27
}
28
return selectedOptions;
29
', $this->xpath);
30
        $result = $this->webDriver->executeScript($javascript);
31
        return $result;
32
    }
33
34
    public function clearSelectedOptions()
35
    {
36
        $javascript = sprintf('
37
var select = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
38
select.selectedIndex = -1;
39
', $this->xpath);
40
        $return = $this->webDriver->executeScript($javascript, []);
41
        return $return;
42
    }
43
44
    public function getSelectedOptions()
45
    {
46
47
        $javascript = sprintf('
48
var select = document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
49
var selectedOptions = [];
50
for (var i=0, n=select.options.length;i<n;i++) {
51
    if (select.options[i].selected)  {
52
        selectedOptions.push({
53
            label: select.options[i].text,
54
            value: select.options[i].value
55
        });
56
    }
57
}
58
return selectedOptions;
59
', $this->xpath);
60
        $result = $this->webDriver->executeScript($javascript);
61
        return $result;
62
    }
63
64
}