Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add selenium tests #70

Merged
merged 26 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
58a8947
Add selenium test
MatMoore Feb 12, 2024
8c17e88
Add page helpers
MatMoore Feb 13, 2024
90278ec
Split out test steps into methods
MatMoore Feb 13, 2024
e944cdc
Add tests
MatMoore Feb 13, 2024
1739bce
Add sort test
MatMoore Feb 13, 2024
3bb28d6
More tests
MatMoore Feb 13, 2024
b29bd60
Run axe-core against the selenium controlled browser
MatMoore Feb 13, 2024
21a1884
Correct accessibility issues flagged by axe-core
MatMoore Feb 13, 2024
840b3ab
Convert to pytest
MatMoore Feb 14, 2024
2a497d1
Convert to pytest
MatMoore Feb 14, 2024
df7a435
Refactor
MatMoore Feb 14, 2024
f55c9b7
refactor
MatMoore Feb 14, 2024
bbcece2
Add screenshotting and mark slow tests
MatMoore Feb 14, 2024
5218229
Restrict CI to running the fast tests
MatMoore Feb 14, 2024
be44830
Commit changes made by code formatters
github-actions[bot] Feb 14, 2024
0237189
Don't run tests on editing PR
MatMoore Feb 14, 2024
0c316d5
Go headless and switch to chrome
MatMoore Feb 14, 2024
7519d87
Add selenium to CI
MatMoore Feb 14, 2024
272f121
Combine jobs
MatMoore Feb 14, 2024
ca287f9
Extract superclass
MatMoore Feb 14, 2024
760fad2
Apply suggestions from code review
MatMoore Feb 14, 2024
ff71d9b
Only take screenshot on failure
MatMoore Feb 15, 2024
c5b897e
Commit changes made by code formatters
github-actions[bot] Feb 15, 2024
ce872a0
Fix screenshot being cropped with headless browser
MatMoore Feb 15, 2024
c51acb7
Update tests/selenium/conftest.py
MatMoore Feb 15, 2024
4fc88b3
Update tests/selenium/conftest.py
MatMoore Feb 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ name: test

on:
pull_request:
types: [opened, edited, reopened, synchronize]
branches:
- main
push:
branches:
- main

jobs:
test:
Expand Down Expand Up @@ -32,5 +36,10 @@ jobs:
- run: poetry install --no-interaction --no-root
if: steps.cache-deps.outputs.cache-hit != 'true'
- run: poetry install --no-interaction
- name: test with coverage
run: poetry run pytest --cov
- name: run unit tests with coverage
id: fast-tests
run: poetry run pytest --cov -m 'not slow'
- name: run selenium tests
id: slow-tests
if: steps.fast-tests.outcome == 'success'
run: poetry run pytest -m 'slow'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,5 @@ cython_debug/
staticfiles/

node_modules

tmp/
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ Run `npm install` and then `npm run sass` to compile the stylesheets.
/search

![Screenshot of the service showing the search page](image.png)

## Testing

- Unit tests: `pytest -m 'not slow'`
- Selenium tests: `pytest -m 'slow'`
8 changes: 8 additions & 0 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,11 @@

# session
SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"

# Not actually used - Just required for LiveServerTestCase
DATABASES = {
MatMoore marked this conversation as resolved.
Show resolved Hide resolved
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
}
7 changes: 5 additions & 2 deletions home/forms/search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from copy import deepcopy
from django import forms
from urllib.parse import urlencode

from django import forms


def get_domain_choices():
"""Make API call to obtain domain choices"""
Expand Down Expand Up @@ -31,7 +32,9 @@ class SearchForm(forms.Form):
max_length=100,
strip=False,
required=False,
widget=forms.TextInput(attrs={"class": "govuk-input search-input"}),
widget=forms.TextInput(
attrs={"class": "govuk-input search-input", "aria-label": "Search"}
),
)
domains = forms.MultipleChoiceField(
choices=get_domain_choices,
Expand Down
5 changes: 2 additions & 3 deletions poetry.lock

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

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pytest-cov = "^4.1.0"
[tool.poetry.group.dev.dependencies]
black = "^23.12.1"
pre-commit = "^3.6.0"
selenium = ">=4.8.0"

[build-system]
requires = ["poetry-core"]
Expand All @@ -32,3 +33,4 @@ build-backend = "poetry.core.masonry.api"
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "core.settings"
python_files = ["test_*.py", "*_test.py", "testing/python/*.py"]
markers = ["slow: marks tests as slow (deselect with '-m \"not slow\"')"]
2 changes: 1 addition & 1 deletion templates/base/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{% include "base/navigation.html" %}

<div class="govuk-width-container app-width-container">
<div class="govuk-phase-banner">
<div class="govuk-phase-banner" role="region">
<p class="govuk-phase-banner__content">
<strong class="govuk-tag govuk-phase-banner__content__tag">Alpha
</strong>
Expand Down
5 changes: 2 additions & 3 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
{% block content %}

<a onclick="history.back()" class="govuk-back-link">Back</a>
<main class="govuk-main-wrapper app-main-class" id="main-content" role="main">
<h1 class="govuk-heading-xl">Customised page template</h1>
</main>
<h1 class="govuk-heading-xl">Customised page template</h1>


{% endblock content %}
8 changes: 3 additions & 5 deletions templates/partial/filter.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ <h3 class="govuk-heading-s govuk-!-margin-bottom-0">Domain</h3>
<input type="hidden" name="clear_label" value="">
{% for label, href in label_clear_href.items %}
<li><a class="moj-filter__tag govuk-link" href="{{ href }}">
{{label}}<span class="govuk-visually-hidden">Remove this filter</span>
<span data-test-id="selected-domain-label">{{label}}</span> <span class="govuk-visually-hidden">Remove this filter</span>
</a></li>
{% endfor %}
</ul>
{% endif %}
</div>
<div class="moj-filter__options">
<button form="searchform" class="govuk-button" data-module="govuk-button" data-test-id="submit-button">
<button form="searchform" class="govuk-button" data-module="govuk-button" data-test-id="apply-filters">
Apply filters
</button>
<div class="govuk-form-group">
Expand All @@ -48,9 +48,7 @@ <h3 class="govuk-heading-s govuk-!-margin-bottom-0">Domain</h3>
{% for domain_option in form.domains %}
<div class="govuk-checkboxes__item">
{{ domain_option.tag}}
<label class="govuk-label govuk-checkboxes__label" for="{{ domain_option.id_for_label }}">
{{domain_option.choice_label}}
</label>
<label class="govuk-label govuk-checkboxes__label" for="{{ domain_option.id_for_label }}">{{domain_option.choice_label}}</label>
</div>
{% endfor %}
</div>
Expand Down
3 changes: 2 additions & 1 deletion templates/partial/pagination.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<span class="govuk-pagination__link-title">Previous<span class="govuk-visually-hidden"> page</span></span></a>
</div>
{% endif %}
<ul class="govuk-pagination__list">
{% for p in page_range %}
{% if paginator.ELLIPSIS != p %}
{% if page_obj.number == p %}
Expand All @@ -20,11 +21,11 @@
{{ p }}
</a>
</li>
</ul>
{% else %}
<li class="govuk-pagination__item govuk-pagination__item--ellipses">&ctdot;</li>
{% endif %}
{% endfor %}
</ul>
{% if page_obj.has_next %}
<div class="govuk-pagination__next">
<a class="govuk-link govuk-pagination__link" href="{% url 'home:pagination' page=page_obj.next_page_number %}{% query_string clear_label=None clear_filter=None value=None new=None %}" rel="next"> <span class="govuk-pagination__link-title">Next<span class="govuk-visually-hidden"> page</span></span> <svg class="govuk-pagination__icon govuk-pagination__icon--next" xmlns="http://www.w3.org/2000/svg" height="13" width="15" aria-hidden="true" focusable="false" viewBox="0 0 15 13">
Expand Down
8 changes: 5 additions & 3 deletions templates/partial/search_result.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{% load markdown %}
{% load humanize %}
<div id="search-results">
{% for result in results %}

<div class="govuk-grid-row" id="search_result">
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<h3 class="govuk-heading-m govuk-!-margin-bottom-2">
<a href="{% url 'home:details' id=result.id %}" class="govuk-link">{{result.name}}</a>
Expand All @@ -17,7 +18,7 @@ <h3 class="govuk-heading-m govuk-!-margin-bottom-2">
{% endif %}
</h3>
{% if result.description %}
<div class="govuk-body-m">{{result.description|markdown}}</div>
<div class="govuk-body-m">{{result.metadata.search_summary|markdown}}</div>
{% endif %}


Expand Down Expand Up @@ -51,4 +52,5 @@ <h3 class="govuk-heading-m govuk-!-margin-bottom-2">
</div>
</div>
</div>
{%endfor%}
{%endfor%}
</div>
4 changes: 1 addition & 3 deletions templates/partial/sort.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
{% for radio in form.sort %}
<div class="govuk-radios__item">
{{radio.tag}}
<label class="govuk-label govuk-radios__label" for="{{ radio.id_for_label }}">
{{radio.choice_label}}
</label>
<label class="govuk-label govuk-radios__label" for="{{ radio.id_for_label }}">{{radio.choice_label}}</label>
</div>
{% endfor %}
</div>
Expand Down
4 changes: 2 additions & 2 deletions templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h1 class="govuk-heading-xl">Find MOJ Data</h1>
{{ form.query }}
<button class="search-button" type="submit">
<svg aria-hidden="true" class="search-icon" focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36" width="40" height="40"><path d="M25.7 24.8L21.9 21c.7-1 1.1-2.2 1.1-3.5 0-3.6-2.9-6.5-6.5-6.5S10 13.9 10 17.5s2.9 6.5 6.5 6.5c1.6 0 3-.6 4.1-1.5l3.7 3.7 1.4-1.4zM12 17.5c0-2.5 2-4.5 4.5-4.5s4.5 2 4.5 4.5-2 4.5-4.5 4.5-4.5-2-4.5-4.5z" fill="currentColor"></path>
</svg><span class="govuk-visually-hidden">Search</span>
</svg><label class="govuk-visually-hidden">Search</span>
</button>
</div>
</div>
Expand All @@ -23,7 +23,7 @@ <h1 class="govuk-heading-xl">Find MOJ Data</h1>
<div class="govuk-grid-row">
{% include "partial/filter.html" %}
<div class="govuk-grid-column-two-thirds">
<h2 class="govuk-heading-l govuk-!-display-inline-block">{{total_results|intcomma}} Results</h2>
<h2 class="govuk-heading-l govuk-!-display-inline-block" id="result-count">{{total_results|intcomma}} Results</h2>
{% include "partial/sort.html" %}
{% include "partial/search_result.html" %}
{% include "partial/pagination.html" %}
Expand Down
Empty file added tests/__init__.py
Empty file.
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
)
from django.test import Client
from faker import Faker
from home.service.search import SearchService
from home.service.details import DetailsService

from home.forms.search import SearchForm
from home.service.details import DetailsService
from home.service.search import SearchService

fake = Faker()

Expand Down
Empty file added tests/home/__init__.py
Empty file.
Empty file added tests/selenium/__init__.py
Empty file.
Loading
Loading