1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Toni Kolev, <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
namespace kolev\MultilingualExtension\Context; |
6
|
|
|
use Behat\MinkExtension\Context\RawMinkContext; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class RawMultilingualContext. |
10
|
|
|
* |
11
|
|
|
* @package kolev\MultilingualExtension\Context |
12
|
|
|
*/ |
13
|
|
|
class RawMultilingualContext extends RawMinkContext implements MultilingualContextInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Parameters of MultilingualExtension. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
public $multilingual_parameters = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function setMultilingualParameters(array $parameters) |
27
|
|
|
{ |
28
|
|
|
if (empty($this->multilingual_parameters)) { |
29
|
|
|
$this->multilingual_parameters = $parameters; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name |
35
|
|
|
* The name of parameter from behat.yml. |
36
|
|
|
* |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
protected function getMultilingualParameter($name) |
40
|
|
|
{ |
41
|
|
|
return isset($this->multilingual_parameters[$name]) ? $this->multilingual_parameters[$name] : false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* RAW definitions of simple functions to be used in MultilingualContext |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
View Code Duplication |
public function iClickOnTheText($text) { |
|
|
|
|
49
|
|
|
|
50
|
|
|
$session = $this->getSession(); |
51
|
|
|
$element = $session->getPage()->find( |
52
|
|
|
'xpath', |
53
|
|
|
$session->getSelectorsHandler()->selectorToXpath('xpath', '//*[contains(text(),"' . $text . '")]')); |
54
|
|
|
|
55
|
|
|
if (null === $element) { |
56
|
|
|
throw new \InvalidArgumentException(sprintf('Cannot find text: "%s"', $text)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$element->click(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function assertValueInInput($value, $input) { |
63
|
|
|
|
64
|
|
|
if (substr($input,0,1) != "#") { |
65
|
|
|
$input = "#" . $input; |
66
|
|
|
} |
67
|
|
|
$session = $this->getSession(); |
68
|
|
|
$element = $session->getPage()->find('css', $input); |
69
|
|
|
$text = ""; |
|
|
|
|
70
|
|
|
|
71
|
|
|
if(isset($element)) { |
72
|
|
|
$text = $element->getValue(); |
73
|
|
|
} |
74
|
|
|
else { |
75
|
|
|
throw new \Exception(sprintf("Element is null")); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if($text === $value) { |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
else { |
82
|
|
|
throw new \Exception(sprintf('Value of input : "%s" does not match the text "%s"', $text, $value)); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function iWaitForTextToAppearWithMaxTime($text, $maxExecutionTime) { |
87
|
|
|
|
88
|
|
|
$isTextFound = false; |
89
|
|
|
|
90
|
|
|
for ($i = 0; $i < $maxExecutionTime; $i++) { |
91
|
|
|
try { |
92
|
|
|
$this->iShouldSeeInTheSourceOfThePage($text); |
93
|
|
|
$isTextFound = true; |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
catch (\Exception $e) { |
97
|
|
|
sleep(1); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!$isTextFound) { |
102
|
|
|
throw new \Exception("'$text' didn't appear on the page for $maxExecutionTime seconds"); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function iShouldSeeInTheSourceOfThePage($text) { |
107
|
|
|
|
108
|
|
|
$html = $this->getSession()->getDriver()->getContent(); |
109
|
|
|
$text = $this->validateTextForSearchInSource($text); |
110
|
|
|
$regex = '/' . $text . '/'; |
111
|
|
|
|
112
|
|
|
preg_match($regex, $html, $results); |
113
|
|
|
|
114
|
|
|
if ($results == null) { |
115
|
|
|
throw new \Exception('The searched text ' . $text . ' was not found in the source of the page.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function validateTextForSearchInSource($text) { |
122
|
|
|
|
123
|
|
|
$text = preg_replace("/(\\.)/", '\\\\.', $text); |
124
|
|
|
$text = preg_replace("/(\/)/", '\\/', $text); |
125
|
|
|
$text = preg_replace("/(\\?)/", '\\\\\?', $text); |
126
|
|
|
|
127
|
|
|
return $text; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
public function iClickOnTheTextInRegion($text, $region) { |
|
|
|
|
131
|
|
|
|
132
|
|
|
$session = $this->getSession(); |
133
|
|
|
$element = $session->getPage()->find('region', $region)->find('xpath', $session->getSelectorsHandler()->selectorToXpath('xpath', |
134
|
|
|
'//*[contains(text(),"' . $text . '")]')); |
135
|
|
|
|
136
|
|
|
if (null === $element) { |
137
|
|
|
throw new \InvalidArgumentException(sprintf('Cannot find text: "%s"', $text)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$element->click(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
View Code Duplication |
public function selectOptionWithJavascript($text, $region) { |
|
|
|
|
144
|
|
|
|
145
|
|
|
$session = $this->getSession(); |
146
|
|
|
$element = $session->getPage()->find('region', $region)->find('xpath', $session->getSelectorsHandler()->selectorToXpath('xpath', |
147
|
|
|
'//*[contains(text(),"' . $text . '")]')); |
148
|
|
|
|
149
|
|
|
if (null === $element) { |
150
|
|
|
throw new \InvalidArgumentException(sprintf('Cannot find text: "%s"', $text)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$element->click(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function assertSelectRadioById($label, $id = '') { |
157
|
|
|
$element = $this->getSession()->getPage(); |
158
|
|
|
$radiobutton = $id ? $element->findById($id) : $element->find('named', array('radio', $this->getSession()->getSelectorsHandler()->xpathLiteral($label))); |
|
|
|
|
159
|
|
|
if ($radiobutton === NULL) { |
160
|
|
|
throw new \Exception(sprintf('The radio button with "%s" was not found on the page %s', $id ? $id : $label, $this->getSession()->getCurrentUrl())); |
161
|
|
|
} |
162
|
|
|
$value = $radiobutton->getAttribute('value'); |
163
|
|
|
$labelonpage = $radiobutton->getParent()->getText(); |
164
|
|
|
if ($label != $labelonpage) { |
165
|
|
|
throw new \Exception(sprintf("Button with id '%s' has label '%s' instead of '%s' on the page %s", $id, $labelonpage, $label, $this->getSession()->getCurrentUrl())); |
166
|
|
|
} |
167
|
|
|
$radiobutton->selectOption($value, FALSE); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
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.