|
| 1 | +from playwright.sync_api import Page, expect |
| 2 | +from pages.base_page import BasePage |
| 3 | +import pytest |
| 4 | +import logging |
| 5 | + |
| 6 | +class GenerateInvitations(BasePage): |
| 7 | + def __init__(self, page: Page): |
| 8 | + super().__init__(page) |
| 9 | + self.page = page |
| 10 | + # Generate Invitations - page links |
| 11 | + self.generate_invitations_button = self.page.get_by_role("button", name="Generate Invitations") |
| 12 | + self.display_rs = self.page.locator("#displayRS") |
| 13 | + self.refresh_button = self.page.get_by_role("button", name="Refresh") |
| 14 | + self.planned_invitations_total = self.page.locator("#col8_total") |
| 15 | + self.self_referrals_total = self.page.locator("#col9_total") |
| 16 | + self.generate_invitations_title = self.page.locator("#ntshPageTitle") |
| 17 | + |
| 18 | + def click_generate_invitations_button(self)->None: |
| 19 | + self.click(self.generate_invitations_button) |
| 20 | + |
| 21 | + def click_refresh_button(self)->None: |
| 22 | + self.click(self.refresh_button) |
| 23 | + |
| 24 | + def verify_generate_invitations_title(self) -> None: |
| 25 | + expect(self.generate_invitations_title).to_contain_text("Generate Invitations") |
| 26 | + |
| 27 | + def verify_invitation_generation_progress_title(self) -> None: |
| 28 | + expect(self.generate_invitations_title).to_contain_text("Invitation Generation Progress") |
| 29 | + |
| 30 | + def wait_for_invitation_generation_complete(self) -> bool: |
| 31 | + """ |
| 32 | + This function is used to wait for the invitations to be generated. |
| 33 | + Every 5 seconds it refreshes the table and checks to see if the invitations have been generated. |
| 34 | + It also checks that enough invitations were generated and checks to see if self referrals are present |
| 35 | + """ |
| 36 | + self.page.wait_for_selector("#displayRS", timeout=5000) |
| 37 | + |
| 38 | + if self.planned_invitations_total == "0": |
| 39 | + pytest.fail("There are no planned invitations to generate") |
| 40 | + |
| 41 | + # Initially, ensure the table contains "Queued" |
| 42 | + expect(self.display_rs).to_contain_text("Queued") |
| 43 | + |
| 44 | + # Set timeout parameters |
| 45 | + timeout = 120000 # Total timeout of 120 seconds (in milliseconds) |
| 46 | + wait_interval = 5000 # Wait 5 seconds between refreshes (in milliseconds) |
| 47 | + elapsed = 0 |
| 48 | + |
| 49 | + # Loop until the table no longer contains "Queued" |
| 50 | + logging.info("Waiting for successful generation") |
| 51 | + while elapsed < timeout: # there may be a stored procedure to speed this process up |
| 52 | + table_text = self.display_rs.text_content() |
| 53 | + if "Failed" in table_text: |
| 54 | + pytest.fail("Invitation has failed to generate") |
| 55 | + elif "Queued" in table_text or "In Progress" in table_text: |
| 56 | + # Click the Refresh button |
| 57 | + self.click_refresh_button() |
| 58 | + self.page.wait_for_timeout(wait_interval) |
| 59 | + elapsed += wait_interval |
| 60 | + else: |
| 61 | + break |
| 62 | + |
| 63 | + # Final check: ensure that the table now contains "Completed" |
| 64 | + try: |
| 65 | + expect(self.display_rs).to_contain_text("Completed") |
| 66 | + logging.info("Invitations successfully generated") |
| 67 | + logging.info(f"Invitations took {elapsed / 1000} seconds to generate") |
| 68 | + except Exception as e: |
| 69 | + pytest.fail(f"Invitations not generated successfully: {str(e)}") |
| 70 | + |
| 71 | + value = self.planned_invitations_total.text_content().strip() # Get text and remove extra spaces |
| 72 | + if int(value) < 5: |
| 73 | + pytest.fail("There are less than 5 invitations generated") |
| 74 | + |
| 75 | + self_referrals_total = int(self.self_referrals_total.text_content().strip()) |
| 76 | + if self_referrals_total >= 1: |
| 77 | + return True |
| 78 | + else: |
| 79 | + logging.warning("No S1 Digital Leaflet batch will be generated") |
| 80 | + return False |
0 commit comments