Skip to content

Commit

Permalink
WIP playwright for TestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed Oct 2, 2024
1 parent d5821e9 commit 76a9dea
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions electionleaflets/apps/leaflets/tests/test_frontend.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 76a9dea

Please sign in to comment.