From 76a9dea505ee3f082cb6a2abd583bf7ecfa65de5 Mon Sep 17 00:00:00 2001 From: Virginia Dooley Date: Mon, 30 Sep 2024 16:11:06 +0100 Subject: [PATCH] WIP playwright for TestCase --- .../apps/leaflets/tests/test_frontend.py | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 electionleaflets/apps/leaflets/tests/test_frontend.py diff --git a/electionleaflets/apps/leaflets/tests/test_frontend.py b/electionleaflets/apps/leaflets/tests/test_frontend.py new file mode 100644 index 00000000..dccecdd6 --- /dev/null +++ b/electionleaflets/apps/leaflets/tests/test_frontend.py @@ -0,0 +1,69 @@ +import os +import unittest + +from playwright.sync_api import expect, sync_playwright # 1.37.0 + + +class TestLeaflet(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.p = sync_playwright().start() + cls.browser = cls.p.chromium.launch() + cls.context = cls.browser.new_context() + cls.context.set_default_timeout(500_000) + + def setUp(self): + self.page = TestLeaflet.context.new_page() + + def tearDown(self): + self.page.close() + + @classmethod + def tearDownClass(cls): + cls.context.close() + cls.browser.close() + cls.p.stop() + + def navigate_to_upload_page(self, page): + base_url = 'http://localhost:8000/' + page.goto(base_url) + page.get_by_role('link', name='Upload a leaflet').click() + page.get_by_text('Take a photo of a leaflet').click() + + def fill_postcode_and_submit(self, page, postcode='SW1A AA'): + page.get_by_label('What postcode was this').click() + page.get_by_label('What postcode was this').fill(postcode) + page.get_by_role('button', name='Submit').click() + page.get_by_text('Green Party').click() + page.get_by_role('button', name='Submit').click() + page.get_by_text('Thank you so much!') + + def test_basic_upload(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) + + 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) + + 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_media/test_images/test_leaflet.docx') + self.page.get_by_role('button', name='Continue').click() + self.page.get_by_text('Error during upload tap to retry') + + def test_read_leaflet_from_latest_leaflets(self): + base_url = 'http://localhost:8000/' + self.page.goto(base_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') + + if __name__ == "__main__": + unittest.main(verbosity=2)