function JSWebAssert::buildJavascriptStatusMessageSelector

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php \Drupal\FunctionalJavascriptTests\JSWebAssert::buildJavascriptStatusMessageSelector()
  2. 11.x core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php \Drupal\FunctionalJavascriptTests\JSWebAssert::buildJavascriptStatusMessageSelector()

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

The selector is designed to work with the Drupal.theme.message template defined in message.js in addition to status-messages.html.twig in the system module.

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.

4 calls to JSWebAssert::buildJavascriptStatusMessageSelector()
JSWebAssert::statusMessageContainsAfterWait in core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php
Asserts that a status message containing given string exists after wait.
JSWebAssert::statusMessageExistsAfterWait in core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php
Asserts that a status message exists after wait.
JSWebAssert::statusMessageNotContainsAfterWait in core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php
Asserts that no status message containing given string exists after wait.
JSWebAssert::statusMessageNotExistsAfterWait in core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php
Asserts that a status message does not exist after wait.

File

core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php, line 694

Class

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

Namespace

Drupal\FunctionalJavascriptTests

Code

private function buildJavascriptStatusMessageSelector(?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));
    }
    if ($type) {
        $class = 'messages--' . $type;
    }
    else {
        $class = 'messages__wrapper';
    }
    if ($message) {
        $js_selector = $this->buildXPathQuery('//div[contains(@class, :class) and contains(., :message)]', [
            ':class' => $class,
            ':message' => $message,
        ]);
    }
    else {
        $js_selector = $this->buildXPathQuery('//div[contains(@class, :class)]', [
            ':class' => $class,
        ]);
    }
    // We select based on WebAssert::buildStatusMessageSelector() or the
    // js_selector we have just built.
    return $this->buildStatusMessageSelector($message, $type) . ' | ' . $js_selector;
}

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