diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php
index bb66b5305d7..430784bfebc 100644
--- a/lib/tests/behat/behat_general.php
+++ b/lib/tests/behat/behat_general.php
@@ -2280,4 +2280,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(),
+ '
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 3aa954890d1..4b84ea50cff 100644
--- a/lib/upgrade.txt
+++ b/lib/upgrade.txt
@@ -7,6 +7,9 @@ information provided here is intended especially for developers.
If it's necessary to override this, pass `false` to its new optional parameter `$appendsitename`.
* New page title separator constant `moodle_page:TITLE_SEPARATOR` has been created to help standardise the separators used in page
titles.
+* 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"`
=== 4.1.5 ===
* Added a new render of caption for the table in render_caption. It can be used by