From c9563d470fabb443ce51020673bfde2cc0dda81a Mon Sep 17 00:00:00 2001 From: Cameron Lamb Date: Tue, 11 Apr 2023 12:44:47 +0100 Subject: [PATCH] Add more e2e tests (#346) --- config/urls.py | 1 - docs/testing.md | 46 ++++++---- docs/upgrading.md | 21 +++++ e2e_tests/{test_smoke.py => test_homepage.py} | 19 ++-- e2e_tests/test_wagtail_admin.py | 86 +++++++++++++++++++ home/templates/home/home_page.html | 2 +- news/factories.py | 11 +++ poetry.lock | 22 ++++- pyproject.toml | 1 + 9 files changed, 178 insertions(+), 31 deletions(-) create mode 100644 docs/upgrading.md rename e2e_tests/{test_smoke.py => test_homepage.py} (76%) create mode 100644 e2e_tests/test_wagtail_admin.py create mode 100644 news/factories.py diff --git a/config/urls.py b/config/urls.py index 008c5e004..381c99ed6 100644 --- a/config/urls.py +++ b/config/urls.py @@ -46,7 +46,6 @@ path("", include(wagtail_urls)), ] - if settings.DEBUG: from django.conf.urls.static import static from django.contrib.staticfiles.urls import staticfiles_urlpatterns diff --git a/docs/testing.md b/docs/testing.md index bd6e42951..834efd267 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -1,20 +1,28 @@ -When upgrading wagtail, check the: +# Testing -- front page - - news showing -- content search -- people finder search -- people finder - - view profile - - edit profile - - view team - - edit team -- django admin -- wagtail admin - - home news order - - drag to sort - - add a news page - - upload and add an image - - publish and view live - - appears on home page - - appears on news page +## E2E codegen tests + +When writing playwright tests it might be useful to use the `make e2e-codegen` tool to help generate code. +If you need to log into the admin side of the site then you can add the following to `config/urls.py`: + +```python +# Test admin login +def login_as_admin(request): + from django.contrib.auth import login + + from user.models import User + + user = User.objects.filter(is_superuser=True).first() + login( + request, + user, + backend="django.contrib.auth.backends.ModelBackend", + ) + return RedirectView.as_view(url="/")(request) + +urlpatterns = [ + path("login-as-admin/", login_as_admin), +] + urlpatterns +``` + +Then when you are browsing the site you can go to `/login-as-admin/` to which will find a superuser and log you in as them. diff --git a/docs/upgrading.md b/docs/upgrading.md new file mode 100644 index 000000000..b58dae7f3 --- /dev/null +++ b/docs/upgrading.md @@ -0,0 +1,21 @@ +## Upgrading the site +When upgrading wagtail, check the: + +- front page + - news showing +- content search +- people finder search +- people finder + - view profile + - edit profile + - view team + - edit team +- django admin +- wagtail admin + - home news order + - drag to sort + - add a news page + - upload and add an image + - publish and view live + - appears on home page + - appears on news page diff --git a/e2e_tests/test_smoke.py b/e2e_tests/test_homepage.py similarity index 76% rename from e2e_tests/test_smoke.py rename to e2e_tests/test_homepage.py index 45e371bf4..74d21f411 100644 --- a/e2e_tests/test_smoke.py +++ b/e2e_tests/test_homepage.py @@ -3,21 +3,22 @@ import pytest from playwright.sync_api import Page, expect -from .utils import login +from news.factories import NewsPageFactory @pytest.mark.e2e -def test_wagtail_accessible(superuser, page: Page): - login(page, superuser) +def test_homepage(page: Page): + NewsPageFactory.create_batch(5) - page.goto("/admin") - expect(page).to_have_title(re.compile(r".*DBT Digital Workspace")) - - -@pytest.mark.e2e -def test_site_has_all_major_sections(page: Page): page.goto("/") expect(page).to_have_title(re.compile(r"Home.*")) + + page.get_by_title("News page 1") + page.get_by_title("News page 2") + page.get_by_title("News page 3") + page.get_by_title("News page 4") + page.get_by_title("News page 5") + page.get_by_text("Quick links").click() page.get_by_role("heading", name="What's Popular?").click() page.get_by_role("heading", name="How do I?").click() diff --git a/e2e_tests/test_wagtail_admin.py b/e2e_tests/test_wagtail_admin.py new file mode 100644 index 000000000..4bd94e7c2 --- /dev/null +++ b/e2e_tests/test_wagtail_admin.py @@ -0,0 +1,86 @@ +import re + +import pytest +from playwright.sync_api import Page, expect + +from news.factories import NewsPageFactory +from news.models import NewsHome + +from .utils import login + + +@pytest.mark.e2e +def test_add_news_page(superuser, page: Page): + news_home = NewsHome.objects.first() + NewsPageFactory.create_batch(5, parent=news_home) + + login(page, superuser) + page.goto("/admin") + expect(page).to_have_title(re.compile(r".*DBT Digital Workspace")) + + page.get_by_role("button", name="Pages").click() + page.get_by_role("link", name="Home", exact=True).click() + page.get_by_role("link", name="Explore child pages of 'News and views'").click() + page.get_by_role("button", name="Actions").click() + page.get_by_role("link", name="Add child page").click() + + # Create a news page. + page.get_by_role("textbox", name="title").fill("Test news page") + page.get_by_role("button", name="Insert a block").click() + page.get_by_placeholder("Search options…").fill("Heading 2") + page.keyboard.press("Enter") + page.locator("#body-0-value").fill("Test news page heading") + + # TODO: The choose an image modal doesn't seem to be appearing. + # page.get_by_role("button", name="Choose an image").click() + # page.locator(".image-choice").nth(0).click() + + # Save draft + page.get_by_role("button", name="Save draft").click() + + # Publish + page.locator("li.footer__container nav .dropdown-toggle").click() + page.get_by_role("button", name="Publish").click() + + # Check the page is visible in the admin. + page.get_by_text("Test news page") + + +@pytest.mark.e2e +def test_home_page_news_order(superuser, page: Page): + news_home = NewsHome.objects.first() + news_pages = NewsPageFactory.create_batch(5, parent=news_home) + + login(page, superuser) + page.goto("/admin") + expect(page).to_have_title(re.compile(r".*DBT Digital Workspace")) + page.get_by_text("Home news order").click() + + # Add the news pages to define their order on the home page. + for news_page in news_pages: + page.get_by_text("Add Home page news order").click() + page.get_by_text("Choose a page (News Page)").click() + page.get_by_text(news_page.title).click() + page.get_by_role("button", name="Save").click() + + # Visit the home page to see the news order + page.goto("/") + home_news_items = [ + i.inner_text() for i in page.get_by_test_id("home-news-item").all() + ] + + # Drag the news pages to change their order. + page.goto("/admin") + page.get_by_text("Home news order").click() + news_page_1 = page.get_by_role("link", name=news_pages[0].title) + news_page_5 = page.get_by_role("link", name=news_pages[4].title) + news_page_1.drag_to(news_page_5) + + # Visit the home page to see the updated news order + page.goto("/") + updated_home_news_items = [ + i.inner_text() for i in page.get_by_test_id("home-news-item").all() + ] + + # Make sure the new order isn't the same as the old order. + assert home_news_items != updated_home_news_items diff --git a/home/templates/home/home_page.html b/home/templates/home/home_page.html index 2ddfdc020..f421151b4 100644 --- a/home/templates/home/home_page.html +++ b/home/templates/home/home_page.html @@ -23,7 +23,7 @@ {% for news_item in news_items %} -
  • +
  • {% include "home/includes/news_item.html" with news_item=news_item %}
  • {% endfor %} diff --git a/news/factories.py b/news/factories.py new file mode 100644 index 000000000..cf07b9f73 --- /dev/null +++ b/news/factories.py @@ -0,0 +1,11 @@ +import factory +import wagtail_factories + +from news import models + + +class NewsPageFactory(wagtail_factories.PageFactory): + title = factory.Sequence(lambda n: f"News page {n+1}") + + class Meta: + model = models.NewsPage diff --git a/poetry.lock b/poetry.lock index 5286f4f96..f53b88a26 100644 --- a/poetry.lock +++ b/poetry.lock @@ -3574,6 +3574,26 @@ files = [ [package.dependencies] wagtail = ">2.9" +[[package]] +name = "wagtail-factories" +version = "4.0.0" +description = "Factory boy classes for wagtail" +category = "dev" +optional = false +python-versions = "*" +files = [ + {file = "wagtail_factories-4.0.0-py2.py3-none-any.whl", hash = "sha256:f59f9af283da79b3ddab7b2ca95d6a14b5bbbb929a697563e4f7143948693f53"}, + {file = "wagtail_factories-4.0.0.tar.gz", hash = "sha256:3e39ec1cc13b61c6e467f1bf223ce2d134e823fa9fe4dc7e32d0222cc8d350ec"}, +] + +[package.dependencies] +factory-boy = ">=3.2" +wagtail = ">=2.15,<5.0" + +[package.extras] +docs = ["sphinx (>=1.4.0)"] +test = ["coverage (==6.0)", "flake8 (==4.0.0)", "flake8-blind-except (==0.1.1)", "flake8-debugger (==4.1.2)", "isort (==5.10.0)", "psycopg2 (>=2.3.1)", "pytest (==6.2.5)", "pytest-cov (==3.0.0)", "pytest-django (==4.5.0)", "pytest-pythonpath (==0.7.3)"] + [[package]] name = "wagtailmedia" version = "0.13.0" @@ -3890,4 +3910,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "487d64c68ff1da89a30491b92b16099b4bebb6680f00a56e703edd8008e27a1c" +content-hash = "d457ceff66065582fe126b72bd6974a24055b6cacd2ca7cac451fe9fbe6134be" diff --git a/pyproject.toml b/pyproject.toml index b2efd4e3b..601393c99 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,6 +82,7 @@ pytest-playwright = "^0.3.2" coverage = "^5.5" faker = "^11.3.0" factory-boy = "^3.2.1" +wagtail-factories = "^4.0.0" # Util bpython = "^0.22.1" django-extensions = "^3.2.1"