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@0
|
193 $this->assert(false !== stripos($actual, $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@0
|
209 $this->assert(false === stripos($actual, $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 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
323 $message = sprintf('The string "%s" was not found anywhere in the HTML response of the current page.', $text);
|
Chris@0
|
324
|
Chris@0
|
325 $this->assert((bool) preg_match($regex, $actual), $message);
|
Chris@0
|
326 }
|
Chris@0
|
327
|
Chris@0
|
328 /**
|
Chris@0
|
329 * Checks that page HTML (response content) does not contains text.
|
Chris@0
|
330 *
|
Chris@0
|
331 * @param string $text
|
Chris@0
|
332 *
|
Chris@0
|
333 * @throws ExpectationException
|
Chris@0
|
334 */
|
Chris@0
|
335 public function responseNotContains($text)
|
Chris@0
|
336 {
|
Chris@0
|
337 $actual = $this->session->getPage()->getContent();
|
Chris@0
|
338 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
339 $message = sprintf('The string "%s" appears in the HTML response of this page, but it should not.', $text);
|
Chris@0
|
340
|
Chris@0
|
341 $this->assert(!preg_match($regex, $actual), $message);
|
Chris@0
|
342 }
|
Chris@0
|
343
|
Chris@0
|
344 /**
|
Chris@0
|
345 * Checks that page HTML (response content) matches regex.
|
Chris@0
|
346 *
|
Chris@0
|
347 * @param string $regex
|
Chris@0
|
348 *
|
Chris@0
|
349 * @throws ExpectationException
|
Chris@0
|
350 */
|
Chris@0
|
351 public function responseMatches($regex)
|
Chris@0
|
352 {
|
Chris@0
|
353 $actual = $this->session->getPage()->getContent();
|
Chris@0
|
354 $message = sprintf('The pattern %s was not found anywhere in the HTML response of the page.', $regex);
|
Chris@0
|
355
|
Chris@0
|
356 $this->assert((bool) preg_match($regex, $actual), $message);
|
Chris@0
|
357 }
|
Chris@0
|
358
|
Chris@0
|
359 /**
|
Chris@0
|
360 * Checks that page HTML (response content) does not matches regex.
|
Chris@0
|
361 *
|
Chris@0
|
362 * @param $regex
|
Chris@0
|
363 *
|
Chris@0
|
364 * @throws ExpectationException
|
Chris@0
|
365 */
|
Chris@0
|
366 public function responseNotMatches($regex)
|
Chris@0
|
367 {
|
Chris@0
|
368 $actual = $this->session->getPage()->getContent();
|
Chris@0
|
369 $message = sprintf('The pattern %s was found in the HTML response of the page, but it should not.', $regex);
|
Chris@0
|
370
|
Chris@0
|
371 $this->assert(!preg_match($regex, $actual), $message);
|
Chris@0
|
372 }
|
Chris@0
|
373
|
Chris@0
|
374 /**
|
Chris@0
|
375 * Checks that there is specified number of specific elements on the page.
|
Chris@0
|
376 *
|
Chris@0
|
377 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
378 * @param string|array $selector element selector
|
Chris@0
|
379 * @param int $count expected count
|
Chris@0
|
380 * @param ElementInterface $container document to check against
|
Chris@0
|
381 *
|
Chris@0
|
382 * @throws ExpectationException
|
Chris@0
|
383 */
|
Chris@0
|
384 public function elementsCount($selectorType, $selector, $count, ElementInterface $container = null)
|
Chris@0
|
385 {
|
Chris@0
|
386 $container = $container ?: $this->session->getPage();
|
Chris@0
|
387 $nodes = $container->findAll($selectorType, $selector);
|
Chris@0
|
388
|
Chris@0
|
389 $message = sprintf(
|
Chris@0
|
390 '%d %s found on the page, but should be %d.',
|
Chris@0
|
391 count($nodes),
|
Chris@0
|
392 $this->getMatchingElementRepresentation($selectorType, $selector, count($nodes) !== 1),
|
Chris@0
|
393 $count
|
Chris@0
|
394 );
|
Chris@0
|
395
|
Chris@0
|
396 $this->assert(intval($count) === count($nodes), $message);
|
Chris@0
|
397 }
|
Chris@0
|
398
|
Chris@0
|
399 /**
|
Chris@0
|
400 * Checks that specific element exists on the current page.
|
Chris@0
|
401 *
|
Chris@0
|
402 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
403 * @param string|array $selector element selector
|
Chris@0
|
404 * @param ElementInterface $container document to check against
|
Chris@0
|
405 *
|
Chris@0
|
406 * @return NodeElement
|
Chris@0
|
407 *
|
Chris@0
|
408 * @throws ElementNotFoundException
|
Chris@0
|
409 */
|
Chris@0
|
410 public function elementExists($selectorType, $selector, ElementInterface $container = null)
|
Chris@0
|
411 {
|
Chris@0
|
412 $container = $container ?: $this->session->getPage();
|
Chris@0
|
413 $node = $container->find($selectorType, $selector);
|
Chris@0
|
414
|
Chris@0
|
415 if (null === $node) {
|
Chris@0
|
416 if (is_array($selector)) {
|
Chris@0
|
417 $selector = implode(' ', $selector);
|
Chris@0
|
418 }
|
Chris@0
|
419
|
Chris@0
|
420 throw new ElementNotFoundException($this->session->getDriver(), 'element', $selectorType, $selector);
|
Chris@0
|
421 }
|
Chris@0
|
422
|
Chris@0
|
423 return $node;
|
Chris@0
|
424 }
|
Chris@0
|
425
|
Chris@0
|
426 /**
|
Chris@0
|
427 * Checks that specific element does not exists on the current page.
|
Chris@0
|
428 *
|
Chris@0
|
429 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
430 * @param string|array $selector element selector
|
Chris@0
|
431 * @param ElementInterface $container document to check against
|
Chris@0
|
432 *
|
Chris@0
|
433 * @throws ExpectationException
|
Chris@0
|
434 */
|
Chris@0
|
435 public function elementNotExists($selectorType, $selector, ElementInterface $container = null)
|
Chris@0
|
436 {
|
Chris@0
|
437 $container = $container ?: $this->session->getPage();
|
Chris@0
|
438 $node = $container->find($selectorType, $selector);
|
Chris@0
|
439
|
Chris@0
|
440 $message = sprintf(
|
Chris@0
|
441 'An %s appears on this page, but it should not.',
|
Chris@0
|
442 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
443 );
|
Chris@0
|
444
|
Chris@0
|
445 $this->assert(null === $node, $message);
|
Chris@0
|
446 }
|
Chris@0
|
447
|
Chris@0
|
448 /**
|
Chris@0
|
449 * Checks that specific element contains text.
|
Chris@0
|
450 *
|
Chris@0
|
451 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
452 * @param string|array $selector element selector
|
Chris@0
|
453 * @param string $text expected text
|
Chris@0
|
454 *
|
Chris@0
|
455 * @throws ElementTextException
|
Chris@0
|
456 */
|
Chris@0
|
457 public function elementTextContains($selectorType, $selector, $text)
|
Chris@0
|
458 {
|
Chris@0
|
459 $element = $this->elementExists($selectorType, $selector);
|
Chris@0
|
460 $actual = $element->getText();
|
Chris@0
|
461 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
462
|
Chris@0
|
463 $message = sprintf(
|
Chris@0
|
464 'The text "%s" was not found in the text of the %s.',
|
Chris@0
|
465 $text,
|
Chris@0
|
466 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
467 );
|
Chris@0
|
468
|
Chris@0
|
469 $this->assertElementText((bool) preg_match($regex, $actual), $message, $element);
|
Chris@0
|
470 }
|
Chris@0
|
471
|
Chris@0
|
472 /**
|
Chris@0
|
473 * Checks that specific element does not contains text.
|
Chris@0
|
474 *
|
Chris@0
|
475 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
476 * @param string|array $selector element selector
|
Chris@0
|
477 * @param string $text expected text
|
Chris@0
|
478 *
|
Chris@0
|
479 * @throws ElementTextException
|
Chris@0
|
480 */
|
Chris@0
|
481 public function elementTextNotContains($selectorType, $selector, $text)
|
Chris@0
|
482 {
|
Chris@0
|
483 $element = $this->elementExists($selectorType, $selector);
|
Chris@0
|
484 $actual = $element->getText();
|
Chris@0
|
485 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
486
|
Chris@0
|
487 $message = sprintf(
|
Chris@0
|
488 'The text "%s" appears in the text of the %s, but it should not.',
|
Chris@0
|
489 $text,
|
Chris@0
|
490 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
491 );
|
Chris@0
|
492
|
Chris@0
|
493 $this->assertElementText(!preg_match($regex, $actual), $message, $element);
|
Chris@0
|
494 }
|
Chris@0
|
495
|
Chris@0
|
496 /**
|
Chris@0
|
497 * Checks that specific element contains HTML.
|
Chris@0
|
498 *
|
Chris@0
|
499 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
500 * @param string|array $selector element selector
|
Chris@0
|
501 * @param string $html expected text
|
Chris@0
|
502 *
|
Chris@0
|
503 * @throws ElementHtmlException
|
Chris@0
|
504 */
|
Chris@0
|
505 public function elementContains($selectorType, $selector, $html)
|
Chris@0
|
506 {
|
Chris@0
|
507 $element = $this->elementExists($selectorType, $selector);
|
Chris@0
|
508 $actual = $element->getHtml();
|
Chris@0
|
509 $regex = '/'.preg_quote($html, '/').'/ui';
|
Chris@0
|
510
|
Chris@0
|
511 $message = sprintf(
|
Chris@0
|
512 'The string "%s" was not found in the HTML of the %s.',
|
Chris@0
|
513 $html,
|
Chris@0
|
514 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
515 );
|
Chris@0
|
516
|
Chris@0
|
517 $this->assertElement((bool) preg_match($regex, $actual), $message, $element);
|
Chris@0
|
518 }
|
Chris@0
|
519
|
Chris@0
|
520 /**
|
Chris@0
|
521 * Checks that specific element does not contains HTML.
|
Chris@0
|
522 *
|
Chris@0
|
523 * @param string $selectorType element selector type (css, xpath)
|
Chris@0
|
524 * @param string|array $selector element selector
|
Chris@0
|
525 * @param string $html expected text
|
Chris@0
|
526 *
|
Chris@0
|
527 * @throws ElementHtmlException
|
Chris@0
|
528 */
|
Chris@0
|
529 public function elementNotContains($selectorType, $selector, $html)
|
Chris@0
|
530 {
|
Chris@0
|
531 $element = $this->elementExists($selectorType, $selector);
|
Chris@0
|
532 $actual = $element->getHtml();
|
Chris@0
|
533 $regex = '/'.preg_quote($html, '/').'/ui';
|
Chris@0
|
534
|
Chris@0
|
535 $message = sprintf(
|
Chris@0
|
536 'The string "%s" appears in the HTML of the %s, but it should not.',
|
Chris@0
|
537 $html,
|
Chris@0
|
538 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
539 );
|
Chris@0
|
540
|
Chris@0
|
541 $this->assertElement(!preg_match($regex, $actual), $message, $element);
|
Chris@0
|
542 }
|
Chris@0
|
543
|
Chris@0
|
544 /**
|
Chris@0
|
545 * Checks that an attribute exists in an element.
|
Chris@0
|
546 *
|
Chris@0
|
547 * @param string $selectorType
|
Chris@0
|
548 * @param string|array $selector
|
Chris@0
|
549 * @param string $attribute
|
Chris@0
|
550 *
|
Chris@0
|
551 * @return NodeElement
|
Chris@0
|
552 *
|
Chris@0
|
553 * @throws ElementHtmlException
|
Chris@0
|
554 */
|
Chris@0
|
555 public function elementAttributeExists($selectorType, $selector, $attribute)
|
Chris@0
|
556 {
|
Chris@0
|
557 $element = $this->elementExists($selectorType, $selector);
|
Chris@0
|
558
|
Chris@0
|
559 $message = sprintf(
|
Chris@0
|
560 'The attribute "%s" was not found in the %s.',
|
Chris@0
|
561 $attribute,
|
Chris@0
|
562 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
563 );
|
Chris@0
|
564
|
Chris@0
|
565 $this->assertElement($element->hasAttribute($attribute), $message, $element);
|
Chris@0
|
566
|
Chris@0
|
567 return $element;
|
Chris@0
|
568 }
|
Chris@0
|
569
|
Chris@0
|
570 /**
|
Chris@0
|
571 * Checks that an attribute of a specific elements contains text.
|
Chris@0
|
572 *
|
Chris@0
|
573 * @param string $selectorType
|
Chris@0
|
574 * @param string|array $selector
|
Chris@0
|
575 * @param string $attribute
|
Chris@0
|
576 * @param string $text
|
Chris@0
|
577 *
|
Chris@0
|
578 * @throws ElementHtmlException
|
Chris@0
|
579 */
|
Chris@0
|
580 public function elementAttributeContains($selectorType, $selector, $attribute, $text)
|
Chris@0
|
581 {
|
Chris@0
|
582 $element = $this->elementAttributeExists($selectorType, $selector, $attribute);
|
Chris@0
|
583 $actual = $element->getAttribute($attribute);
|
Chris@0
|
584 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
585
|
Chris@0
|
586 $message = sprintf(
|
Chris@0
|
587 'The text "%s" was not found in the attribute "%s" of the %s.',
|
Chris@0
|
588 $text,
|
Chris@0
|
589 $attribute,
|
Chris@0
|
590 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
591 );
|
Chris@0
|
592
|
Chris@0
|
593 $this->assertElement((bool) preg_match($regex, $actual), $message, $element);
|
Chris@0
|
594 }
|
Chris@0
|
595
|
Chris@0
|
596 /**
|
Chris@0
|
597 * Checks that an attribute of a specific elements does not contain text.
|
Chris@0
|
598 *
|
Chris@0
|
599 * @param string $selectorType
|
Chris@0
|
600 * @param string|array $selector
|
Chris@0
|
601 * @param string $attribute
|
Chris@0
|
602 * @param string $text
|
Chris@0
|
603 *
|
Chris@0
|
604 * @throws ElementHtmlException
|
Chris@0
|
605 */
|
Chris@0
|
606 public function elementAttributeNotContains($selectorType, $selector, $attribute, $text)
|
Chris@0
|
607 {
|
Chris@0
|
608 $element = $this->elementAttributeExists($selectorType, $selector, $attribute);
|
Chris@0
|
609 $actual = $element->getAttribute($attribute);
|
Chris@0
|
610 $regex = '/'.preg_quote($text, '/').'/ui';
|
Chris@0
|
611
|
Chris@0
|
612 $message = sprintf(
|
Chris@0
|
613 'The text "%s" was found in the attribute "%s" of the %s.',
|
Chris@0
|
614 $text,
|
Chris@0
|
615 $attribute,
|
Chris@0
|
616 $this->getMatchingElementRepresentation($selectorType, $selector)
|
Chris@0
|
617 );
|
Chris@0
|
618
|
Chris@0
|
619 $this->assertElement(!preg_match($regex, $actual), $message, $element);
|
Chris@0
|
620 }
|
Chris@0
|
621
|
Chris@0
|
622 /**
|
Chris@0
|
623 * Checks that specific field exists on the current page.
|
Chris@0
|
624 *
|
Chris@0
|
625 * @param string $field field id|name|label|value
|
Chris@0
|
626 * @param TraversableElement $container document to check against
|
Chris@0
|
627 *
|
Chris@0
|
628 * @return NodeElement
|
Chris@0
|
629 *
|
Chris@0
|
630 * @throws ElementNotFoundException
|
Chris@0
|
631 */
|
Chris@0
|
632 public function fieldExists($field, TraversableElement $container = null)
|
Chris@0
|
633 {
|
Chris@0
|
634 $container = $container ?: $this->session->getPage();
|
Chris@0
|
635 $node = $container->findField($field);
|
Chris@0
|
636
|
Chris@0
|
637 if (null === $node) {
|
Chris@0
|
638 throw new ElementNotFoundException($this->session->getDriver(), 'form field', 'id|name|label|value', $field);
|
Chris@0
|
639 }
|
Chris@0
|
640
|
Chris@0
|
641 return $node;
|
Chris@0
|
642 }
|
Chris@0
|
643
|
Chris@0
|
644 /**
|
Chris@0
|
645 * Checks that specific field does not exists on the current page.
|
Chris@0
|
646 *
|
Chris@0
|
647 * @param string $field field id|name|label|value
|
Chris@0
|
648 * @param TraversableElement $container document to check against
|
Chris@0
|
649 *
|
Chris@0
|
650 * @throws ExpectationException
|
Chris@0
|
651 */
|
Chris@0
|
652 public function fieldNotExists($field, TraversableElement $container = null)
|
Chris@0
|
653 {
|
Chris@0
|
654 $container = $container ?: $this->session->getPage();
|
Chris@0
|
655 $node = $container->findField($field);
|
Chris@0
|
656
|
Chris@0
|
657 $this->assert(null === $node, sprintf('A field "%s" appears on this page, but it should not.', $field));
|
Chris@0
|
658 }
|
Chris@0
|
659
|
Chris@0
|
660 /**
|
Chris@0
|
661 * Checks that specific field have provided value.
|
Chris@0
|
662 *
|
Chris@0
|
663 * @param string $field field id|name|label|value
|
Chris@0
|
664 * @param string $value field value
|
Chris@0
|
665 * @param TraversableElement $container document to check against
|
Chris@0
|
666 *
|
Chris@0
|
667 * @throws ExpectationException
|
Chris@0
|
668 */
|
Chris@0
|
669 public function fieldValueEquals($field, $value, TraversableElement $container = null)
|
Chris@0
|
670 {
|
Chris@0
|
671 $node = $this->fieldExists($field, $container);
|
Chris@0
|
672 $actual = $node->getValue();
|
Chris@0
|
673 $regex = '/^'.preg_quote($value, '/').'$/ui';
|
Chris@0
|
674
|
Chris@0
|
675 $message = sprintf('The field "%s" value is "%s", but "%s" expected.', $field, $actual, $value);
|
Chris@0
|
676
|
Chris@0
|
677 $this->assert((bool) preg_match($regex, $actual), $message);
|
Chris@0
|
678 }
|
Chris@0
|
679
|
Chris@0
|
680 /**
|
Chris@0
|
681 * Checks that specific field have provided value.
|
Chris@0
|
682 *
|
Chris@0
|
683 * @param string $field field id|name|label|value
|
Chris@0
|
684 * @param string $value field value
|
Chris@0
|
685 * @param TraversableElement $container document to check against
|
Chris@0
|
686 *
|
Chris@0
|
687 * @throws ExpectationException
|
Chris@0
|
688 */
|
Chris@0
|
689 public function fieldValueNotEquals($field, $value, TraversableElement $container = null)
|
Chris@0
|
690 {
|
Chris@0
|
691 $node = $this->fieldExists($field, $container);
|
Chris@0
|
692 $actual = $node->getValue();
|
Chris@0
|
693 $regex = '/^'.preg_quote($value, '/').'$/ui';
|
Chris@0
|
694
|
Chris@0
|
695 $message = sprintf('The field "%s" value is "%s", but it should not be.', $field, $actual);
|
Chris@0
|
696
|
Chris@0
|
697 $this->assert(!preg_match($regex, $actual), $message);
|
Chris@0
|
698 }
|
Chris@0
|
699
|
Chris@0
|
700 /**
|
Chris@0
|
701 * Checks that specific checkbox is checked.
|
Chris@0
|
702 *
|
Chris@0
|
703 * @param string $field field id|name|label|value
|
Chris@0
|
704 * @param TraversableElement $container document to check against
|
Chris@0
|
705 *
|
Chris@0
|
706 * @throws ExpectationException
|
Chris@0
|
707 */
|
Chris@0
|
708 public function checkboxChecked($field, TraversableElement $container = null)
|
Chris@0
|
709 {
|
Chris@0
|
710 $node = $this->fieldExists($field, $container);
|
Chris@0
|
711
|
Chris@0
|
712 $this->assert($node->isChecked(), sprintf('Checkbox "%s" is not checked, but it should be.', $field));
|
Chris@0
|
713 }
|
Chris@0
|
714
|
Chris@0
|
715 /**
|
Chris@0
|
716 * Checks that specific checkbox is unchecked.
|
Chris@0
|
717 *
|
Chris@0
|
718 * @param string $field field id|name|label|value
|
Chris@0
|
719 * @param TraversableElement $container document to check against
|
Chris@0
|
720 *
|
Chris@0
|
721 * @throws ExpectationException
|
Chris@0
|
722 */
|
Chris@0
|
723 public function checkboxNotChecked($field, TraversableElement $container = null)
|
Chris@0
|
724 {
|
Chris@0
|
725 $node = $this->fieldExists($field, $container);
|
Chris@0
|
726
|
Chris@0
|
727 $this->assert(!$node->isChecked(), sprintf('Checkbox "%s" is checked, but it should not be.', $field));
|
Chris@0
|
728 }
|
Chris@0
|
729
|
Chris@0
|
730 /**
|
Chris@0
|
731 * Gets current url of the page.
|
Chris@0
|
732 *
|
Chris@0
|
733 * @return string
|
Chris@0
|
734 */
|
Chris@0
|
735 protected function getCurrentUrlPath()
|
Chris@0
|
736 {
|
Chris@0
|
737 return $this->cleanUrl($this->session->getCurrentUrl());
|
Chris@0
|
738 }
|
Chris@0
|
739
|
Chris@0
|
740 /**
|
Chris@0
|
741 * Trims scriptname from the URL.
|
Chris@0
|
742 *
|
Chris@0
|
743 * @param string $url
|
Chris@0
|
744 *
|
Chris@0
|
745 * @return string
|
Chris@0
|
746 */
|
Chris@0
|
747 protected function cleanUrl($url)
|
Chris@0
|
748 {
|
Chris@0
|
749 $parts = parse_url($url);
|
Chris@0
|
750 $fragment = empty($parts['fragment']) ? '' : '#'.$parts['fragment'];
|
Chris@0
|
751 $path = empty($parts['path']) ? '/' : $parts['path'];
|
Chris@0
|
752
|
Chris@0
|
753 return preg_replace('/^\/[^\.\/]+\.php\//', '/', $path).$fragment;
|
Chris@0
|
754 }
|
Chris@0
|
755
|
Chris@0
|
756 /**
|
Chris@0
|
757 * Asserts a condition.
|
Chris@0
|
758 *
|
Chris@0
|
759 * @param bool $condition
|
Chris@0
|
760 * @param string $message Failure message
|
Chris@0
|
761 *
|
Chris@0
|
762 * @throws ExpectationException when the condition is not fulfilled
|
Chris@0
|
763 */
|
Chris@0
|
764 private function assert($condition, $message)
|
Chris@0
|
765 {
|
Chris@0
|
766 if ($condition) {
|
Chris@0
|
767 return;
|
Chris@0
|
768 }
|
Chris@0
|
769
|
Chris@0
|
770 throw new ExpectationException($message, $this->session->getDriver());
|
Chris@0
|
771 }
|
Chris@0
|
772
|
Chris@0
|
773 /**
|
Chris@0
|
774 * Asserts a condition involving the response text.
|
Chris@0
|
775 *
|
Chris@0
|
776 * @param bool $condition
|
Chris@0
|
777 * @param string $message Failure message
|
Chris@0
|
778 *
|
Chris@0
|
779 * @throws ResponseTextException when the condition is not fulfilled
|
Chris@0
|
780 */
|
Chris@0
|
781 private function assertResponseText($condition, $message)
|
Chris@0
|
782 {
|
Chris@0
|
783 if ($condition) {
|
Chris@0
|
784 return;
|
Chris@0
|
785 }
|
Chris@0
|
786
|
Chris@0
|
787 throw new ResponseTextException($message, $this->session->getDriver());
|
Chris@0
|
788 }
|
Chris@0
|
789
|
Chris@0
|
790 /**
|
Chris@0
|
791 * Asserts a condition on an element.
|
Chris@0
|
792 *
|
Chris@0
|
793 * @param bool $condition
|
Chris@0
|
794 * @param string $message Failure message
|
Chris@0
|
795 * @param Element $element
|
Chris@0
|
796 *
|
Chris@0
|
797 * @throws ElementHtmlException when the condition is not fulfilled
|
Chris@0
|
798 */
|
Chris@0
|
799 private function assertElement($condition, $message, Element $element)
|
Chris@0
|
800 {
|
Chris@0
|
801 if ($condition) {
|
Chris@0
|
802 return;
|
Chris@0
|
803 }
|
Chris@0
|
804
|
Chris@0
|
805 throw new ElementHtmlException($message, $this->session->getDriver(), $element);
|
Chris@0
|
806 }
|
Chris@0
|
807
|
Chris@0
|
808 /**
|
Chris@0
|
809 * Asserts a condition involving the text of an element.
|
Chris@0
|
810 *
|
Chris@0
|
811 * @param bool $condition
|
Chris@0
|
812 * @param string $message Failure message
|
Chris@0
|
813 * @param Element $element
|
Chris@0
|
814 *
|
Chris@0
|
815 * @throws ElementTextException when the condition is not fulfilled
|
Chris@0
|
816 */
|
Chris@0
|
817 private function assertElementText($condition, $message, Element $element)
|
Chris@0
|
818 {
|
Chris@0
|
819 if ($condition) {
|
Chris@0
|
820 return;
|
Chris@0
|
821 }
|
Chris@0
|
822
|
Chris@0
|
823 throw new ElementTextException($message, $this->session->getDriver(), $element);
|
Chris@0
|
824 }
|
Chris@0
|
825
|
Chris@0
|
826 /**
|
Chris@0
|
827 * @param string $selectorType
|
Chris@0
|
828 * @param string|array $selector
|
Chris@0
|
829 * @param bool $plural
|
Chris@0
|
830 *
|
Chris@0
|
831 * @return string
|
Chris@0
|
832 */
|
Chris@0
|
833 private function getMatchingElementRepresentation($selectorType, $selector, $plural = false)
|
Chris@0
|
834 {
|
Chris@0
|
835 $pluralization = $plural ? 's' : '';
|
Chris@0
|
836
|
Chris@0
|
837 if (in_array($selectorType, array('named', 'named_exact', 'named_partial'))
|
Chris@0
|
838 && is_array($selector) && 2 === count($selector)
|
Chris@0
|
839 ) {
|
Chris@0
|
840 return sprintf('%s%s matching locator "%s"', $selector[0], $pluralization, $selector[1]);
|
Chris@0
|
841 }
|
Chris@0
|
842
|
Chris@0
|
843 if (is_array($selector)) {
|
Chris@0
|
844 $selector = implode(' ', $selector);
|
Chris@0
|
845 }
|
Chris@0
|
846
|
Chris@0
|
847 return sprintf('element%s matching %s "%s"', $pluralization, $selectorType, $selector);
|
Chris@0
|
848 }
|
Chris@0
|
849 }
|