Skip to content

Commit

Permalink
Add more e2e tests (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
CamLamb authored Apr 11, 2023
1 parent eed3880 commit c9563d4
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 31 deletions.
1 change: 0 additions & 1 deletion config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 27 additions & 19 deletions docs/testing.md
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 21 additions & 0 deletions docs/upgrading.md
Original file line number Diff line number Diff line change
@@ -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
19 changes: 10 additions & 9 deletions e2e_tests/test_smoke.py → e2e_tests/test_homepage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
86 changes: 86 additions & 0 deletions e2e_tests/test_wagtail_admin.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion home/templates/home/home_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h3 class="govuk-heading-m" id="quick-link-menu">Quick links</h3>
</nav>
</li>
{% for news_item in news_items %}
<li class="news-item news-item-{{ forloop.counter }}">
<li class="news-item news-item-{{ forloop.counter }}" data-testid="home-news-item">
{% include "home/includes/news_item.html" with news_item=news_item %}
</li>
{% endfor %}
Expand Down
11 changes: 11 additions & 0 deletions news/factories.py
Original file line number Diff line number Diff line change
@@ -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
22 changes: 21 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit c9563d4

Please sign in to comment.