function WebAssert::pageTextContainsOnce

Same name in other branches
  1. 9 core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::pageTextContainsOnce()
  2. 10 core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::pageTextContainsOnce()
  3. 11.x core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::pageTextContainsOnce()

Checks that current page contains text only once.

Parameters

string $text: The string to look for.

See also

\Behat\Mink\WebAssert::pageTextContains()

File

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

Class

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

Namespace

Drupal\Tests

Code

public function pageTextContainsOnce($text) {
    $actual = $this->session
        ->getPage()
        ->getText();
    $actual = preg_replace('/\\s+/u', ' ', $actual);
    $regex = '/' . preg_quote($text, '/') . '/ui';
    $count = preg_match_all($regex, $actual);
    if ($count === 1) {
        return;
    }
    if ($count > 1) {
        $message = sprintf('The text "%s" appears in the text of this page more than once, but it should not.', $text);
    }
    else {
        $message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text);
    }
    throw new ResponseTextException($message, $this->session
        ->getDriver());
}

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