Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Mink package.
|
Chris@0
|
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
6 *
|
Chris@0
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
8 * file that was distributed with this source code.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@0
|
11 namespace Behat\Mink;
|
Chris@0
|
12
|
Chris@0
|
13 use Behat\Mink\Element\Element;
|
Chris@0
|
14 use Behat\Mink\Element\ElementInterface;
|
Chris@0
|
15 use Behat\Mink\Element\NodeElement;
|
Chris@0
|
16 use Behat\Mink\Element\TraversableElement;
|
Chris@0
|
17 use Behat\Mink\Exception\ElementNotFoundException;
|
Chris@0
|
18 use Behat\Mink\Exception\ExpectationException;
|
Chris@0
|
19 use Behat\Mink\Exception\ResponseTextException;
|
Chris@0
|
20 use Behat\Mink\Exception\ElementHtmlException;
|
Chris@0
|
21 use Behat\Mink\Exception\ElementTextException;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Mink web assertions tool.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @author Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
27 */
|
Chris@0
|
28 class WebAssert
|
Chris@0
|
29 {
|
Chris@0
|
30 protected $session;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Initializes assertion engine.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @param Session $session
|
Chris@0
|
36 */
|
Chris@0
|
37 public function __construct(Session $session)
|
Chris@0
|
38 {
|
Chris@0
|
39 $this->session = $session;
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Checks that current session address is equals to provided one.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @param string $page
|
Chris@0
|
46 *
|
Chris@0
|
47 * @throws ExpectationException
|
Chris@0
|
48 */
|
Chris@0
|
49 public function addressEquals($page)
|
Chris@0
|
50 {
|
Chris@0
|
51 $expected = $this->cleanUrl($page);
|
Chris@0
|
52 $actual = $this->getCurrentUrlPath();
|
Chris@0
|
53
|
Chris@0
|
54 $this->assert($actual === $expected, sprintf('Current page is "%s", but "%s" expected.', $actual, $expected));
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Checks that current session address is not equals to provided one.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @param string $page
|
Chris@0
|
61 *
|
Chris@0
|
62 * @throws ExpectationException
|
Chris@0
|
63 */
|
Chris@0
|
64 public function addressNotEquals($page)
|
Chris@0
|
65 {
|
Chris@0
|
66 $expected = $this->cleanUrl($page);
|
Chris@0
|
67 $actual = $this->getCurrentUrlPath();
|
Chris@0
|
68
|
Chris@0
|
69 $this->assert($actual !== $expected, sprintf('Current page is "%s", but should not be.', $actual));
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Checks that current session address matches regex.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @param string $regex
|
Chris@0
|
76 *
|
Chris@0
|
77 * @throws ExpectationException
|
Chris@0
|
78 */
|
Chris@0
|
79 public function addressMatches($regex)
|
Chris@0
|
80 {
|
Chris@0
|
81 $actual = $this->getCurrentUrlPath();
|
Chris@0
|
82 $message = sprintf('Current page "%s" does not match the regex "%s".', $actual, $regex);
|
Chris@0
|
83
|
Chris@0
|
84 $this->assert((bool) preg_match($regex, $actual), $message);
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * Checks that specified cookie exists and its value equals to a given one.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @param string $name cookie name
|
Chris@0
|
91 * @param string $value cookie value
|
Chris@0
|
92 *
|
Chris@0
|
93 * @throws ExpectationException
|
Chris@0
|
94 */
|
Chris@0
|
95 public function cookieEquals($name, $value)
|
Chris@0
|
96 {
|
Chris@0
|
97 $this->cookieExists($name);
|
Chris@0
|
98
|
Chris@0
|
99 $actualValue = $this->session->getCookie($name);
|
Chris@0
|
100 $message = sprintf('Cookie "%s" value is "%s", but should be "%s".', $name, $actualValue, $value);
|
Chris@0
|
101
|
Chris@0
|
102 $this->assert($actualValue == $value, $message);
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * Checks that specified cookie exists.
|
Chris@0
|
107 *
|
Chris@0
|
108 * @param string $name cookie name
|
Chris@0
|
109 *
|
Chris@0
|
110 * @throws ExpectationException
|
Chris@0
|
111 */
|
Chris@0
|
112 public function cookieExists($name)
|
Chris@0
|
113 {
|
Chris@0
|
114 $message = sprintf('Cookie "%s" is not set, but should be.', $name);
|
Chris@0
|
115 $this->assert($this->session->getCookie($name) !== null, $message);
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * Checks that current response code equals to provided one.
|
Chris@0
|
120 *
|
Chris@0
|
121 * @param int $code
|
Chris@0
|
122 *
|
Chris@0
|
123 * @throws ExpectationException
|
Chris@0
|
124 */
|
Chris@0
|
125 public function statusCodeEquals($code)
|
Chris@0
|
126 {
|
Chris@0
|
127 $actual = $this->session->getStatusCode();
|
Chris@0
|
128 $message = sprintf('Current response status code is %d, but %d expected.', $actual, $code);
|
Chris@0
|
129
|
Chris@0
|
130 $this->assert(intval($code) === intval($actual), $message);
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * Checks that current response code not equals to provided one.
|
Chris@0
|
135 *
|
Chris@0
|
136 * @param int $code
|
Chris@0
|
137 *
|
Chris@0
|
138 * @throws ExpectationException
|
Chris@0
|
139 */
|
Chris@0
|
140 public function statusCodeNotEquals($code)
|
Chris@0
|
141 {
|
Chris@0
|
142 $actual = $this->session->getStatusCode();
|
Chris@0
|
143 $message = sprintf('Current response status code is %d, but should not be.', $actual);
|
Chris@0
|
144
|
Chris@0
|
145 $this->assert(intval($code) !== intval($actual), $message);
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * Checks that current response header equals value.
|
Chris@0
|
150 *
|
Chris@0
|
151 * @param string $name
|
Chris@0
|
152 * @param string $value
|
Chris@0
|
153 *
|
Chris@0
|
154 * @throws ExpectationException
|
Chris@0
|
155 */
|
Chris@0
|
156 public function responseHeaderEquals($name, $value)
|
Chris@0
|
157 {
|
Chris@0
|
158 $actual = $this->session->getResponseHeader($name);
|
Chris@0
|
159 $message = sprintf('Current response header "%s" is "%s", but "%s" expected.', $name, $actual, $value);
|
Chris@0
|
160
|
Chris@0
|
161 $this->assert($value === $actual, $message);
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * Checks that current response header does not equal value.
|
Chris@0
|
166 *
|
Chris@0
|
167 * @param string $name
|
Chris@0
|
168 * @param string $value
|
Chris@0
|
169 *
|
Chris@0
|
170 * @throws ExpectationException
|
Chris@0
|
171 */
|
Chris@0
|
172 public function responseHeaderNotEquals($name, $value)
|
Chris@0
|
173 {
|
Chris@0
|
174 $actual = $this->session->getResponseHeader($name);
|
Chris@0
|
175 $message = sprintf('Current response header "%s" is "%s", but should not be.', $name, $actual, $value);
|
Chris@0
|
176
|
Chris@0
|
177 $this->assert($value !== $actual, $message);
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 /**
|
Chris@0
|
181 * Checks that current response header contains value.
|
Chris@0
|
182 *
|
Chris@0
|
183 * @param string $name
|
Chris@0
|
184 * @param string $value
|
Chris@0
|
185 *
|
Chris@0
|
186 * @throws ExpectationException
|
Chris@0
|
187 */
|
Chris@0
|
188 public function responseHeaderContains($name, $value)
|
Chris@0
|
189 {
|
Chris@0
|
190 $actual = $this->session->getResponseHeader($name);
|
Chris@0
|
191 $message = sprintf('The text "%s" was not found anywhere in the "%s" response header.', $value, $name);
|
Chris@0
|
192
|
Chris@18
|
193 $this->assert(false !== stripos($actual, (string) $value), $message);
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 /**
|
Chris@0
|
197 * Checks that current response header does not contain value.
|
Chris@0
|
198 *
|
Chris@0
|
199 * @param string $name
|
Chris@0
|
200 * @param string $value
|
Chris@0
|
201 *
|
Chris@0
|
202 * @throws ExpectationException
|
Chris@0
|
203 */
|
Chris@0
|
204 public function responseHeaderNotContains($name, $value)
|
Chris@0
|
205 {
|
Chris@0
|
206 $actual = $this->session->getResponseHeader($name);
|
Chris@0
|
207 $message = sprintf('The text "%s" was found in the "%s" response header, but it should not.', $value, $name);
|
Chris@0
|
208
|
Chris@18
|
209 $this->assert(false === stripos($actual, (string) $value), $message);
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * Checks that current response header matches regex.
|
Chris@0
|
214 *
|
Chris@0
|
215 * @param string $name
|
Chris@0
|
216 * @param string $regex
|
Chris@0
|
217 *
|
Chris@0
|
218 * @throws ExpectationException
|
Chris@0
|
219 */
|
Chris@0
|
220 public function responseHeaderMatches($name, $regex)
|
Chris@0
|
221 {
|
Chris@0
|
222 $actual = $this->session->getResponseHeader($name);
|
Chris@0
|
223 $message = sprintf('The pattern "%s" was not found anywhere in the "%s" response header.', $regex, $name);
|
Chris@0
|
224
|
Chris@0
|
225 $this->assert((bool) preg_match($regex, $actual), $message);
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 /**
|
Chris@0
|
229 * Checks that current response header does not match regex.
|
Chris@0
|
230 *
|
Chris@0
|
231 * @param string $name
|
Chris@0
|
232 * @param string $regex
|
Chris@0
|
233 *
|
Chris@0
|
234 * @throws ExpectationException
|
Chris@0
|
235 */
|
Chris@0
|
236 public function responseHeaderNotMatches($name, $regex)
|
Chris@0
|
237 {
|
Chris@0
|
238 $actual = $this->session->getResponseHeader($name);
|
Chris@0
|
239 $message = sprintf(
|
Chris@0
|
240 'The pattern "%s" was found in the text of the "%s" response header, but it should not.',
|
Chris@0
|
241 $regex,
|
Chris@0
|
242 $name
|
Chris@0
|
243 );
|
Chris@0
|
244
|
Chris@0
|
245 $this->assert(!preg_match($regex, $actual), $message);
|
Chris@0
|
246 }
|
Chris@0
|
247
|
Chris@0
|
248 /**
|
Chris@0
|
249 * Checks that current page contains text.
|
Chris@0
|
250 *
|
Chris@0
|
251 * @param string $text
|
Chris@0
|
252 *
|
Chris@0
|
253 * @throws ResponseTextException
|
Chris@0
|
254 */
|
Chris@0
|
255 public function pageTextContains($text)
|
Chris@0
|
256 {
|
Chris@0
|
257 $actual = $this->session->getPage()->getText();
|
Chris@0
|
258 $actual = preg_replace('/\s+/u', ' ', $actual);
|
Chris@0
|
259 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
260 $message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text);
|
Chris@0
|
261
|
Chris@0
|
262 $this->assertResponseText((bool) preg_match($regex, $actual), $message);
|
Chris@0
|
263 }
|
Chris@0
|
264
|
Chris@0
|
265 /**
|
Chris@0
|
266 * Checks that current page does not contains text.
|
Chris@0
|
267 *
|
Chris@0
|
268 * @param string $text
|
Chris@0
|
269 *
|
Chris@0
|
270 * @throws ResponseTextException
|
Chris@0
|
271 */
|
Chris@0
|
272 public function pageTextNotContains($text)
|
Chris@0
|
273 {
|
Chris@0
|
274 $actual = $this->session->getPage()->getText();
|
Chris@0
|
275 $actual = preg_replace('/\s+/u', ' ', $actual);
|
Chris@0
|
276 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
277 $message = sprintf('The text "%s" appears in the text of this page, but it should not.', $text);
|
Chris@0
|
278
|
Chris@0
|
279 $this->assertResponseText(!preg_match($regex, $actual), $message);
|
Chris@0
|
280 }
|
Chris@0
|
281
|
Chris@0
|
282 /**
|
Chris@0
|
283 * Checks that current page text matches regex.
|
Chris@0
|
284 *
|
Chris@0
|
285 * @param string $regex
|
Chris@0
|
286 *
|
Chris@0
|
287 * @throws ResponseTextException
|
Chris@0
|
288 */
|
Chris@0
|
289 public function pageTextMatches($regex)
|
Chris@0
|
290 {
|
Chris@0
|
291 $actual = $this->session->getPage()->getText();
|
Chris@0
|
292 $message = sprintf('The pattern %s was not found anywhere in the text of the current page.', $regex);
|
Chris@0
|
293
|
Chris@0
|
294 $this->assertResponseText((bool) preg_match($regex, $actual), $message);
|
Chris@0
|
295 }
|
Chris@0
|
296
|
Chris@0
|
297 /**
|
Chris@0
|
298 * Checks that current page text does not matches regex.
|
Chris@0
|
299 *
|
Chris@0
|
300 * @param string $regex
|
Chris@0
|
301 *
|
Chris@0
|
302 * @throws ResponseTextException
|
Chris@0
|
303 */
|
Chris@0
|
304 public function pageTextNotMatches($regex)
|
Chris@0
|
305 {
|
Chris@0
|
306 $actual = $this->session->getPage()->getText();
|
Chris@0
|
307 $message = sprintf('The pattern %s was found in the text of the current page, but it should not.', $regex);
|
Chris@0
|
308
|
Chris@0
|
309 $this->assertResponseText(!preg_match($regex, $actual), $message);
|
Chris@0
|
310 }
|
Chris@0
|
311
|
Chris@0
|
312 /**
|
Chris@0
|
313 * Checks that page HTML (response content) contains text.
|
Chris@0
|
314 *
|
Chris@0
|
315 * @param string $text
|
Chris@0
|
316 *
|
Chris@0
|
317 * @throws ExpectationException
|
Chris@0
|
318 */
|
Chris@0
|
319 public function responseContains($text)
|
Chris@0
|
320 {
|
Chris@0
|
321 $actual = $this->session->getPage()->getContent();
|
Chris@0
|
322 $message = sprintf('The string "%s" was not found anywhere in the HTML response of the current page.', $text);
|
Chris@0
|
323
|
Chris@18
|
324 $this->assert(stripos($actual, (string) $text) !== false, $message);
|
Chris@0
|
325 }
|
Chris@0
|
326
|
Chris@0
|
327 /**
|
Chris@0
|
328 * Checks that page HTML (response content) does not contains text.
|
Chris@0
|
329 *
|
Chris@0
|
330 * @param string $text
|
Chris@0
|
331 *
|
Chris@0
|
332 * @throws ExpectationException
|
Chris@0
|
333 */
|
Chris@0
|
334 public function responseNotContains($text)
|
Chris@0
|
335 {
|
Chris@0
|
336 $actual = $this->session->getPage()->getContent();
|
Chris@0
|
337 $message = sprintf('The string "%s" appears in the HTML response of this page, but it should not.', $text);
|
Chris@0
|
338
|
Chris@18
|
339 $this->assert(stripos($actual, (string) $text) === false, $message);
|
Chris@0
|
340 }
|
Chris@0
|
341
|
Chris@0
|
342 /**
|
Chris@0
|
343 * Checks that page HTML (response content) matches regex.
|
Chris@0
|
344 *
|
Chris@0
|
345 * @param string $regex
|
Chris@0
|
346 *
|
Chris@0
|
347 * @throws ExpectationException
|
Chris@0
|
348 */
|
Chris@0
|
349 public function responseMatches($regex)
|
Chris@0
|
350 {
|
Chris@0
|
351 $actual = $this->session->getPage()->getContent();
|
Chris@0
|
352 $message = sprintf('The pattern %s was not found anywhere in the HTML response of the page.', $regex);
|
Chris@0
|
353
|
Chris@0
|
354 $this->assert((bool) preg_match($regex, $actual), $message);
|
Chris@0
|
355 }
|
Chris@0
|
356
|
Chris@0
|
357 /**
|
Chris@0
|
358 * Checks that page HTML (response content) does not matches regex.
|
Chris@0
|
359 *
|
Chris@0
|
360 * @param $regex
|
Chris@0
|
361 *
|
Chris@0
|
362 * @throws ExpectationException
|
Chris@0
|
363 */
|
Chris@0
|
364 public function responseNotMatches($regex)
|
Chris@0
|
365 {
|
Chris@0
|
366 $actual = $this->session->getPage()->getContent();
|
Chris@0
|
367 $message = sprintf('The pattern %s was found in the HTML response of the page, but it should not.', $regex);
|
Chris@0
|
368
|
Chris@0
|
369 $this->assert(!preg_match($regex, $actual), $message);
|
Chris@0
|
370 }
|
Chris@0
|
371
|
Chris@0
|
372 /**
|
Chris@0
|
373 * Checks that there is specified number of specific elements on the page.
|
Chris@0
|
374 *
|
Chris@0
|
375 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
376 * @param string|array $selector element selector
|
Chris@0
|
377 * @param int $count expected count
|
Chris@0
|
378 * @param ElementInterface $container document to check against
|
Chris@0
|
379 *
|
Chris@0
|
380 * @throws ExpectationException
|
Chris@0
|
381 */
|
Chris@0
|
382 public function elementsCount($selectorType, $selector, $count, ElementInterface $container = null)
|
Chris@0
|
383 {
|
Chris@0
|
384 $container = $container ?: $this->session->getPage();
|
Chris@0
|
385 $nodes = $container->findAll($selectorType, $selector);
|
Chris@0
|
386
|
Chris@0
|
387 $message = sprintf(
|
Chris@0
|
388 '%d %s found on the page, but should be %d.',
|
Chris@0
|
389 count($nodes),
|
Chris@0
|
390 $this->getMatchingElementRepresentation($selectorType, $selector, count($nodes) !== 1),
|
Chris@0
|
391 $count
|
Chris@0
|
392 );
|
Chris@0
|
393
|
Chris@0
|
394 $this->assert(intval($count) === count($nodes), $message);
|
Chris@0
|
395 }
|
Chris@0
|
396
|
Chris@0
|
397 /**
|
Chris@0
|
398 * Checks that specific element exists on the current page.
|
Chris@0
|
399 *
|
Chris@0
|
400 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
401 * @param string|array $selector element selector
|
Chris@0
|
402 * @param ElementInterface $container document to check against
|
Chris@0
|
403 *
|
Chris@0
|
404 * @return NodeElement
|
Chris@0
|
405 *
|
Chris@0
|
406 * @throws ElementNotFoundException
|
Chris@0
|
407 */
|
Chris@0
|
408 public function elementExists($selectorType, $selector, ElementInterface $container = null)
|
Chris@0
|
409 {
|
Chris@0
|
410 $container = $container ?: $this->session->getPage();
|
Chris@0
|
411 $node = $container->find($selectorType, $selector);
|
Chris@0
|
412
|
Chris@0
|
413 if (null === $node) {
|
Chris@0
|
414 if (is_array($selector)) {
|
Chris@0
|
415 $selector = implode(' ', $selector);
|
Chris@0
|
416 }
|
Chris@0
|
417
|
Chris@0
|
418 throw new ElementNotFoundException($this->session->getDriver(), 'element', $selectorType, $selector);
|
Chris@0
|
419 }
|
Chris@0
|
420
|
Chris@0
|
421 return $node;
|
Chris@0
|
422 }
|
Chris@0
|
423
|
Chris@0
|
424 /**
|
Chris@0
|
425 * Checks that specific element does not exists on the current page.
|
Chris@0
|
426 *
|
Chris@0
|
427 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
428 * @param string|array $selector element selector
|
Chris@0
|
429 * @param ElementInterface $container document to check against
|
Chris@0
|
430 *
|
Chris@0
|
431 * @throws ExpectationException
|
Chris@0
|
432 */
|
Chris@0
|
433 public function elementNotExists($selectorType, $selector, ElementInterface $container = null)
|
Chris@0
|
434 {
|
Chris@0
|
435 $container = $container ?: $this->session->getPage();
|
Chris@0
|
436 $node = $container->find($selectorType, $selector);
|
Chris@0
|
437
|
Chris@0
|
438 $message = sprintf(
|
Chris@0
|
439 'An %s appears on this page, but it should not.',
|
Chris@0
|
440 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
441 );
|
Chris@0
|
442
|
Chris@0
|
443 $this->assert(null === $node, $message);
|
Chris@0
|
444 }
|
Chris@0
|
445
|
Chris@0
|
446 /**
|
Chris@0
|
447 * Checks that specific element contains text.
|
Chris@0
|
448 *
|
Chris@0
|
449 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
450 * @param string|array $selector element selector
|
Chris@0
|
451 * @param string $text expected text
|
Chris@0
|
452 *
|
Chris@0
|
453 * @throws ElementTextException
|
Chris@0
|
454 */
|
Chris@0
|
455 public function elementTextContains($selectorType, $selector, $text)
|
Chris@0
|
456 {
|
Chris@0
|
457 $element = $this->elementExists($selectorType, $selector);
|
Chris@0
|
458 $actual = $element->getText();
|
Chris@0
|
459 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
460
|
Chris@0
|
461 $message = sprintf(
|
Chris@0
|
462 'The text "%s" was not found in the text of the %s.',
|
Chris@0
|
463 $text,
|
Chris@0
|
464 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
465 );
|
Chris@0
|
466
|
Chris@0
|
467 $this->assertElementText((bool) preg_match($regex, $actual), $message, $element);
|
Chris@0
|
468 }
|
Chris@0
|
469
|
Chris@0
|
470 /**
|
Chris@0
|
471 * Checks that specific element does not contains text.
|
Chris@0
|
472 *
|
Chris@0
|
473 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
474 * @param string|array $selector element selector
|
Chris@0
|
475 * @param string $text expected text
|
Chris@0
|
476 *
|
Chris@0
|
477 * @throws ElementTextException
|
Chris@0
|
478 */
|
Chris@0
|
479 public function elementTextNotContains($selectorType, $selector, $text)
|
Chris@0
|
480 {
|
Chris@0
|
481 $element = $this->elementExists($selectorType, $selector);
|
Chris@0
|
482 $actual = $element->getText();
|
Chris@0
|
483 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
484
|
Chris@0
|
485 $message = sprintf(
|
Chris@0
|
486 'The text "%s" appears in the text of the %s, but it should not.',
|
Chris@0
|
487 $text,
|
Chris@0
|
488 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
489 );
|
Chris@0
|
490
|
Chris@0
|
491 $this->assertElementText(!preg_match($regex, $actual), $message, $element);
|
Chris@0
|
492 }
|
Chris@0
|
493
|
Chris@0
|
494 /**
|
Chris@0
|
495 * Checks that specific element contains HTML.
|
Chris@0
|
496 *
|
Chris@0
|
497 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
498 * @param string|array $selector element selector
|
Chris@0
|
499 * @param string $html expected text
|
Chris@0
|
500 *
|
Chris@0
|
501 * @throws ElementHtmlException
|
Chris@0
|
502 */
|
Chris@0
|
503 public function elementContains($selectorType, $selector, $html)
|
Chris@0
|
504 {
|
Chris@0
|
505 $element = $this->elementExists($selectorType, $selector);
|
Chris@0
|
506 $actual = $element->getHtml();
|
Chris@0
|
507 $regex = '/'.preg_quote($html, '/').'/ui';
|
Chris@0
|
508
|
Chris@0
|
509 $message = sprintf(
|
Chris@0
|
510 'The string "%s" was not found in the HTML of the %s.',
|
Chris@0
|
511 $html,
|
Chris@0
|
512 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
513 );
|
Chris@0
|
514
|
Chris@0
|
515 $this->assertElement((bool) preg_match($regex, $actual), $message, $element);
|
Chris@0
|
516 }
|
Chris@0
|
517
|
Chris@0
|
518 /**
|
Chris@0
|
519 * Checks that specific element does not contains HTML.
|
Chris@0
|
520 *
|
Chris@0
|
521 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
522 * @param string|array $selector element selector
|
Chris@0
|
523 * @param string $html expected text
|
Chris@0
|
524 *
|
Chris@0
|
525 * @throws ElementHtmlException
|
Chris@0
|
526 */
|
Chris@0
|
527 public function elementNotContains($selectorType, $selector, $html)
|
Chris@0
|
528 {
|
Chris@0
|
529 $element = $this->elementExists($selectorType, $selector);
|
Chris@0
|
530 $actual = $element->getHtml();
|
Chris@0
|
531 $regex = '/'.preg_quote($html, '/').'/ui';
|
Chris@0
|
532
|
Chris@0
|
533 $message = sprintf(
|
Chris@0
|
534 'The string "%s" appears in the HTML of the %s, but it should not.',
|
Chris@0
|
535 $html,
|
Chris@0
|
536 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
537 );
|
Chris@0
|
538
|
Chris@0
|
539 $this->assertElement(!preg_match($regex, $actual), $message, $element);
|
Chris@0
|
540 }
|
Chris@0
|
541
|
Chris@0
|
542 /**
|
Chris@0
|
543 * Checks that an attribute exists in an element.
|
Chris@0
|
544 *
|
Chris@0
|
545 * @param string $selectorType
|
Chris@0
|
546 * @param string|array $selector
|
Chris@0
|
547 * @param string $attribute
|
Chris@0
|
548 *
|
Chris@0
|
549 * @return NodeElement
|
Chris@0
|
550 *
|
Chris@0
|
551 * @throws ElementHtmlException
|
Chris@0
|
552 */
|
Chris@0
|
553 public function elementAttributeExists($selectorType, $selector, $attribute)
|
Chris@0
|
554 {
|
Chris@0
|
555 $element = $this->elementExists($selectorType, $selector);
|
Chris@0
|
556
|
Chris@0
|
557 $message = sprintf(
|
Chris@0
|
558 'The attribute "%s" was not found in the %s.',
|
Chris@0
|
559 $attribute,
|
Chris@0
|
560 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
561 );
|
Chris@0
|
562
|
Chris@0
|
563 $this->assertElement($element->hasAttribute($attribute), $message, $element);
|
Chris@0
|
564
|
Chris@0
|
565 return $element;
|
Chris@0
|
566 }
|
Chris@0
|
567
|
Chris@0
|
568 /**
|
Chris@0
|
569 * Checks that an attribute of a specific elements contains text.
|
Chris@0
|
570 *
|
Chris@0
|
571 * @param string $selectorType
|
Chris@0
|
572 * @param string|array $selector
|
Chris@0
|
573 * @param string $attribute
|
Chris@0
|
574 * @param string $text
|
Chris@0
|
575 *
|
Chris@0
|
576 * @throws ElementHtmlException
|
Chris@0
|
577 */
|
Chris@0
|
578 public function elementAttributeContains($selectorType, $selector, $attribute, $text)
|
Chris@0
|
579 {
|
Chris@0
|
580 $element = $this->elementAttributeExists($selectorType, $selector, $attribute);
|
Chris@0
|
581 $actual = $element->getAttribute($attribute);
|
Chris@0
|
582 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
583
|
Chris@0
|
584 $message = sprintf(
|
Chris@0
|
585 'The text "%s" was not found in the attribute "%s" of the %s.',
|
Chris@0
|
586 $text,
|
Chris@0
|
587 $attribute,
|
Chris@0
|
588 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
589 );
|
Chris@0
|
590
|
Chris@0
|
591 $this->assertElement((bool) preg_match($regex, $actual), $message, $element);
|
Chris@0
|
592 }
|
Chris@0
|
593
|
Chris@0
|
594 /**
|
Chris@0
|
595 * Checks that an attribute of a specific elements does not contain text.
|
Chris@0
|
596 *
|
Chris@0
|
597 * @param string $selectorType
|
Chris@0
|
598 * @param string|array $selector
|
Chris@0
|
599 * @param string $attribute
|
Chris@0
|
600 * @param string $text
|
Chris@0
|
601 *
|
Chris@0
|
602 * @throws ElementHtmlException
|
Chris@0
|
603 */
|
Chris@0
|
604 public function elementAttributeNotContains($selectorType, $selector, $attribute, $text)
|
Chris@0
|
605 {
|
Chris@0
|
606 $element = $this->elementAttributeExists($selectorType, $selector, $attribute);
|
Chris@0
|
607 $actual = $element->getAttribute($attribute);
|
Chris@0
|
608 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
609
|
Chris@0
|
610 $message = sprintf(
|
Chris@0
|
611 'The text "%s" was found in the attribute "%s" of the %s.',
|
Chris@0
|
612 $text,
|
Chris@0
|
613 $attribute,
|
Chris@0
|
614 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
615 );
|
Chris@0
|
616
|
Chris@0
|
617 $this->assertElement(!preg_match($regex, $actual), $message, $element);
|
Chris@0
|
618 }
|
Chris@0
|
619
|
Chris@0
|
620 /**
|
Chris@0
|
621 * Checks that specific field exists on the current page.
|
Chris@0
|
622 *
|
Chris@0
|
623 * @param string $field field id|name|label|value
|
Chris@0
|
624 * @param TraversableElement $container document to check against
|
Chris@0
|
625 *
|
Chris@0
|
626 * @return NodeElement
|
Chris@0
|
627 *
|
Chris@0
|
628 * @throws ElementNotFoundException
|
Chris@0
|
629 */
|
Chris@0
|
630 public function fieldExists($field, TraversableElement $container = null)
|
Chris@0
|
631 {
|
Chris@0
|
632 $container = $container ?: $this->session->getPage();
|
Chris@0
|
633 $node = $container->findField($field);
|
Chris@0
|
634
|
Chris@0
|
635 if (null === $node) {
|
Chris@0
|
636 throw new ElementNotFoundException($this->session->getDriver(), 'form field', 'id|name|label|value', $field);
|
Chris@0
|
637 }
|
Chris@0
|
638
|
Chris@0
|
639 return $node;
|
Chris@0
|
640 }
|
Chris@0
|
641
|
Chris@0
|
642 /**
|
Chris@0
|
643 * Checks that specific field does not exists on the current page.
|
Chris@0
|
644 *
|
Chris@0
|
645 * @param string $field field id|name|label|value
|
Chris@0
|
646 * @param TraversableElement $container document to check against
|
Chris@0
|
647 *
|
Chris@0
|
648 * @throws ExpectationException
|
Chris@0
|
649 */
|
Chris@0
|
650 public function fieldNotExists($field, TraversableElement $container = null)
|
Chris@0
|
651 {
|
Chris@0
|
652 $container = $container ?: $this->session->getPage();
|
Chris@0
|
653 $node = $container->findField($field);
|
Chris@0
|
654
|
Chris@0
|
655 $this->assert(null === $node, sprintf('A field "%s" appears on this page, but it should not.', $field));
|
Chris@0
|
656 }
|
Chris@0
|
657
|
Chris@0
|
658 /**
|
Chris@0
|
659 * Checks that specific field have provided value.
|
Chris@0
|
660 *
|
Chris@0
|
661 * @param string $field field id|name|label|value
|
Chris@0
|
662 * @param string $value field value
|
Chris@0
|
663 * @param TraversableElement $container document to check against
|
Chris@0
|
664 *
|
Chris@0
|
665 * @throws ExpectationException
|
Chris@0
|
666 */
|
Chris@0
|
667 public function fieldValueEquals($field, $value, TraversableElement $container = null)
|
Chris@0
|
668 {
|
Chris@0
|
669 $node = $this->fieldExists($field, $container);
|
Chris@0
|
670 $actual = $node->getValue();
|
Chris@0
|
671 $regex = '/^'.preg_quote($value, '/').'$/ui';
|
Chris@0
|
672
|
Chris@0
|
673 $message = sprintf('The field "%s" value is "%s", but "%s" expected.', $field, $actual, $value);
|
Chris@0
|
674
|
Chris@0
|
675 $this->assert((bool) preg_match($regex, $actual), $message);
|
Chris@0
|
676 }
|
Chris@0
|
677
|
Chris@0
|
678 /**
|
Chris@0
|
679 * Checks that specific field have provided value.
|
Chris@0
|
680 *
|
Chris@0
|
681 * @param string $field field id|name|label|value
|
Chris@0
|
682 * @param string $value field value
|
Chris@0
|
683 * @param TraversableElement $container document to check against
|
Chris@0
|
684 *
|
Chris@0
|
685 * @throws ExpectationException
|
Chris@0
|
686 */
|
Chris@0
|
687 public function fieldValueNotEquals($field, $value, TraversableElement $container = null)
|
Chris@0
|
688 {
|
Chris@0
|
689 $node = $this->fieldExists($field, $container);
|
Chris@0
|
690 $actual = $node->getValue();
|
Chris@0
|
691 $regex = '/^'.preg_quote($value, '/').'$/ui';
|
Chris@0
|
692
|
Chris@0
|
693 $message = sprintf('The field "%s" value is "%s", but it should not be.', $field, $actual);
|
Chris@0
|
694
|
Chris@0
|
695 $this->assert(!preg_match($regex, $actual), $message);
|
Chris@0
|
696 }
|
Chris@0
|
697
|
Chris@0
|
698 /**
|
Chris@0
|
699 * Checks that specific checkbox is checked.
|
Chris@0
|
700 *
|
Chris@0
|
701 * @param string $field field id|name|label|value
|
Chris@0
|
702 * @param TraversableElement $container document to check against
|
Chris@0
|
703 *
|
Chris@0
|
704 * @throws ExpectationException
|
Chris@0
|
705 */
|
Chris@0
|
706 public function checkboxChecked($field, TraversableElement $container = null)
|
Chris@0
|
707 {
|
Chris@0
|
708 $node = $this->fieldExists($field, $container);
|
Chris@0
|
709
|
Chris@0
|
710 $this->assert($node->isChecked(), sprintf('Checkbox "%s" is not checked, but it should be.', $field));
|
Chris@0
|
711 }
|
Chris@0
|
712
|
Chris@0
|
713 /**
|
Chris@0
|
714 * Checks that specific checkbox is unchecked.
|
Chris@0
|
715 *
|
Chris@0
|
716 * @param string $field field id|name|label|value
|
Chris@0
|
717 * @param TraversableElement $container document to check against
|
Chris@0
|
718 *
|
Chris@0
|
719 * @throws ExpectationException
|
Chris@0
|
720 */
|
Chris@0
|
721 public function checkboxNotChecked($field, TraversableElement $container = null)
|
Chris@0
|
722 {
|
Chris@0
|
723 $node = $this->fieldExists($field, $container);
|
Chris@0
|
724
|
Chris@0
|
725 $this->assert(!$node->isChecked(), sprintf('Checkbox "%s" is checked, but it should not be.', $field));
|
Chris@0
|
726 }
|
Chris@0
|
727
|
Chris@0
|
728 /**
|
Chris@0
|
729 * Gets current url of the page.
|
Chris@0
|
730 *
|
Chris@0
|
731 * @return string
|
Chris@0
|
732 */
|
Chris@0
|
733 protected function getCurrentUrlPath()
|
Chris@0
|
734 {
|
Chris@0
|
735 return $this->cleanUrl($this->session->getCurrentUrl());
|
Chris@0
|
736 }
|
Chris@0
|
737
|
Chris@0
|
738 /**
|
Chris@0
|
739 * Trims scriptname from the URL.
|
Chris@0
|
740 *
|
Chris@0
|
741 * @param string $url
|
Chris@0
|
742 *
|
Chris@0
|
743 * @return string
|
Chris@0
|
744 */
|
Chris@0
|
745 protected function cleanUrl($url)
|
Chris@0
|
746 {
|
Chris@0
|
747 $parts = parse_url($url);
|
Chris@0
|
748 $fragment = empty($parts['fragment']) ? '' : '#'.$parts['fragment'];
|
Chris@0
|
749 $path = empty($parts['path']) ? '/' : $parts['path'];
|
Chris@0
|
750
|
Chris@0
|
751 return preg_replace('/^\/[^\.\/]+\.php\//', '/', $path).$fragment;
|
Chris@0
|
752 }
|
Chris@0
|
753
|
Chris@0
|
754 /**
|
Chris@0
|
755 * Asserts a condition.
|
Chris@0
|
756 *
|
Chris@0
|
757 * @param bool $condition
|
Chris@0
|
758 * @param string $message Failure message
|
Chris@0
|
759 *
|
Chris@0
|
760 * @throws ExpectationException when the condition is not fulfilled
|
Chris@0
|
761 */
|
Chris@0
|
762 private function assert($condition, $message)
|
Chris@0
|
763 {
|
Chris@0
|
764 if ($condition) {
|
Chris@0
|
765 return;
|
Chris@0
|
766 }
|
Chris@0
|
767
|
Chris@0
|
768 throw new ExpectationException($message, $this->session->getDriver());
|
Chris@0
|
769 }
|
Chris@0
|
770
|
Chris@0
|
771 /**
|
Chris@0
|
772 * Asserts a condition involving the response text.
|
Chris@0
|
773 *
|
Chris@0
|
774 * @param bool $condition
|
Chris@0
|
775 * @param string $message Failure message
|
Chris@0
|
776 *
|
Chris@0
|
777 * @throws ResponseTextException when the condition is not fulfilled
|
Chris@0
|
778 */
|
Chris@0
|
779 private function assertResponseText($condition, $message)
|
Chris@0
|
780 {
|
Chris@0
|
781 if ($condition) {
|
Chris@0
|
782 return;
|
Chris@0
|
783 }
|
Chris@0
|
784
|
Chris@0
|
785 throw new ResponseTextException($message, $this->session->getDriver());
|
Chris@0
|
786 }
|
Chris@0
|
787
|
Chris@0
|
788 /**
|
Chris@0
|
789 * Asserts a condition on an element.
|
Chris@0
|
790 *
|
Chris@0
|
791 * @param bool $condition
|
Chris@0
|
792 * @param string $message Failure message
|
Chris@0
|
793 * @param Element $element
|
Chris@0
|
794 *
|
Chris@0
|
795 * @throws ElementHtmlException when the condition is not fulfilled
|
Chris@0
|
796 */
|
Chris@0
|
797 private function assertElement($condition, $message, Element $element)
|
Chris@0
|
798 {
|
Chris@0
|
799 if ($condition) {
|
Chris@0
|
800 return;
|
Chris@0
|
801 }
|
Chris@0
|
802
|
Chris@0
|
803 throw new ElementHtmlException($message, $this->session->getDriver(), $element);
|
Chris@0
|
804 }
|
Chris@0
|
805
|
Chris@0
|
806 /**
|
Chris@0
|
807 * Asserts a condition involving the text of an element.
|
Chris@0
|
808 *
|
Chris@0
|
809 * @param bool $condition
|
Chris@0
|
810 * @param string $message Failure message
|
Chris@0
|
811 * @param Element $element
|
Chris@0
|
812 *
|
Chris@0
|
813 * @throws ElementTextException when the condition is not fulfilled
|
Chris@0
|
814 */
|
Chris@0
|
815 private function assertElementText($condition, $message, Element $element)
|
Chris@0
|
816 {
|
Chris@0
|
817 if ($condition) {
|
Chris@0
|
818 return;
|
Chris@0
|
819 }
|
Chris@0
|
820
|
Chris@0
|
821 throw new ElementTextException($message, $this->session->getDriver(), $element);
|
Chris@0
|
822 }
|
Chris@0
|
823
|
Chris@0
|
824 /**
|
Chris@0
|
825 * @param string $selectorType
|
Chris@0
|
826 * @param string|array $selector
|
Chris@0
|
827 * @param bool $plural
|
Chris@0
|
828 *
|
Chris@0
|
829 * @return string
|
Chris@0
|
830 */
|
Chris@0
|
831 private function getMatchingElementRepresentation($selectorType, $selector, $plural = false)
|
Chris@0
|
832 {
|
Chris@0
|
833 $pluralization = $plural ? 's' : '';
|
Chris@0
|
834
|
Chris@0
|
835 if (in_array($selectorType, array('named', 'named_exact', 'named_partial'))
|
Chris@0
|
836 && is_array($selector) && 2 === count($selector)
|
Chris@0
|
837 ) {
|
Chris@0
|
838 return sprintf('%s%s matching locator "%s"', $selector[0], $pluralization, $selector[1]);
|
Chris@0
|
839 }
|
Chris@0
|
840
|
Chris@0
|
841 if (is_array($selector)) {
|
Chris@0
|
842 $selector = implode(' ', $selector);
|
Chris@0
|
843 }
|
Chris@0
|
844
|
Chris@0
|
845 return sprintf('element%s matching %s "%s"', $pluralization, $selectorType, $selector);
|
Chris@0
|
846 }
|
Chris@0
|
847 }
|