From 4533cb81a4236fd3c7cbeae819fd48652aa82ad5 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Sat, 9 Sep 2023 20:44:19 +0800 Subject: [PATCH] MDL-78806 behat: Create a step that for checking the page title Create a Behat step "the page title should contain ':title'" to check the page title. --- lib/tests/behat/behat_general.php | 33 +++++++++++++++++++++++++++++++ lib/upgrade.txt | 5 +++++ 2 files changed, 38 insertions(+) diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index dc6242f277f..371e0797762 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -2188,4 +2188,37 @@ 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(), + ' element', + 'css', + 'head title' + ); + } + $actualtitle = $titleelement->getText(); + } + + if (strpos($actualtitle, $title) === false) { + throw new ExpectationException( + "'$title' was not found from the current page title '$actualtitle'", + $session + ); + } + } } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 721dd4214b4..edd3c1cb0ac 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -1,6 +1,11 @@ This files describes API changes in core libraries and APIs, information provided here is intended especially for developers. +=== 3.9.24 === +* New Behat step \behat_general::the_page_title_should_contain() has been added to allow checking of page titles. You can use this + when writing feature files to check that the page title contains the expected string. + e.g. `And the page title should contain "Some title"` + ===3.9.22=== * Added a new parameter in address_in_subnet to give us the ability to check for 0.0.0.0 or not.