FastSelectElement::getOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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
}