namespace Drupal\Tests;
use Behat\Mink\Exception\ExpectationException;
+use Behat\Mink\Exception\ResponseTextException;
use Behat\Mink\WebAssert as MinkWebAssert;
use Behat\Mink\Element\TraversableElement;
use Behat\Mink\Exception\ElementNotFoundException;
$this->assert(!preg_match($regex, $actual), $message);
}
+ /**
+ * Checks that current page contains text only once.
+ *
+ * @param string $text
+ * The string to look for.
+ *
+ * @see \Behat\Mink\WebAssert::pageTextContains()
+ */
+ 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());
+ }
+
}