diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index 258103b5c22..371e0797762 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -2189,4 +2189,36 @@ EOF; } } + /** + * Check that the page title contains a given string. + * + * @Given the page title should contain ":title" + * @param string $title The string that should be present on the page title. + */ + public function the_page_title_should_contain(string $title): void { + $session = $this->getSession(); + if ($this->running_javascript()) { + // When running on JS, the page title can be changed via JS, so it's more reliable to get the actual page title via JS. + $actualtitle = $session->evaluateScript("return document.title"); + } else { + $titleelement = $session->getPage()->find('css', 'head title'); + if ($titleelement === null) { + // Throw an exception if a page title is not present on the page. + throw new ElementNotFoundException( + $this->getSession(), + '