Skip to content

Commit

Permalink
MDL-78806 behat: Create a step that for checking the page title
Browse files Browse the repository at this point in the history
Create a Behat step "the page title should contain ':title'" to check
the page title.
  • Loading branch information
junpataleta committed Sep 10, 2023
1 parent 94efef8 commit 9b6ed47
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/tests/behat/behat_general.php
Expand Up @@ -2280,4 +2280,36 @@ public function the_site_is_running_moodle_version_or_lower(string $maxversion):
}
}

/**
* 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(),
'<title> 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
);
}
}
}
3 changes: 3 additions & 0 deletions lib/upgrade.txt
Expand Up @@ -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
Expand Down

0 comments on commit 9b6ed47

Please sign in to comment.