From 17199f0c2695856448b786d1d08fb1613bc5cbcb Mon Sep 17 00:00:00 2001 From: Chris Hill Scott Date: Tue, 4 Jul 2017 14:04:46 +0100 Subject: [PATCH] Test for page titles in client request fixture MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We’re frequently ending up with mismatched page titles on our pages because: - they’re hard to spot in the browsers tab bar - new pages are often created by copy pasting old ones It would be hard to write some kind of macro that spat out the h1 and the page title because they go into different blocks in the parent template. Instead, we can catch these mistakes by testing for them. Going forward we should be using the `client_request` fixture for testing. So it makes sense to put the page title test in here so that we get it for free in every test – same way we do for testing response codes. fixup! Test for page titles in client request fixture --- tests/conftest.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 69fc41a23..1f2c6218c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1757,13 +1757,26 @@ def client_request(logged_in_client): yield session @staticmethod - def get(endpoint, _expected_status=200, _follow_redirects=False, **endpoint_kwargs): + def get( + endpoint, + _expected_status=200, + _follow_redirects=False, + _test_page_title=True, + **endpoint_kwargs + ): resp = logged_in_client.get( url_for(endpoint, **(endpoint_kwargs or {})), follow_redirects=_follow_redirects, ) assert resp.status_code == _expected_status - return BeautifulSoup(resp.data.decode('utf-8'), 'html.parser') + page = BeautifulSoup(resp.data.decode('utf-8'), 'html.parser') + if _test_page_title: + page_title, h1 = ( + normalize_spaces(page.find(selector).text) for selector in ('title', 'h1') + ) + if not normalize_spaces(page_title).startswith(h1): + raise AssertionError('Page title ‘{}’ does not start with H1 ‘{}’'.format(page_title, h1)) + return page @staticmethod def post(endpoint, _data=None, _expected_status=None, _follow_redirects=False, **endpoint_kwargs):