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