function WebAssert::buildStatusMessageSelector

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::buildStatusMessageSelector()
  2. 11.x core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::buildStatusMessageSelector()

Builds a xpath selector for a message with given type and text.

The selector is designed to work with the status-messages.html.twig template in the system module.

See Drupal\Core\Render\Element\StatusMessages for aria label definition.

Parameters

string|null $message: The optional message or partial message to assert.

string|null $type: The optional message type: status, error, or warning.

Return value

string The xpath selector for the message.

Throws

\InvalidArgumentException Thrown when $type is not an allowed type.

7 calls to WebAssert::buildStatusMessageSelector()
JSWebAssert::buildJavascriptStatusMessageSelector in core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php
Builds a xpath selector for a message with given type and text.
JSWebAssert::statusMessageContains in core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php
JSWebAssert::statusMessageNotContains in core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php
WebAssert::statusMessageContains in core/tests/Drupal/Tests/WebAssert.php
Asserts that a status message containing a given string exists.
WebAssert::statusMessageExists in core/tests/Drupal/Tests/WebAssert.php
Asserts that a status message exists.

... See full list

File

core/tests/Drupal/Tests/WebAssert.php, line 901

Class

WebAssert
Defines a class with methods for asserting presence of elements during tests.

Namespace

Drupal\Tests

Code

protected function buildStatusMessageSelector(?string $message = NULL, ?string $type = NULL) : string {
  $allowed_types = [
    'status',
    'error',
    'warning',
    NULL,
  ];
  if (!in_array($type, $allowed_types, TRUE)) {
    throw new \InvalidArgumentException(sprintf("If a status message type is specified, the allowed values are 'status', 'error', 'warning'. The value provided was '%s'.", $type));
  }
  $selector = '//div[@data-drupal-messages]';
  $aria_label = NULL;
  switch ($type) {
    case 'status':
      $aria_label = 'Status message';
      break;

    case 'error':
      $aria_label = 'Error message';
      break;

    case 'warning':
      $aria_label = 'Warning message';
  }
  if ($message && $aria_label && $type) {
    $selector = $this->buildXPathQuery($selector . '//div[(contains(@aria-label, :aria_label) or contains(@aria-labelledby, :type)) and contains(., :message)]', [
      // Value of the 'aria-label' attribute, used in Stark.
':aria_label' => $aria_label,
      // Value of the 'aria-labelledby' attribute, used in Claro and Olivero.
':type' => $type,
      ':message' => $message,
    ]);
  }
  elseif ($message) {
    $selector = $this->buildXPathQuery($selector . '//div[contains(., :message)]', [
      ':message' => $message,
    ]);
  }
  elseif ($aria_label) {
    $selector = $this->buildXPathQuery($selector . '//div[@aria-label=:aria_label]', [
      ':aria_label' => $aria_label,
    ]);
  }
  return $selector;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.