3 namespace Drupal\Tests;
5 use Behat\Mink\Exception\ExpectationException;
6 use Behat\Mink\Exception\ResponseTextException;
7 use Behat\Mink\WebAssert as MinkWebAssert;
8 use Behat\Mink\Element\TraversableElement;
9 use Behat\Mink\Exception\ElementNotFoundException;
10 use Behat\Mink\Session;
11 use Drupal\Component\Utility\Html;
15 * Defines a class with methods for asserting presence of elements during tests.
17 class WebAssert extends MinkWebAssert {
20 * The absolute URL of the site under test.
24 protected $baseUrl = '';
29 * @param \Behat\Mink\Session $session
30 * The Behat session object;
31 * @param string $base_url
32 * The base URL of the site under test.
34 public function __construct(Session $session, $base_url = '') {
35 parent::__construct($session);
36 $this->baseUrl = $base_url;
42 protected function cleanUrl($url) {
43 if ($url instanceof Url) {
44 $url = $url->setAbsolute()->toString();
46 // Strip the base URL from the beginning for absolute URLs.
47 if ($this->baseUrl !== '' && strpos($url, $this->baseUrl) === 0) {
48 $url = substr($url, strlen($this->baseUrl));
50 // Make sure there is a forward slash at the beginning of relative URLs for
52 if (parse_url($url, PHP_URL_HOST) === NULL && strpos($url, '/') !== 0) {
55 return parent::cleanUrl($url);
59 * Checks that specific button exists on the current page.
61 * @param string $button
62 * One of id|name|label|value for the button.
63 * @param \Behat\Mink\Element\TraversableElement $container
64 * (optional) The document to check against. Defaults to the current page.
66 * @return \Behat\Mink\Element\NodeElement
67 * The matching element.
69 * @throws \Behat\Mink\Exception\ElementNotFoundException
70 * When the element doesn't exist.
72 public function buttonExists($button, TraversableElement $container = NULL) {
73 $container = $container ?: $this->session->getPage();
74 $node = $container->findButton($button);
77 throw new ElementNotFoundException($this->session, 'button', 'id|name|label|value', $button);
84 * Checks that the specific button does NOT exist on the current page.
86 * @param string $button
87 * One of id|name|label|value for the button.
88 * @param \Behat\Mink\Element\TraversableElement $container
89 * (optional) The document to check against. Defaults to the current page.
91 * @throws \Behat\Mink\Exception\ExpectationException
92 * When the button exists.
94 public function buttonNotExists($button, TraversableElement $container = NULL) {
95 $container = $container ?: $this->session->getPage();
96 $node = $container->findButton($button);
98 $this->assert(NULL === $node, sprintf('A button "%s" appears on this page, but it should not.', $button));
102 * Checks that specific select field exists on the current page.
104 * @param string $select
105 * One of id|name|label|value for the select field.
106 * @param \Behat\Mink\Element\TraversableElement $container
107 * (optional) The document to check against. Defaults to the current page.
109 * @return \Behat\Mink\Element\NodeElement
110 * The matching element
112 * @throws \Behat\Mink\Exception\ElementNotFoundException
113 * When the element doesn't exist.
115 public function selectExists($select, TraversableElement $container = NULL) {
116 $container = $container ?: $this->session->getPage();
117 $node = $container->find('named', [
122 if ($node === NULL) {
123 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
130 * Checks that specific option in a select field exists on the current page.
132 * @param string $select
133 * One of id|name|label|value for the select field.
134 * @param string $option
136 * @param \Behat\Mink\Element\TraversableElement $container
137 * (optional) The document to check against. Defaults to the current page.
139 * @return \Behat\Mink\Element\NodeElement
140 * The matching option element
142 * @throws \Behat\Mink\Exception\ElementNotFoundException
143 * When the element doesn't exist.
145 public function optionExists($select, $option, TraversableElement $container = NULL) {
146 $container = $container ?: $this->session->getPage();
147 $select_field = $container->find('named', [
152 if ($select_field === NULL) {
153 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
156 $option_field = $select_field->find('named', ['option', $option]);
158 if ($option_field === NULL) {
159 throw new ElementNotFoundException($this->session->getDriver(), 'select', 'id|name|label|value', $option);
162 return $option_field;
166 * Checks that an option in a select field does NOT exist on the current page.
168 * @param string $select
169 * One of id|name|label|value for the select field.
170 * @param string $option
171 * The option value that should not exist.
172 * @param \Behat\Mink\Element\TraversableElement $container
173 * (optional) The document to check against. Defaults to the current page.
175 * @throws \Behat\Mink\Exception\ElementNotFoundException
176 * When the select element doesn't exist.
178 public function optionNotExists($select, $option, TraversableElement $container = NULL) {
179 $container = $container ?: $this->session->getPage();
180 $select_field = $container->find('named', [
185 if ($select_field === NULL) {
186 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
189 $option_field = $select_field->find('named', ['option', $option]);
191 $this->assert($option_field === NULL, sprintf('An option "%s" exists in select "%s", but it should not.', $option, $select));
195 * Pass if the page title is the given string.
197 * @param string $expected_title
198 * The string the page title should be.
200 * @throws \Behat\Mink\Exception\ExpectationException
201 * Thrown when element doesn't exist, or the title is a different one.
203 public function titleEquals($expected_title) {
204 $title_element = $this->session->getPage()->find('css', 'title');
205 if (!$title_element) {
206 throw new ExpectationException('No title element found on the page', $this->session->getDriver());
208 $actual_title = $title_element->getText();
209 $this->assert($expected_title === $actual_title, 'Title found');
213 * Passes if a link with the specified label is found.
215 * An optional link index may be passed.
217 * @param string $label
218 * Text between the anchor tags.
220 * Link position counting from zero.
221 * @param string $message
222 * (optional) A message to display with the assertion. Do not translate
223 * messages: use strtr() to embed variables in the message text, not
224 * t(). If left blank, a default message will be displayed.
226 * @throws \Behat\Mink\Exception\ExpectationException
227 * Thrown when element doesn't exist, or the link label is a different one.
229 public function linkExists($label, $index = 0, $message = '') {
230 $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label]));
231 $links = $this->session->getPage()->findAll('named', ['link', $label]);
232 $this->assert(!empty($links[$index]), $message);
236 * Passes if a link with the exactly specified label is found.
238 * An optional link index may be passed.
240 * @param string $label
241 * Text between the anchor tags.
243 * Link position counting from zero.
244 * @param string $message
245 * (optional) A message to display with the assertion. Do not translate
246 * messages: use strtr() to embed variables in the message text, not
247 * t(). If left blank, a default message will be displayed.
249 * @throws \Behat\Mink\Exception\ExpectationException
250 * Thrown when element doesn't exist, or the link label is a different one.
252 public function linkExistsExact($label, $index = 0, $message = '') {
253 $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label]));
254 $links = $this->session->getPage()->findAll('named_exact', ['link', $label]);
255 $this->assert(!empty($links[$index]), $message);
259 * Passes if a link with the specified label is not found.
261 * An optional link index may be passed.
263 * @param string $label
264 * Text between the anchor tags.
265 * @param string $message
266 * (optional) A message to display with the assertion. Do not translate
267 * messages: use strtr() to embed variables in the message text, not
268 * t(). If left blank, a default message will be displayed.
270 * @throws \Behat\Mink\Exception\ExpectationException
271 * Thrown when element doesn't exist, or the link label is a different one.
273 public function linkNotExists($label, $message = '') {
274 $message = ($message ? $message : strtr('Link with label %label not found.', ['%label' => $label]));
275 $links = $this->session->getPage()->findAll('named', ['link', $label]);
276 $this->assert(empty($links), $message);
280 * Passes if a link with the exactly specified label is not found.
282 * An optional link index may be passed.
284 * @param string $label
285 * Text between the anchor tags.
286 * @param string $message
287 * (optional) A message to display with the assertion. Do not translate
288 * messages: use strtr() to embed variables in the message text, not
289 * t(). If left blank, a default message will be displayed.
291 * @throws \Behat\Mink\Exception\ExpectationException
292 * Thrown when element doesn't exist, or the link label is a different one.
294 public function linkNotExistsExact($label, $message = '') {
295 $message = ($message ? $message : strtr('Link with label %label not found.', ['%label' => $label]));
296 $links = $this->session->getPage()->findAll('named_exact', ['link', $label]);
297 $this->assert(empty($links), $message);
301 * Passes if a link containing a given href (part) is found.
303 * @param string $href
304 * The full or partial value of the 'href' attribute of the anchor tag.
306 * Link position counting from zero.
307 * @param string $message
308 * (optional) A message to display with the assertion. Do not translate
309 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
310 * variables in the message text, not t(). If left blank, a default message
313 * @throws \Behat\Mink\Exception\ExpectationException
314 * Thrown when element doesn't exist, or the link label is a different one.
316 public function linkByHrefExists($href, $index = 0, $message = '') {
317 $xpath = $this->buildXPathQuery('//a[contains(@href, :href)]', [':href' => $href]);
318 $message = ($message ? $message : strtr('Link containing href %href found.', ['%href' => $href]));
319 $links = $this->session->getPage()->findAll('xpath', $xpath);
320 $this->assert(!empty($links[$index]), $message);
324 * Passes if a link containing a given href (part) is not found.
326 * @param string $href
327 * The full or partial value of the 'href' attribute of the anchor tag.
328 * @param string $message
329 * (optional) A message to display with the assertion. Do not translate
330 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
331 * variables in the message text, not t(). If left blank, a default message
334 * @throws \Behat\Mink\Exception\ExpectationException
335 * Thrown when element doesn't exist, or the link label is a different one.
337 public function linkByHrefNotExists($href, $message = '') {
338 $xpath = $this->buildXPathQuery('//a[contains(@href, :href)]', [':href' => $href]);
339 $message = ($message ? $message : strtr('No link containing href %href found.', ['%href' => $href]));
340 $links = $this->session->getPage()->findAll('xpath', $xpath);
341 $this->assert(empty($links), $message);
345 * Builds an XPath query.
347 * Builds an XPath query by replacing placeholders in the query by the value
350 * XPath 1.0 (the version supported by libxml2, the underlying XML library
351 * used by PHP) doesn't support any form of quotation. This function
352 * simplifies the building of XPath expression.
354 * @param string $xpath
355 * An XPath query, possibly with placeholders in the form ':name'.
357 * An array of arguments with keys in the form ':name' matching the
358 * placeholders in the query. The values may be either strings or numeric
362 * An XPath query with arguments replaced.
364 public function buildXPathQuery($xpath, array $args = []) {
365 // Replace placeholders.
366 foreach ($args as $placeholder => $value) {
367 if (is_object($value)) {
368 throw new \InvalidArgumentException('Just pass in scalar values for $args and remove all t() calls from your test.');
370 // XPath 1.0 doesn't support a way to escape single or double quotes in a
371 // string literal. We split double quotes out of the string, and encode
373 if (is_string($value)) {
374 // Explode the text at the quote characters.
375 $parts = explode('"', $value);
378 foreach ($parts as &$part) {
379 $part = '"' . $part . '"';
382 // Return the string.
383 $value = count($parts) > 1 ? 'concat(' . implode(', \'"\', ', $parts) . ')' : $parts[0];
386 // Use preg_replace_callback() instead of preg_replace() to prevent the
387 // regular expression engine from trying to substitute backreferences.
388 $replacement = function ($matches) use ($value) {
391 $xpath = preg_replace_callback('/' . preg_quote($placeholder) . '\b/', $replacement, $xpath);
397 * Passes if the raw text IS NOT found escaped on the loaded page.
399 * Raw text refers to the raw HTML that the page generated.
402 * Raw (HTML) string to look for.
404 public function assertNoEscaped($raw) {
405 $this->responseNotContains(Html::escape($raw));
409 * Passes if the raw text IS found escaped on the loaded page.
411 * Raw text refers to the raw HTML that the page generated.
414 * Raw (HTML) string to look for.
416 public function assertEscaped($raw) {
417 $this->responseContains(Html::escape($raw));
421 * Asserts a condition.
423 * The parent method is overridden because it is a private method.
425 * @param bool $condition
427 * @param string $message
428 * The success message.
430 * @throws \Behat\Mink\Exception\ExpectationException
431 * When the condition is not fulfilled.
433 public function assert($condition, $message) {
438 throw new ExpectationException($message, $this->session->getDriver());
442 * Checks that a given form field element is disabled.
444 * @param string $field
445 * One of id|name|label|value for the field.
446 * @param \Behat\Mink\Element\TraversableElement $container
447 * (optional) The document to check against. Defaults to the current page.
449 * @return \Behat\Mink\Element\NodeElement
450 * The matching element.
452 * @throws \Behat\Mink\Exception\ElementNotFoundException
453 * @throws \Behat\Mink\Exception\ExpectationException
455 public function fieldDisabled($field, TraversableElement $container = NULL) {
456 $container = $container ?: $this->session->getPage();
457 $node = $container->findField($field);
459 if ($node === NULL) {
460 throw new ElementNotFoundException($this->session->getDriver(), 'field', 'id|name|label|value', $field);
463 if (!$node->hasAttribute('disabled')) {
464 throw new ExpectationException("Field $field is disabled", $this->session->getDriver());
471 * Checks that specific hidden field exists.
473 * @param string $field
474 * One of id|name|value for the hidden field.
475 * @param \Behat\Mink\Element\TraversableElement $container
476 * (optional) The document to check against. Defaults to the current page.
478 * @return \Behat\Mink\Element\NodeElement
479 * The matching element.
481 * @throws \Behat\Mink\Exception\ElementNotFoundException
483 public function hiddenFieldExists($field, TraversableElement $container = NULL) {
484 $container = $container ?: $this->session->getPage();
485 if ($node = $container->find('hidden_field_selector', ['hidden_field', $field])) {
488 throw new ElementNotFoundException($this->session->getDriver(), 'form hidden field', 'id|name|value', $field);
492 * Checks that specific hidden field does not exist.
494 * @param string $field
495 * One of id|name|value for the hidden field.
496 * @param \Behat\Mink\Element\TraversableElement $container
497 * (optional) The document to check against. Defaults to the current page.
499 * @throws \Behat\Mink\Exception\ExpectationException
501 public function hiddenFieldNotExists($field, TraversableElement $container = NULL) {
502 $container = $container ?: $this->session->getPage();
503 $node = $container->find('hidden_field_selector', ['hidden_field', $field]);
504 $this->assert($node === NULL, "A hidden field '$field' exists on this page, but it should not.");
508 * Checks that specific hidden field have provided value.
510 * @param string $field
511 * One of id|name|value for the hidden field.
512 * @param string $value
513 * The hidden field value that needs to be checked.
514 * @param \Behat\Mink\Element\TraversableElement $container
515 * (optional) The document to check against. Defaults to the current page.
517 * @throws \Behat\Mink\Exception\ElementNotFoundException
518 * @throws \Behat\Mink\Exception\ExpectationException
520 public function hiddenFieldValueEquals($field, $value, TraversableElement $container = NULL) {
521 $node = $this->hiddenFieldExists($field, $container);
522 $actual = $node->getValue();
523 $regex = '/^' . preg_quote($value, '/') . '$/ui';
524 $message = "The hidden field '$field' value is '$actual', but '$value' expected.";
525 $this->assert((bool) preg_match($regex, $actual), $message);
529 * Checks that specific hidden field doesn't have the provided value.
531 * @param string $field
532 * One of id|name|value for the hidden field.
533 * @param string $value
534 * The hidden field value that needs to be checked.
535 * @param \Behat\Mink\Element\TraversableElement $container
536 * (optional) The document to check against. Defaults to the current page.
538 * @throws \Behat\Mink\Exception\ElementNotFoundException
539 * @throws \Behat\Mink\Exception\ExpectationException
541 public function hiddenFieldValueNotEquals($field, $value, TraversableElement $container = NULL) {
542 $node = $this->hiddenFieldExists($field, $container);
543 $actual = $node->getValue();
544 $regex = '/^' . preg_quote($value, '/') . '$/ui';
545 $message = "The hidden field '$field' value is '$actual', but it should not be.";
546 $this->assert(!preg_match($regex, $actual), $message);
550 * Checks that current page contains text only once.
552 * @param string $text
553 * The string to look for.
555 * @see \Behat\Mink\WebAssert::pageTextContains()
557 public function pageTextContainsOnce($text) {
558 $actual = $this->session->getPage()->getText();
559 $actual = preg_replace('/\s+/u', ' ', $actual);
560 $regex = '/' . preg_quote($text, '/') . '/ui';
561 $count = preg_match_all($regex, $actual);
567 $message = sprintf('The text "%s" appears in the text of this page more than once, but it should not.', $text);
570 $message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text);
573 throw new ResponseTextException($message, $this->session->getDriver());