-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e4ab4d
commit 421dfdf
Showing
1 changed file
with
83 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,184 +1,108 @@ | ||
from asyncio import sleep | ||
import os | ||
from pathlib import Path | ||
import unittest | ||
from django.contrib.staticfiles.testing import LiveServerTestCase | ||
from electionleaflets import settings | ||
from playwright.sync_api import sync_playwright | ||
from asgiref.sync import sync_to_async | ||
|
||
|
||
class TestLeafletUpload(LiveServerTestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.p = sync_playwright().start() | ||
cls.browser = cls.p.chromium.launch(headless=False) | ||
cls.context = cls.browser.new_context() | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
cls.context.close() | ||
cls.browser.close() | ||
cls.p.stop() | ||
super().tearDownClass() | ||
|
||
"""TestLeafletUpload is a test case class for testing the leaflet upload functionality of the application using Playwright. | ||
This class inherits from LiveServerTestCase and provides methods to set up the test environment, navigate through the | ||
application, and perform various actions such as uploading a leaflet, filling in the postcode, selecting the time, | ||
and submitting forms. Each test method simulates a specific user interaction scenario to ensure the application | ||
behaves as expected. | ||
Methods: | ||
setUp(): Set up the test environment for the frontend tests. | ||
tearDown(): Clean up resources after each test method. | ||
navigate_to_home_page(): Navigate to the home page of the live server. | ||
get_test_image(leaflet_file_path): Returns the absolute path to a test image file. | ||
navigate_to_upload_page(): Navigate to the upload page of the application. | ||
upload_leaflet(leaflet_file_path): Upload a leaflet file to the application. | ||
fill_postcode(postcode): Fill in the postcode field in the form. | ||
select_time_and_submit(): Select the time option and submit the form. | ||
select_party_and_submit(party): Select the party option and submit the form. | ||
enter_dates_and_submit(): Enter dates in the form and submit it. | ||
test_basic_upload(): Test the basic upload functionality of the leaflet application. | ||
test_upload_leaflet_from_some_time_ago(): Test uploading a leaflet with a time specified as "some time ago". | ||
test_upload_leaflet_with_invalid_postcode(): Test uploading a leaflet with an invalid postcode. | ||
""" | ||
def setUp(self): | ||
self.page = TestLeafletUpload.context.new_page() | ||
self.p = sync_playwright().start() | ||
self.browser = self.p.chromium.launch(headless=False) | ||
self.context = self.browser.new_context() | ||
self.page = self.context.new_page() | ||
self.page.set_default_timeout(60000) # Set timeout to 60 seconds | ||
|
||
def tearDown(self): | ||
self.page.close() | ||
|
||
def get_test_image(self): | ||
self.context.close() | ||
self.browser.close() | ||
self.p.stop() | ||
|
||
def navigate_to_home_page(self): | ||
self.page.goto(self.live_server_url) | ||
|
||
def get_test_image(self, leaflet_file_path="apps/leaflets/tests/test_images/front_test.jpg"): | ||
project_root = Path(settings.PROJECT_ROOT).resolve() | ||
return str(project_root / "apps/leaflets/tests/test_images/front_test.jpg") | ||
return str(project_root / leaflet_file_path) | ||
|
||
def navigate_to_upload_page(self): | ||
self.page.goto(self.live_server_url) | ||
self.page.get_by_role('link', name='Upload a leaflet').click() | ||
|
||
def upload_leaflet(self, leaflet_file_path="apps/leaflets/tests/test_images/front_test.jpg"): | ||
self.navigate_to_upload_page() | ||
self.assertTrue(self.page.get_by_text('Choose File')) | ||
self.page.set_input_files( | ||
selector="input[type='file']", | ||
files=[self.get_test_image(leaflet_file_path=leaflet_file_path)] | ||
) | ||
self.page.query_selector("button[type='submit']").click() | ||
|
||
def navigate_to_upload_page(self, page): | ||
page.goto(self.live_server_url) | ||
page.get_by_role('link', name='Upload a leaflet').click() | ||
|
||
def fill_postcode_and_submit(self, page, postcode='SW1A 1AA'): | ||
page.get_by_label('What postcode was this').click() | ||
page.get_by_label('What postcode was this').fill(postcode) | ||
def fill_postcode(self, postcode='SW1A 1AA'): | ||
self.page.get_by_label('What postcode was this').click() | ||
self.page.get_by_label('What postcode was this').fill(postcode) | ||
|
||
def select_time_and_submit(self, page): | ||
def select_time_and_submit(self): | ||
time_input = self.page.query_selector("input[type='radio'][value='now']") | ||
self.assertTrue(time_input) | ||
time_input.check() | ||
page.get_by_role('button', name='Submit').click() | ||
self.page.get_by_role('button', name='Submit').click() | ||
|
||
def select_party_and_submit(self, page, party='Green Party'): | ||
page.get_by_text(party).click() | ||
page.get_by_role('button', name='Submit').click() | ||
def select_party_and_submit(self, party='Green Party'): | ||
self.page.get_by_text(party).click() | ||
self.page.get_by_role('button', name='Submit').click() | ||
|
||
def test_basic_upload(self): | ||
self.navigate_to_upload_page(self.page) | ||
# The follow element does not render in the test browser: | ||
# self.page.get_by_label('Take a photo of a leaflet') | ||
# so I've commented it out and started testing the JS | ||
# disabled interface for this step: | ||
file_upload_button = self.page.query_selector("input[type='file']") | ||
file_upload_button.click() | ||
self.page.set_input_files("input[type='file']", self.get_test_image()) | ||
self.page.query_selector("button").click() | ||
self.fill_postcode_and_submit(self.page) | ||
self.select_time_and_submit(self.page) | ||
self.select_party_and_submit(self.page) | ||
def enter_dates_and_submit(self): | ||
self.page.get_by_label('day').fill('01') | ||
self.page.get_by_label('month').fill('01') | ||
self.page.get_by_label('year').fill('2021') | ||
self.page.get_by_role('button', name='Submit').click() | ||
|
||
@sync_to_async | ||
def _fixture_teardown(self): | ||
super()._fixture_teardown() | ||
def test_basic_upload(self): | ||
self.upload_leaflet() | ||
self.fill_postcode() | ||
self.select_time_and_submit() | ||
self.select_party_and_submit() | ||
|
||
# def test_uploading_multiple_leaflet_images(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# image_dir = 'electionleaflets/test_media/test_images' | ||
# image_files = [f'{image_dir}/{file}' for file in os.listdir(image_dir)] | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files(image_files) | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.fill_postcode_and_submit(self.page) | ||
# self.select_time_and_submit(self.page) | ||
# self.select_party_and_submit(self.page) | ||
|
||
def test_upload_leaflet_from_some_time_ago(self): | ||
self.navigate_to_upload_page(self.page) | ||
# The follow element does not render in the test browser: | ||
# self.page.get_by_label('Take a photo of a leaflet') | ||
# so I've commented it out and started testing the JS | ||
# disabled interface for this step: | ||
file_upload_button = self.page.query_selector("input[type='file']") | ||
file_upload_button.click() | ||
self.page.set_input_files(files=self.get_test_image(), selector="input[type='file']") | ||
self.page.query_selector("button").click() | ||
self.fill_postcode_and_submit(self.page) | ||
self.select_time_and_submit(self.page) | ||
self.select_party_and_submit(self.page) | ||
self.upload_leaflet() | ||
self.fill_postcode() | ||
time_input = self.page.query_selector("input[type='radio'][value='before']") | ||
time_input.check() | ||
self.page.get_by_role('button', name='Submit').click() | ||
self.enter_dates_and_submit() | ||
self.select_party_and_submit() | ||
|
||
# def test_uploading_non_image_leaflet_file(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files('../test_images/test_leaflet.docx') | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.navigate_to_upload_page() | ||
# self.assertTrue(self.page.get_by_text('Choose File')) | ||
# self.page.set_input_files( | ||
# selector="input[type='file']", | ||
# files=[self.get_test_image(leaflet_file_path="apps/leaflets/tests/test_images/test_leaflet.docx")] | ||
# ) | ||
# self.page.query_selector("button[type='submit']").click() | ||
# self.page.get_by_text('Error during upload tap to retry') | ||
|
||
# def test_read_leaflet_from_latest_leaflets(self): | ||
# self.page.goto(self.live_server_url) | ||
# self.page.get_by_role('link', name='Latest leaflets').click() | ||
# self.page.locator('.ds-card-link').first().click() | ||
# self.page.get_by_text('Leaflet details') | ||
|
||
# def test_upload_leaflet_with_invalid_postcode(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files('../test_images/test_leaflet.jpeg') | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.fill_postcode_and_submit(self.page, postcode='INVALID') | ||
# self.page.get_by_text('Enter a valid postcode') | ||
|
||
# def test_upload_leaflet_with_no_postcode(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files('../test_media/test_images/test_leaflet.jpeg') | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.page.get_by_role('button', name='Submit').click() | ||
# self.page.get_by_text('Enter a postcode') | ||
|
||
# def test_upload_leaflet_with_no_party_selected(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files('../test_images/test_leaflet.jpeg') | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.page.get_by_label('What postcode was this').fill('SW1A 1AA') | ||
# self.page.get_by_role('button', name='Submit').click() | ||
# self.page.get_by_text('Select a party') | ||
|
||
# def test_upload_leaflet_with_invalid_file_type(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files('../test_images/test_leaflet.txt') | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.page.get_by_text('Invalid file type') | ||
|
||
# def test_upload_leaflet_with_large_file(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files('../test_images/large_test_leaflet.jpeg') | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.page.get_by_text('File too large') | ||
|
||
# def test_upload_leaflet_with_multiple_files(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files(['../test_images/test_leaflet.jpeg', '../test_images/test_leaflet2.jpeg']) | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.fill_postcode_and_submit(self.page) | ||
# self.select_time_and_submit(self.page) | ||
# self.select_party_and_submit(self.page) | ||
|
||
# def test_upload_leaflet_with_no_file(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.page.get_by_text('Please upload a file') | ||
|
||
# def test_upload_leaflet_with_corrupted_file(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files('../test_images/corrupted_test_leaflet.jpeg') | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.page.get_by_text('Error during upload tap to retry') | ||
|
||
# def test_upload_leaflet_with_special_characters_in_postcode(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files('../test_media/test_images/test_leaflet.jpeg') | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.fill_postcode_and_submit(self.page, postcode='SW1A@1AA') | ||
# self.page.get_by_text('Enter a valid postcode') | ||
|
||
# def test_upload_leaflet_with_empty_postcode(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files('../test_media/test_images/test_leaflet.jpeg') | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.page.get_by_label('What postcode was this').fill('') | ||
# self.page.get_by_role('button', name='Submit').click() | ||
# self.page.get_by_text('Enter a postcode') | ||
|
||
# def test_upload_leaflet_with_large_number_of_files(self): | ||
# self.navigate_to_upload_page(self.page) | ||
# image_dir = 'electionleaflets/test_media/test_images' | ||
# image_files = [f'{image_dir}/{file}' for file in os.listdir(image_dir)] * 10 | ||
# self.page.get_by_label('Take a photo of a leaflet').set_input_files(image_files) | ||
# self.page.get_by_role('button', name='Continue').click() | ||
# self.fill_postcode_and_submit(self.page) | ||
# self.select_time_and_submit(self.page) | ||
# self.select_party_and_submit(self.page) | ||
def test_upload_leaflet_with_invalid_postcode(self): | ||
self.upload_leaflet() | ||
self.fill_postcode(postcode='INVALID') | ||
self.page.get_by_text('Enter a valid postcode') |