annotate core/tests/Drupal/Tests/WebAssert.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests;
Chris@0 4
Chris@0 5 use Behat\Mink\Exception\ExpectationException;
Chris@17 6 use Behat\Mink\Exception\ResponseTextException;
Chris@0 7 use Behat\Mink\WebAssert as MinkWebAssert;
Chris@0 8 use Behat\Mink\Element\TraversableElement;
Chris@0 9 use Behat\Mink\Exception\ElementNotFoundException;
Chris@0 10 use Behat\Mink\Session;
Chris@0 11 use Drupal\Component\Utility\Html;
Chris@0 12 use Drupal\Core\Url;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Defines a class with methods for asserting presence of elements during tests.
Chris@0 16 */
Chris@0 17 class WebAssert extends MinkWebAssert {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The absolute URL of the site under test.
Chris@0 21 *
Chris@0 22 * @var string
Chris@0 23 */
Chris@0 24 protected $baseUrl = '';
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Constructor.
Chris@0 28 *
Chris@0 29 * @param \Behat\Mink\Session $session
Chris@0 30 * The Behat session object;
Chris@0 31 * @param string $base_url
Chris@0 32 * The base URL of the site under test.
Chris@0 33 */
Chris@0 34 public function __construct(Session $session, $base_url = '') {
Chris@0 35 parent::__construct($session);
Chris@0 36 $this->baseUrl = $base_url;
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * {@inheritdoc}
Chris@0 41 */
Chris@0 42 protected function cleanUrl($url) {
Chris@0 43 if ($url instanceof Url) {
Chris@0 44 $url = $url->setAbsolute()->toString();
Chris@0 45 }
Chris@0 46 // Strip the base URL from the beginning for absolute URLs.
Chris@0 47 if ($this->baseUrl !== '' && strpos($url, $this->baseUrl) === 0) {
Chris@0 48 $url = substr($url, strlen($this->baseUrl));
Chris@0 49 }
Chris@0 50 // Make sure there is a forward slash at the beginning of relative URLs for
Chris@0 51 // consistency.
Chris@0 52 if (parse_url($url, PHP_URL_HOST) === NULL && strpos($url, '/') !== 0) {
Chris@0 53 $url = "/$url";
Chris@0 54 }
Chris@0 55 return parent::cleanUrl($url);
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Checks that specific button exists on the current page.
Chris@0 60 *
Chris@0 61 * @param string $button
Chris@0 62 * One of id|name|label|value for the button.
Chris@0 63 * @param \Behat\Mink\Element\TraversableElement $container
Chris@0 64 * (optional) The document to check against. Defaults to the current page.
Chris@0 65 *
Chris@0 66 * @return \Behat\Mink\Element\NodeElement
Chris@0 67 * The matching element.
Chris@0 68 *
Chris@0 69 * @throws \Behat\Mink\Exception\ElementNotFoundException
Chris@0 70 * When the element doesn't exist.
Chris@0 71 */
Chris@0 72 public function buttonExists($button, TraversableElement $container = NULL) {
Chris@0 73 $container = $container ?: $this->session->getPage();
Chris@0 74 $node = $container->findButton($button);
Chris@0 75
Chris@0 76 if ($node === NULL) {
Chris@0 77 throw new ElementNotFoundException($this->session, 'button', 'id|name|label|value', $button);
Chris@0 78 }
Chris@0 79
Chris@0 80 return $node;
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Checks that the specific button does NOT exist on the current page.
Chris@0 85 *
Chris@0 86 * @param string $button
Chris@0 87 * One of id|name|label|value for the button.
Chris@0 88 * @param \Behat\Mink\Element\TraversableElement $container
Chris@0 89 * (optional) The document to check against. Defaults to the current page.
Chris@0 90 *
Chris@0 91 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 92 * When the button exists.
Chris@0 93 */
Chris@0 94 public function buttonNotExists($button, TraversableElement $container = NULL) {
Chris@0 95 $container = $container ?: $this->session->getPage();
Chris@0 96 $node = $container->findButton($button);
Chris@0 97
Chris@0 98 $this->assert(NULL === $node, sprintf('A button "%s" appears on this page, but it should not.', $button));
Chris@0 99 }
Chris@0 100
Chris@0 101 /**
Chris@0 102 * Checks that specific select field exists on the current page.
Chris@0 103 *
Chris@0 104 * @param string $select
Chris@0 105 * One of id|name|label|value for the select field.
Chris@0 106 * @param \Behat\Mink\Element\TraversableElement $container
Chris@0 107 * (optional) The document to check against. Defaults to the current page.
Chris@0 108 *
Chris@0 109 * @return \Behat\Mink\Element\NodeElement
Chris@0 110 * The matching element
Chris@0 111 *
Chris@0 112 * @throws \Behat\Mink\Exception\ElementNotFoundException
Chris@0 113 * When the element doesn't exist.
Chris@0 114 */
Chris@0 115 public function selectExists($select, TraversableElement $container = NULL) {
Chris@0 116 $container = $container ?: $this->session->getPage();
Chris@0 117 $node = $container->find('named', [
Chris@0 118 'select',
Chris@17 119 $select,
Chris@0 120 ]);
Chris@0 121
Chris@0 122 if ($node === NULL) {
Chris@0 123 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
Chris@0 124 }
Chris@0 125
Chris@0 126 return $node;
Chris@0 127 }
Chris@0 128
Chris@0 129 /**
Chris@0 130 * Checks that specific option in a select field exists on the current page.
Chris@0 131 *
Chris@0 132 * @param string $select
Chris@0 133 * One of id|name|label|value for the select field.
Chris@0 134 * @param string $option
Chris@0 135 * The option value.
Chris@0 136 * @param \Behat\Mink\Element\TraversableElement $container
Chris@0 137 * (optional) The document to check against. Defaults to the current page.
Chris@0 138 *
Chris@0 139 * @return \Behat\Mink\Element\NodeElement
Chris@0 140 * The matching option element
Chris@0 141 *
Chris@0 142 * @throws \Behat\Mink\Exception\ElementNotFoundException
Chris@0 143 * When the element doesn't exist.
Chris@0 144 */
Chris@0 145 public function optionExists($select, $option, TraversableElement $container = NULL) {
Chris@0 146 $container = $container ?: $this->session->getPage();
Chris@0 147 $select_field = $container->find('named', [
Chris@0 148 'select',
Chris@17 149 $select,
Chris@0 150 ]);
Chris@0 151
Chris@0 152 if ($select_field === NULL) {
Chris@0 153 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
Chris@0 154 }
Chris@0 155
Chris@0 156 $option_field = $select_field->find('named', ['option', $option]);
Chris@0 157
Chris@0 158 if ($option_field === NULL) {
Chris@17 159 throw new ElementNotFoundException($this->session->getDriver(), 'select', 'id|name|label|value', $option);
Chris@0 160 }
Chris@0 161
Chris@0 162 return $option_field;
Chris@0 163 }
Chris@0 164
Chris@0 165 /**
Chris@0 166 * Checks that an option in a select field does NOT exist on the current page.
Chris@0 167 *
Chris@0 168 * @param string $select
Chris@0 169 * One of id|name|label|value for the select field.
Chris@0 170 * @param string $option
Chris@17 171 * The option value that should not exist.
Chris@0 172 * @param \Behat\Mink\Element\TraversableElement $container
Chris@0 173 * (optional) The document to check against. Defaults to the current page.
Chris@0 174 *
Chris@0 175 * @throws \Behat\Mink\Exception\ElementNotFoundException
Chris@0 176 * When the select element doesn't exist.
Chris@0 177 */
Chris@0 178 public function optionNotExists($select, $option, TraversableElement $container = NULL) {
Chris@0 179 $container = $container ?: $this->session->getPage();
Chris@0 180 $select_field = $container->find('named', [
Chris@0 181 'select',
Chris@17 182 $select,
Chris@0 183 ]);
Chris@0 184
Chris@0 185 if ($select_field === NULL) {
Chris@0 186 throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select);
Chris@0 187 }
Chris@0 188
Chris@0 189 $option_field = $select_field->find('named', ['option', $option]);
Chris@0 190
Chris@0 191 $this->assert($option_field === NULL, sprintf('An option "%s" exists in select "%s", but it should not.', $option, $select));
Chris@0 192 }
Chris@0 193
Chris@0 194 /**
Chris@0 195 * Pass if the page title is the given string.
Chris@0 196 *
Chris@0 197 * @param string $expected_title
Chris@0 198 * The string the page title should be.
Chris@0 199 *
Chris@0 200 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 201 * Thrown when element doesn't exist, or the title is a different one.
Chris@0 202 */
Chris@0 203 public function titleEquals($expected_title) {
Chris@0 204 $title_element = $this->session->getPage()->find('css', 'title');
Chris@0 205 if (!$title_element) {
Chris@16 206 throw new ExpectationException('No title element found on the page', $this->session->getDriver());
Chris@0 207 }
Chris@0 208 $actual_title = $title_element->getText();
Chris@0 209 $this->assert($expected_title === $actual_title, 'Title found');
Chris@0 210 }
Chris@0 211
Chris@0 212 /**
Chris@0 213 * Passes if a link with the specified label is found.
Chris@0 214 *
Chris@0 215 * An optional link index may be passed.
Chris@0 216 *
Chris@0 217 * @param string $label
Chris@0 218 * Text between the anchor tags.
Chris@0 219 * @param int $index
Chris@0 220 * Link position counting from zero.
Chris@0 221 * @param string $message
Chris@0 222 * (optional) A message to display with the assertion. Do not translate
Chris@0 223 * messages: use strtr() to embed variables in the message text, not
Chris@0 224 * t(). If left blank, a default message will be displayed.
Chris@0 225 *
Chris@0 226 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 227 * Thrown when element doesn't exist, or the link label is a different one.
Chris@0 228 */
Chris@0 229 public function linkExists($label, $index = 0, $message = '') {
Chris@0 230 $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label]));
Chris@0 231 $links = $this->session->getPage()->findAll('named', ['link', $label]);
Chris@0 232 $this->assert(!empty($links[$index]), $message);
Chris@0 233 }
Chris@0 234
Chris@0 235 /**
Chris@0 236 * Passes if a link with the exactly specified label is found.
Chris@0 237 *
Chris@0 238 * An optional link index may be passed.
Chris@0 239 *
Chris@0 240 * @param string $label
Chris@0 241 * Text between the anchor tags.
Chris@0 242 * @param int $index
Chris@0 243 * Link position counting from zero.
Chris@0 244 * @param string $message
Chris@0 245 * (optional) A message to display with the assertion. Do not translate
Chris@0 246 * messages: use strtr() to embed variables in the message text, not
Chris@0 247 * t(). If left blank, a default message will be displayed.
Chris@0 248 *
Chris@0 249 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 250 * Thrown when element doesn't exist, or the link label is a different one.
Chris@0 251 */
Chris@0 252 public function linkExistsExact($label, $index = 0, $message = '') {
Chris@0 253 $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label]));
Chris@0 254 $links = $this->session->getPage()->findAll('named_exact', ['link', $label]);
Chris@0 255 $this->assert(!empty($links[$index]), $message);
Chris@0 256 }
Chris@0 257
Chris@0 258 /**
Chris@0 259 * Passes if a link with the specified label is not found.
Chris@0 260 *
Chris@0 261 * An optional link index may be passed.
Chris@0 262 *
Chris@0 263 * @param string $label
Chris@0 264 * Text between the anchor tags.
Chris@0 265 * @param string $message
Chris@0 266 * (optional) A message to display with the assertion. Do not translate
Chris@0 267 * messages: use strtr() to embed variables in the message text, not
Chris@0 268 * t(). If left blank, a default message will be displayed.
Chris@0 269 *
Chris@0 270 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 271 * Thrown when element doesn't exist, or the link label is a different one.
Chris@0 272 */
Chris@0 273 public function linkNotExists($label, $message = '') {
Chris@0 274 $message = ($message ? $message : strtr('Link with label %label not found.', ['%label' => $label]));
Chris@0 275 $links = $this->session->getPage()->findAll('named', ['link', $label]);
Chris@0 276 $this->assert(empty($links), $message);
Chris@0 277 }
Chris@0 278
Chris@0 279 /**
Chris@0 280 * Passes if a link with the exactly specified label is not found.
Chris@0 281 *
Chris@0 282 * An optional link index may be passed.
Chris@0 283 *
Chris@0 284 * @param string $label
Chris@0 285 * Text between the anchor tags.
Chris@0 286 * @param string $message
Chris@0 287 * (optional) A message to display with the assertion. Do not translate
Chris@0 288 * messages: use strtr() to embed variables in the message text, not
Chris@0 289 * t(). If left blank, a default message will be displayed.
Chris@0 290 *
Chris@0 291 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 292 * Thrown when element doesn't exist, or the link label is a different one.
Chris@0 293 */
Chris@0 294 public function linkNotExistsExact($label, $message = '') {
Chris@0 295 $message = ($message ? $message : strtr('Link with label %label not found.', ['%label' => $label]));
Chris@0 296 $links = $this->session->getPage()->findAll('named_exact', ['link', $label]);
Chris@0 297 $this->assert(empty($links), $message);
Chris@0 298 }
Chris@0 299
Chris@0 300 /**
Chris@0 301 * Passes if a link containing a given href (part) is found.
Chris@0 302 *
Chris@0 303 * @param string $href
Chris@0 304 * The full or partial value of the 'href' attribute of the anchor tag.
Chris@0 305 * @param int $index
Chris@0 306 * Link position counting from zero.
Chris@0 307 * @param string $message
Chris@0 308 * (optional) A message to display with the assertion. Do not translate
Chris@17 309 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
Chris@0 310 * variables in the message text, not t(). If left blank, a default message
Chris@0 311 * will be displayed.
Chris@0 312 *
Chris@0 313 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 314 * Thrown when element doesn't exist, or the link label is a different one.
Chris@0 315 */
Chris@0 316 public function linkByHrefExists($href, $index = 0, $message = '') {
Chris@0 317 $xpath = $this->buildXPathQuery('//a[contains(@href, :href)]', [':href' => $href]);
Chris@0 318 $message = ($message ? $message : strtr('Link containing href %href found.', ['%href' => $href]));
Chris@0 319 $links = $this->session->getPage()->findAll('xpath', $xpath);
Chris@0 320 $this->assert(!empty($links[$index]), $message);
Chris@0 321 }
Chris@0 322
Chris@0 323 /**
Chris@0 324 * Passes if a link containing a given href (part) is not found.
Chris@0 325 *
Chris@0 326 * @param string $href
Chris@0 327 * The full or partial value of the 'href' attribute of the anchor tag.
Chris@0 328 * @param string $message
Chris@0 329 * (optional) A message to display with the assertion. Do not translate
Chris@17 330 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
Chris@0 331 * variables in the message text, not t(). If left blank, a default message
Chris@0 332 * will be displayed.
Chris@0 333 *
Chris@0 334 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 335 * Thrown when element doesn't exist, or the link label is a different one.
Chris@0 336 */
Chris@0 337 public function linkByHrefNotExists($href, $message = '') {
Chris@0 338 $xpath = $this->buildXPathQuery('//a[contains(@href, :href)]', [':href' => $href]);
Chris@0 339 $message = ($message ? $message : strtr('No link containing href %href found.', ['%href' => $href]));
Chris@0 340 $links = $this->session->getPage()->findAll('xpath', $xpath);
Chris@0 341 $this->assert(empty($links), $message);
Chris@0 342 }
Chris@0 343
Chris@0 344 /**
Chris@0 345 * Builds an XPath query.
Chris@0 346 *
Chris@0 347 * Builds an XPath query by replacing placeholders in the query by the value
Chris@0 348 * of the arguments.
Chris@0 349 *
Chris@0 350 * XPath 1.0 (the version supported by libxml2, the underlying XML library
Chris@0 351 * used by PHP) doesn't support any form of quotation. This function
Chris@0 352 * simplifies the building of XPath expression.
Chris@0 353 *
Chris@0 354 * @param string $xpath
Chris@0 355 * An XPath query, possibly with placeholders in the form ':name'.
Chris@0 356 * @param array $args
Chris@0 357 * An array of arguments with keys in the form ':name' matching the
Chris@0 358 * placeholders in the query. The values may be either strings or numeric
Chris@0 359 * values.
Chris@0 360 *
Chris@0 361 * @return string
Chris@0 362 * An XPath query with arguments replaced.
Chris@0 363 */
Chris@0 364 public function buildXPathQuery($xpath, array $args = []) {
Chris@0 365 // Replace placeholders.
Chris@0 366 foreach ($args as $placeholder => $value) {
Chris@0 367 if (is_object($value)) {
Chris@0 368 throw new \InvalidArgumentException('Just pass in scalar values for $args and remove all t() calls from your test.');
Chris@0 369 }
Chris@0 370 // XPath 1.0 doesn't support a way to escape single or double quotes in a
Chris@0 371 // string literal. We split double quotes out of the string, and encode
Chris@0 372 // them separately.
Chris@0 373 if (is_string($value)) {
Chris@0 374 // Explode the text at the quote characters.
Chris@0 375 $parts = explode('"', $value);
Chris@0 376
Chris@0 377 // Quote the parts.
Chris@0 378 foreach ($parts as &$part) {
Chris@0 379 $part = '"' . $part . '"';
Chris@0 380 }
Chris@0 381
Chris@0 382 // Return the string.
Chris@0 383 $value = count($parts) > 1 ? 'concat(' . implode(', \'"\', ', $parts) . ')' : $parts[0];
Chris@0 384 }
Chris@0 385
Chris@0 386 // Use preg_replace_callback() instead of preg_replace() to prevent the
Chris@0 387 // regular expression engine from trying to substitute backreferences.
Chris@0 388 $replacement = function ($matches) use ($value) {
Chris@0 389 return $value;
Chris@0 390 };
Chris@0 391 $xpath = preg_replace_callback('/' . preg_quote($placeholder) . '\b/', $replacement, $xpath);
Chris@0 392 }
Chris@0 393 return $xpath;
Chris@0 394 }
Chris@0 395
Chris@0 396 /**
Chris@0 397 * Passes if the raw text IS NOT found escaped on the loaded page.
Chris@0 398 *
Chris@0 399 * Raw text refers to the raw HTML that the page generated.
Chris@0 400 *
Chris@0 401 * @param string $raw
Chris@0 402 * Raw (HTML) string to look for.
Chris@0 403 */
Chris@0 404 public function assertNoEscaped($raw) {
Chris@0 405 $this->responseNotContains(Html::escape($raw));
Chris@0 406 }
Chris@0 407
Chris@0 408 /**
Chris@0 409 * Passes if the raw text IS found escaped on the loaded page.
Chris@0 410 *
Chris@0 411 * Raw text refers to the raw HTML that the page generated.
Chris@0 412 *
Chris@0 413 * @param string $raw
Chris@0 414 * Raw (HTML) string to look for.
Chris@0 415 */
Chris@0 416 public function assertEscaped($raw) {
Chris@0 417 $this->responseContains(Html::escape($raw));
Chris@0 418 }
Chris@0 419
Chris@0 420 /**
Chris@18 421 * Checks that page HTML (response content) contains text.
Chris@18 422 *
Chris@18 423 * @param string|object $text
Chris@18 424 * Text value. Any non-string value will be cast to string.
Chris@18 425 *
Chris@18 426 * @throws ExpectationException
Chris@18 427 */
Chris@18 428 public function responseContains($text) {
Chris@18 429 parent::responseContains((string) $text);
Chris@18 430 }
Chris@18 431
Chris@18 432 /**
Chris@18 433 * Checks that page HTML (response content) does not contains text.
Chris@18 434 *
Chris@18 435 * @param string|object $text
Chris@18 436 * Text value. Any non-string value will be cast to string.
Chris@18 437 *
Chris@18 438 * @throws ExpectationException
Chris@18 439 */
Chris@18 440 public function responseNotContains($text) {
Chris@18 441 parent::responseNotContains((string) $text);
Chris@18 442 }
Chris@18 443
Chris@18 444 /**
Chris@0 445 * Asserts a condition.
Chris@0 446 *
Chris@0 447 * The parent method is overridden because it is a private method.
Chris@0 448 *
Chris@0 449 * @param bool $condition
Chris@0 450 * The condition.
Chris@0 451 * @param string $message
Chris@0 452 * The success message.
Chris@0 453 *
Chris@0 454 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 455 * When the condition is not fulfilled.
Chris@0 456 */
Chris@0 457 public function assert($condition, $message) {
Chris@0 458 if ($condition) {
Chris@0 459 return;
Chris@0 460 }
Chris@0 461
Chris@0 462 throw new ExpectationException($message, $this->session->getDriver());
Chris@0 463 }
Chris@0 464
Chris@0 465 /**
Chris@0 466 * Checks that a given form field element is disabled.
Chris@0 467 *
Chris@0 468 * @param string $field
Chris@0 469 * One of id|name|label|value for the field.
Chris@0 470 * @param \Behat\Mink\Element\TraversableElement $container
Chris@0 471 * (optional) The document to check against. Defaults to the current page.
Chris@0 472 *
Chris@0 473 * @return \Behat\Mink\Element\NodeElement
Chris@0 474 * The matching element.
Chris@0 475 *
Chris@0 476 * @throws \Behat\Mink\Exception\ElementNotFoundException
Chris@0 477 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 478 */
Chris@12 479 public function fieldDisabled($field, TraversableElement $container = NULL) {
Chris@0 480 $container = $container ?: $this->session->getPage();
Chris@0 481 $node = $container->findField($field);
Chris@0 482
Chris@0 483 if ($node === NULL) {
Chris@0 484 throw new ElementNotFoundException($this->session->getDriver(), 'field', 'id|name|label|value', $field);
Chris@0 485 }
Chris@0 486
Chris@0 487 if (!$node->hasAttribute('disabled')) {
Chris@0 488 throw new ExpectationException("Field $field is disabled", $this->session->getDriver());
Chris@0 489 }
Chris@0 490
Chris@0 491 return $node;
Chris@0 492 }
Chris@0 493
Chris@0 494 /**
Chris@0 495 * Checks that specific hidden field exists.
Chris@0 496 *
Chris@0 497 * @param string $field
Chris@0 498 * One of id|name|value for the hidden field.
Chris@0 499 * @param \Behat\Mink\Element\TraversableElement $container
Chris@0 500 * (optional) The document to check against. Defaults to the current page.
Chris@0 501 *
Chris@0 502 * @return \Behat\Mink\Element\NodeElement
Chris@0 503 * The matching element.
Chris@0 504 *
Chris@0 505 * @throws \Behat\Mink\Exception\ElementNotFoundException
Chris@0 506 */
Chris@0 507 public function hiddenFieldExists($field, TraversableElement $container = NULL) {
Chris@0 508 $container = $container ?: $this->session->getPage();
Chris@0 509 if ($node = $container->find('hidden_field_selector', ['hidden_field', $field])) {
Chris@0 510 return $node;
Chris@0 511 }
Chris@0 512 throw new ElementNotFoundException($this->session->getDriver(), 'form hidden field', 'id|name|value', $field);
Chris@0 513 }
Chris@0 514
Chris@0 515 /**
Chris@17 516 * Checks that specific hidden field does not exist.
Chris@0 517 *
Chris@0 518 * @param string $field
Chris@0 519 * One of id|name|value for the hidden field.
Chris@0 520 * @param \Behat\Mink\Element\TraversableElement $container
Chris@0 521 * (optional) The document to check against. Defaults to the current page.
Chris@0 522 *
Chris@0 523 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 524 */
Chris@0 525 public function hiddenFieldNotExists($field, TraversableElement $container = NULL) {
Chris@0 526 $container = $container ?: $this->session->getPage();
Chris@0 527 $node = $container->find('hidden_field_selector', ['hidden_field', $field]);
Chris@0 528 $this->assert($node === NULL, "A hidden field '$field' exists on this page, but it should not.");
Chris@0 529 }
Chris@0 530
Chris@0 531 /**
Chris@0 532 * Checks that specific hidden field have provided value.
Chris@0 533 *
Chris@0 534 * @param string $field
Chris@0 535 * One of id|name|value for the hidden field.
Chris@0 536 * @param string $value
Chris@0 537 * The hidden field value that needs to be checked.
Chris@0 538 * @param \Behat\Mink\Element\TraversableElement $container
Chris@0 539 * (optional) The document to check against. Defaults to the current page.
Chris@0 540 *
Chris@0 541 * @throws \Behat\Mink\Exception\ElementNotFoundException
Chris@0 542 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 543 */
Chris@0 544 public function hiddenFieldValueEquals($field, $value, TraversableElement $container = NULL) {
Chris@0 545 $node = $this->hiddenFieldExists($field, $container);
Chris@0 546 $actual = $node->getValue();
Chris@0 547 $regex = '/^' . preg_quote($value, '/') . '$/ui';
Chris@0 548 $message = "The hidden field '$field' value is '$actual', but '$value' expected.";
Chris@0 549 $this->assert((bool) preg_match($regex, $actual), $message);
Chris@0 550 }
Chris@0 551
Chris@0 552 /**
Chris@0 553 * Checks that specific hidden field doesn't have the provided value.
Chris@0 554 *
Chris@0 555 * @param string $field
Chris@0 556 * One of id|name|value for the hidden field.
Chris@0 557 * @param string $value
Chris@0 558 * The hidden field value that needs to be checked.
Chris@0 559 * @param \Behat\Mink\Element\TraversableElement $container
Chris@0 560 * (optional) The document to check against. Defaults to the current page.
Chris@0 561 *
Chris@0 562 * @throws \Behat\Mink\Exception\ElementNotFoundException
Chris@0 563 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 564 */
Chris@0 565 public function hiddenFieldValueNotEquals($field, $value, TraversableElement $container = NULL) {
Chris@0 566 $node = $this->hiddenFieldExists($field, $container);
Chris@0 567 $actual = $node->getValue();
Chris@0 568 $regex = '/^' . preg_quote($value, '/') . '$/ui';
Chris@0 569 $message = "The hidden field '$field' value is '$actual', but it should not be.";
Chris@0 570 $this->assert(!preg_match($regex, $actual), $message);
Chris@0 571 }
Chris@0 572
Chris@17 573 /**
Chris@17 574 * Checks that current page contains text only once.
Chris@17 575 *
Chris@17 576 * @param string $text
Chris@17 577 * The string to look for.
Chris@17 578 *
Chris@17 579 * @see \Behat\Mink\WebAssert::pageTextContains()
Chris@17 580 */
Chris@17 581 public function pageTextContainsOnce($text) {
Chris@17 582 $actual = $this->session->getPage()->getText();
Chris@17 583 $actual = preg_replace('/\s+/u', ' ', $actual);
Chris@17 584 $regex = '/' . preg_quote($text, '/') . '/ui';
Chris@17 585 $count = preg_match_all($regex, $actual);
Chris@17 586 if ($count === 1) {
Chris@17 587 return;
Chris@17 588 }
Chris@17 589
Chris@17 590 if ($count > 1) {
Chris@17 591 $message = sprintf('The text "%s" appears in the text of this page more than once, but it should not.', $text);
Chris@17 592 }
Chris@17 593 else {
Chris@17 594 $message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text);
Chris@17 595 }
Chris@17 596
Chris@17 597 throw new ResponseTextException($message, $this->session->getDriver());
Chris@17 598 }
Chris@17 599
Chris@0 600 }