|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Magium\Actions; |
|
5
|
|
|
|
|
6
|
|
|
use Facebook\WebDriver\WebDriverElement; |
|
7
|
|
|
use Facebook\WebDriver\WebDriverSelect; |
|
8
|
|
|
use Magium\NotFoundException; |
|
9
|
|
|
use Magium\WebDriver\WebDriver; |
|
10
|
|
|
|
|
11
|
|
|
class SetFormValue implements ActionInterface |
|
12
|
|
|
{ |
|
13
|
|
|
const ACTION = "SetFormValue"; |
|
14
|
|
|
|
|
15
|
|
|
protected $webDriver; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct( |
|
18
|
|
|
WebDriver $webDriver |
|
19
|
|
|
) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->webDriver = $webDriver; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function setByLabel($label, $value) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->set($label, $value); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function setById($id, $value) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->set('id:' . $id, $value); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function set($name, $value) |
|
35
|
|
|
{ |
|
36
|
|
|
$formElement = null; |
|
|
|
|
|
|
37
|
|
|
$pos = strpos($name, 'id:'); |
|
38
|
|
|
if ($pos === 0) { |
|
39
|
|
|
$formElement = $this->webDriver->byId(substr($name, 3)); |
|
40
|
|
|
} else { |
|
41
|
|
|
$element = $this->webDriver->byXpath(sprintf('//label[.="%s"]', $name)); |
|
42
|
|
|
if (!$element->getAttribute('for')) { |
|
|
|
|
|
|
43
|
|
|
throw new NotFoundException('Unable to find the "for" attribute for a label with the text: ' . $name); |
|
44
|
|
|
} |
|
45
|
|
|
$formElement = $this->webDriver->byId($element->getAttribute('for')); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$type = strtolower($formElement->getTagName()); |
|
49
|
|
|
switch ($type) { |
|
50
|
|
|
case 'select': |
|
51
|
|
|
$this->setSelect($formElement, $value); |
|
52
|
|
|
break; |
|
53
|
|
|
default: |
|
54
|
|
|
$type = strtolower($formElement->getAttribute('type')); |
|
55
|
|
|
switch ($type) { |
|
56
|
|
|
case 'radio': |
|
57
|
|
|
$this->setClicked($formElement, $value); |
|
58
|
|
|
break; |
|
59
|
|
|
case 'checkbox': |
|
60
|
|
|
$this->setClicked($formElement, $value); |
|
61
|
|
|
break; |
|
62
|
|
|
default: |
|
63
|
|
|
$this->setText($formElement, $value); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
protected function setText(WebDriverElement $element, $value) |
|
70
|
|
|
{ |
|
71
|
|
|
$element->clear(); |
|
72
|
|
|
$element->sendKeys($value); |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function setClicked(WebDriverElement $element, $value) |
|
77
|
|
|
{ |
|
78
|
|
|
$checked = (bool)$element->getAttribute('checked'); |
|
79
|
|
|
if ((bool)$checked != (bool)$value) { |
|
80
|
|
|
$element->click(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function setSelect(WebDriverElement $element, $value) |
|
85
|
|
|
{ |
|
86
|
|
|
$select = new WebDriverSelect($element); |
|
87
|
|
|
if ($select->isMultiple()) { |
|
88
|
|
|
$select->deselectAll(); |
|
89
|
|
|
} |
|
90
|
|
|
if (!is_array($value)) { |
|
91
|
|
|
$value = [$value]; |
|
92
|
|
|
} |
|
93
|
|
|
foreach ($value as $v) { |
|
94
|
|
|
if (strpos($v, 'value:') === 0) { |
|
95
|
|
|
$select->selectByValue(substr($value, 6)); |
|
96
|
|
|
} else { |
|
97
|
|
|
$select->selectByVisibleText($v); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function execute($name, $value) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->set($name, $value); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.