This repository was archived by the owner on Mar 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCodeceptionUtilities.php
More file actions
218 lines (198 loc) · 6.63 KB
/
Copy pathCodeceptionUtilities.php
File metadata and controls
218 lines (198 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace Codeception\Module;
use Codeception\Module;
use Codeception\Util\Locator;
use Ixis\Codeception\Util\BrowserTrait;
class CodeceptionUtilities extends Module
{
use BrowserTrait;
/**
* Looks for a link in a particular CSS or XPath selector.
*
* @param string $text
* Text to look for.
* @param string $link
* URL the text should link to.
* @param string $cssOrXpath
* The selector in which to look for the link.
*/
public function seeLinkInSelector($text, $link, $cssOrXpath)
{
$this->getBrowserModule()->see($text, $this->getLinkSelector($link, $cssOrXpath));
}
/**
* Checks that a link does not appear in a particular CSS or XPath selector.
*
* @param string $text
* Text to ensure doesn't exist.
* @param string $link
* URL the text should not link to.
* @param string $cssOrXpath
* The selector in which to look for the lack of link.
*/
public function dontSeeLinkInSelector($text, $link, $cssOrXpath)
{
$this->getBrowserModule()->dontSee($text, $this->getLinkSelector($link, $cssOrXpath));
}
/**
* Helper method to calculate the correct link selector.
*
* @param string $link
* @param string $cssOrXpath
*
* @return string
*/
protected function getLinkSelector($link, $cssOrXpath)
{
if ($link) {
if (Locator::isCSS($cssOrXpath)) {
$link_selector = sprintf("%s a[href*='%s']", $cssOrXpath, $link);
} else {
$link_selector = sprintf("%s//a[contains(@href,'%s')]", $cssOrXpath, $link);
}
} else {
if (Locator::isCSS($cssOrXpath)) {
$link_selector = sprintf("%s a", $cssOrXpath);
} else {
$link_selector = sprintf("%s//a", $cssOrXpath);
}
}
return $link_selector;
}
/**
* See element has been applied a style.
*
* Allows you to check a single element has a css style assigned.
* e.g. you can check that ".icon-r" class is floated right.
*
* @param string $selector
* A CSS (only) selector to identify the element.
* @param string $style
* The style name e.g. font-weight, float
* @param string $value
* The value to check e.g. bold, right
* @param string $pseudo
* The pseudo selector to retrieve the style for e.g. ::before, :hover
*/
public function seeElementHasStyle($selector, $style, $value, $pseudo = null)
{
$this->assert($this->proceedSeeElementHasStyle($selector, $style, $value, $pseudo));
}
/**
* See element has not been applied a style.
*
* @param string $selector
* A CSS (only) selector to identify the element.
* @param string $style
* The style name e.g. font-weight, float
* @param string $value
* The value to check e.g. bold, right
* @param string $pseudo
* The pseudo selector to retrieve the style for e.g. ::before, :hover
*/
public function dontSeeElementHasStyle($selector, $style, $value, $pseudo = null)
{
$this->assertNot($this->proceedSeeElementHasStyle($selector, $style, $value, $pseudo));
}
/**
* Helper method for checking an element has a css style applied.
*
* @param string $selector
* A CSS (only) selector to identify the element.
* @param string $style
* The style name e.g. font-weight, float
* @param string $value
* The value to check e.g. bold, right
* @param string $pseudo
* The pseudo selector to retrieve the style for e.g. ::before, :hover
*
* @return array
*
* @throws \LogicException
* If attempting to call function when WebDriver not in use.
*/
protected function proceedSeeElementHasStyle($selector, $style, $value, $pseudo = null)
{
$computedvalue = $this->grabElementStyle($selector, $style, $pseudo);
return array("Equals", $value, $computedvalue);
}
/**
* Get a style value from an element.
*
* @param string $selector
* A CSS (only) selector to identify the element.
* @param string $style
* The style name e.g. font-weight, float
* @param string $pseudo
* The pseudo selector to retrieve the style for e.g. ::before, :hover
*
* @return mixed
*
* @throws \LogicException
* If attempting to call function when WebDriver not in use.
*/
public function grabElementStyle($selector, $style, $pseudo = null)
{
if ($this->getBrowserModuleName() !== 'WebDriver') {
throw new \LogicException("Computed styles only available for inspection when using WebDriver");
}
$js = sprintf(
"return window.getComputedStyle(document.querySelector('%s')%s)['%s']",
$selector,
$pseudo ? ", '$pseudo'" : "",
$style
);
return $this->getModule("WebDriver")->executeJs($js);
}
/**
* Helper function for seeRegexInSource() and dontSeeRegexInSource().
*
* Switch behaviour based on whether we're using PhpBrowser or WebDriver because they both handle acquisition of
* page content slightly differently.
*
* @param string $string
* The regular expression to check for.
*
* @return bool
* Returns true if string found.
*/
protected function proceedSeeRegexInSource($string)
{
$moduleName = $this->getBrowserModuleName();
switch ($moduleName) {
case 'PhpBrowser':
default:
$session = $this->getModule('PhpBrowser')->client;
$content = $session->getResponse()->getContent();
break;
case 'WebDriver':
/** @var WebDriver $module */
$module = $this->getModule($moduleName);
$content = $module->webDriver->getPageSource();
break;
}
return array("True", preg_match($string, $content) === 1);
}
/**
* Perform a simple preg_match() to check for regex $string in a page's
* source.
*
* @param $string
* The regex string to check for.
*/
public function seeRegexInSource($string)
{
$this->assert($this->proceedSeeRegexInSource($string));
}
/**
* Perform a simple preg_match to check that regex $string does not
* exist in a page's source.
*
* @param $string
* The regex string to check for.
*/
public function dontSeeRegexInSource($string)
{
$this->assertNot($this->proceedSeeRegexInSource($string));
}
}