Skip to content

Commit

Permalink
E2E Test stabilisation
Browse files Browse the repository at this point in the history
  • Loading branch information
szymon-kellton committed Nov 7, 2024
1 parent 077cb30 commit 2537a26
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 72 deletions.
29 changes: 26 additions & 3 deletions tests/selenium/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,17 @@ def create_session(host: str, username: str, password: str, csrf: str = "") -> o


@pytest.fixture(scope="session")
def driver() -> Chrome:
def download_path(worker_id: str) -> str:
try:
worker_id = worker_id
assert worker_id is not None
yield f"{settings.DOWNLOAD_DIRECTORY}/{worker_id}"
except BaseException:
yield settings.DOWNLOAD_DIRECTORY


@pytest.fixture(scope="session")
def driver(download_path: str) -> Chrome:
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
Expand All @@ -222,15 +232,19 @@ def driver() -> Chrome:
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument("--disable-dev-shm-usage")
try:
os.makedirs(settings.DOWNLOAD_DIRECTORY)
os.makedirs(download_path)
except FileExistsError:
pass
prefs = {
"download.default_directory": settings.DOWNLOAD_DIRECTORY,
"download.default_directory": download_path,
}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=chrome_options)
yield driver
# try:
# shutil.rmtree(download_path)
# except FileNotFoundError:
# pass


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -514,6 +528,15 @@ def business_area() -> BusinessArea:
yield business_area


@pytest.fixture
def clear_downloaded_files(download_path: str) -> None:
for file in os.listdir(download_path):
os.remove(os.path.join(download_path, file))
yield
for file in os.listdir(download_path):
os.remove(os.path.join(download_path, file))


@pytest.fixture
def change_super_user(business_area: BusinessArea) -> None:
user = User.objects.filter(email="[email protected]").first()
Expand Down
2 changes: 1 addition & 1 deletion tests/selenium/helpers/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class Common:
DEFAULT_TIMEOUT = 10
DEFAULT_TIMEOUT = 20

def __init__(self, driver: Chrome):
self.driver = driver
Expand Down
2 changes: 1 addition & 1 deletion tests/selenium/page_object/base_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def selectGlobalProgramFilter(self, name: str) -> None:
self.getGlobalProgramFilterSearchInput().clear()
self.getGlobalProgramFilterSearchInput().send_keys(Keys.CONTROL + "a") # Select all (use COMMAND on Mac)
self.getGlobalProgramFilterSearchInput().send_keys(Keys.DELETE)
for _ in range(len(self.getGlobalProgramFilterSearchInput().get_attribute("value"))):
for _ in range(len(self.getGlobalProgramFilterSearchInput().get_attribute("value"))): # type: ignore
self.getGlobalProgramFilterSearchInput().send_keys(Keys.BACKSPACE)
self.getGlobalProgramFilterSearchButton().click()
if name != "All Programmes":
Expand Down
2 changes: 1 addition & 1 deletion tests/selenium/page_object/grievance/grievance_tickets.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def getTabSystemGenerated(self) -> WebElement:
def getTabUserGenerated(self) -> WebElement:
return self.wait_for(self.tabUserGenerated)

def getTicketListRow(self) -> WebElement:
def getTicketListRow(self) -> [WebElement]:
self.wait_for(self.ticketListRow)
return self.get_elements(self.ticketListRow)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def getStatus(self) -> [WebElement]:
def getStatusContainer(self) -> WebElement:
return self.wait_for(self.statusContainer)

def waitForStatusContainer(self, status: str) -> []:
return self.wait_for_text(status, self.statusContainer)
def waitForStatusContainer(self, status: str, timeout: int = 20) -> []:
return self.wait_for_text(status, self.statusContainer, timeout=timeout)

def getLabelHousehold(self) -> WebElement:
return self.wait_for(self.labelHousehold)
Expand Down
26 changes: 8 additions & 18 deletions tests/selenium/payment_module/test_payment_plans.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,6 @@ def create_targeting(create_test_program: Program) -> None:
)


@pytest.fixture
def clear_downloaded_files() -> None:
for file in os.listdir(settings.DOWNLOAD_DIRECTORY):
os.remove(os.path.join(settings.DOWNLOAD_DIRECTORY, file))
yield
for file in os.listdir(settings.DOWNLOAD_DIRECTORY):
os.remove(os.path.join(settings.DOWNLOAD_DIRECTORY, file))


@pytest.fixture
def create_payment_plan(create_targeting: None) -> PaymentPlan:
tp = TargetPopulation.objects.get(program__name="Test Program")
Expand Down Expand Up @@ -447,6 +438,7 @@ def test_payment_plan_happy_path(
pageNewPaymentPlan: NewPaymentPlan,
pageProgramCycle: ProgramCyclePage,
pageProgramCycleDetails: ProgramCycleDetailsPage,
download_path: str,
) -> None:
targeting = TargetPopulation.objects.first()
pageProgramCycle.selectGlobalProgramFilter("Test Program")
Expand Down Expand Up @@ -521,23 +513,21 @@ def test_payment_plan_happy_path(
pagePaymentModule.driver.refresh()
pagePaymentModuleDetails.getButtonDownloadXlsx().click()

zip_file = find_file(".zip", number_of_ties=15)
with zipfile.ZipFile(os.path.join(settings.DOWNLOAD_DIRECTORY, zip_file), "r") as zip_ref:
zip_ref.extractall(settings.DOWNLOAD_DIRECTORY)
zip_file = find_file(".zip", number_of_ties=15, search_in_dir=download_path)
with zipfile.ZipFile(os.path.join(download_path, zip_file), "r") as zip_ref:
zip_ref.extractall(download_path)

xlsx_file = find_file(".xlsx")
wb1 = openpyxl.load_workbook(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file))
xlsx_file = find_file(".xlsx", search_in_dir=download_path)
wb1 = openpyxl.load_workbook(os.path.join(download_path, xlsx_file))
ws1 = wb1.active
for cell in ws1["N:N"]:
if cell.row >= 2:
ws1.cell(row=cell.row, column=16, value=cell.value)

wb1.save(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file))
wb1.save(os.path.join(download_path, xlsx_file))

pagePaymentModuleDetails.getButtonUploadReconciliationInfo().click()
pagePaymentModuleDetails.upload_file(
os.path.abspath(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file)), timeout=120
)
pagePaymentModuleDetails.upload_file(os.path.abspath(os.path.join(download_path, xlsx_file)), timeout=120)
pagePaymentModuleDetails.getButtonImportSubmit().click()
pagePaymentModuleDetails.checkStatus("FINISHED")
assert "14 (100%)" in pagePaymentModuleDetails.getLabelReconciled().text
Expand Down
46 changes: 22 additions & 24 deletions tests/selenium/payment_verification/test_payment_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pytest
from dateutil.relativedelta import relativedelta
from selenium.webdriver.common.by import By
from sorl.thumbnail.conf import settings

from hct_mis_api.apps.account.models import User
from hct_mis_api.apps.core.fixtures import DataCollectingTypeFactory
Expand Down Expand Up @@ -250,12 +249,12 @@ def payment_verification_creator(channel: str = PaymentVerificationPlan.VERIFICA


@pytest.fixture
def clear_downloaded_files() -> None:
for file in os.listdir(settings.DOWNLOAD_DIRECTORY):
os.remove(os.path.join(settings.DOWNLOAD_DIRECTORY, file))
def clear_downloaded_files(download_path: str) -> None:
for file in os.listdir(download_path):
os.remove(os.path.join(download_path, file))
yield
for file in os.listdir(settings.DOWNLOAD_DIRECTORY):
os.remove(os.path.join(settings.DOWNLOAD_DIRECTORY, file))
for file in os.listdir(download_path):
os.remove(os.path.join(download_path, file))


@pytest.mark.usefixtures("login")
Expand Down Expand Up @@ -663,6 +662,7 @@ def test_payment_verification_xlsx_successful(
add_payment_verification_xlsx: PV,
pagePaymentVerification: PaymentVerification,
pagePaymentVerificationDetails: PaymentVerificationDetails,
download_path: str,
pagePaymentRecord: PaymentRecord,
) -> None:
pagePaymentVerification.selectGlobalProgramFilter("Active Program")
Expand All @@ -680,24 +680,24 @@ def test_payment_verification_xlsx_successful(

pagePaymentVerificationDetails.getDownloadXlsx().click()

xlsx_file = find_file(".xlsx", number_of_ties=10)
wb1 = openpyxl.load_workbook(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file))
xlsx_file = find_file(".xlsx", number_of_ties=10, search_in_dir=download_path)
wb1 = openpyxl.load_workbook(os.path.join(download_path, xlsx_file))
ws1 = wb1.active
for cell in ws1["N:N"]:
if cell.row >= 2:
ws1.cell(row=cell.row, column=3, value="YES")
ws1.cell(row=cell.row, column=16, value=ws1.cell(row=cell.row, column=15).value)

wb1.save(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file))
wb1.save(os.path.join(download_path, "new_" + xlsx_file))
find_file("new_" + xlsx_file, number_of_ties=10, search_in_dir=download_path)
pagePaymentVerificationDetails.getImportXlsx().click()

pagePaymentVerificationDetails.upload_file(
os.path.abspath(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file)), timeout=120
os.path.abspath(os.path.join(download_path, "new_" + xlsx_file)), timeout=120
)

pagePaymentVerificationDetails.getButtonImportEntitlement().click()

assert pagePaymentRecord.waitForStatusContainer("RECEIVED")
assert pagePaymentRecord.waitForStatusContainer("RECEIVED", timeout=60)
assert "RECEIVED" == pagePaymentRecord.getStatusContainer().text

def test_payment_verification_xlsx_partially_successful(
Expand All @@ -708,6 +708,7 @@ def test_payment_verification_xlsx_partially_successful(
pagePaymentVerification: PaymentVerification,
pagePaymentVerificationDetails: PaymentVerificationDetails,
pagePaymentRecord: PaymentRecord,
download_path: str,
) -> None:
pagePaymentVerification.selectGlobalProgramFilter("Active Program")

Expand All @@ -730,20 +731,18 @@ def test_payment_verification_xlsx_partially_successful(

pagePaymentVerificationDetails.getDownloadXlsx().click()

xlsx_file = find_file(".xlsx", number_of_ties=10)
wb1 = openpyxl.load_workbook(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file))
xlsx_file = find_file(".xlsx", number_of_ties=10, search_in_dir=download_path)
wb1 = openpyxl.load_workbook(os.path.join(download_path, xlsx_file))
ws1 = wb1.active
for cell in ws1["N:N"]:
if cell.row >= 2:
ws1.cell(row=cell.row, column=3, value="YES")
ws1.cell(row=cell.row, column=16, value=float(quantity) - 1.0)

wb1.save(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file))
wb1.save(os.path.join(download_path, xlsx_file))
pagePaymentVerificationDetails.getImportXlsx().click()

pagePaymentVerificationDetails.upload_file(
os.path.abspath(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file)), timeout=120
)
pagePaymentVerificationDetails.upload_file(os.path.abspath(os.path.join(download_path, xlsx_file)), timeout=120)

pagePaymentVerificationDetails.getButtonImportEntitlement().click()

Expand All @@ -757,6 +756,7 @@ def test_payment_verification_xlsx_not_received(
pagePaymentVerification: PaymentVerification,
pagePaymentVerificationDetails: PaymentVerificationDetails,
pagePaymentRecord: PaymentRecord,
download_path: str,
) -> None:
pagePaymentVerification.selectGlobalProgramFilter("Active Program")

Expand All @@ -773,20 +773,18 @@ def test_payment_verification_xlsx_not_received(

pagePaymentVerificationDetails.getDownloadXlsx().click()

xlsx_file = find_file(".xlsx", number_of_ties=10)
wb1 = openpyxl.load_workbook(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file))
xlsx_file = find_file(".xlsx", number_of_ties=10, search_in_dir=download_path)
wb1 = openpyxl.load_workbook(os.path.join(download_path, xlsx_file))
ws1 = wb1.active
for cell in ws1["N:N"]:
if cell.row >= 2:
ws1.cell(row=cell.row, column=3, value="NO")
ws1.cell(row=cell.row, column=16, value=0)

wb1.save(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file))
wb1.save(os.path.join(download_path, xlsx_file))
pagePaymentVerificationDetails.getImportXlsx().click()

pagePaymentVerificationDetails.upload_file(
os.path.abspath(os.path.join(settings.DOWNLOAD_DIRECTORY, xlsx_file)), timeout=120
)
pagePaymentVerificationDetails.upload_file(os.path.abspath(os.path.join(download_path, xlsx_file)), timeout=120)

pagePaymentVerificationDetails.getButtonImportEntitlement().click()

Expand Down
10 changes: 5 additions & 5 deletions tests/selenium/people/test_people_periodic_data_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from datetime import datetime
from time import sleep

from django.conf import settings

import pytest
from dateutil.relativedelta import relativedelta

Expand Down Expand Up @@ -55,10 +53,12 @@


@pytest.fixture
def clear_downloaded_files() -> None:
def clear_downloaded_files(download_path: str) -> None:
for file in os.listdir(download_path):
os.remove(os.path.join(download_path, file))
yield
for file in os.listdir(settings.DOWNLOAD_DIRECTORY):
os.remove(os.path.join(settings.DOWNLOAD_DIRECTORY, file))
for file in os.listdir(download_path):
os.remove(os.path.join(download_path, file))


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ def test_create_programme_back_scenarios(
assert "Test Name" in pageProgrammeManagement.getInputProgrammeName().get_attribute("value")
pageProgrammeManagement.getInputProgrammeName().send_keys(Keys.CONTROL, "a")
pageProgrammeManagement.getInputProgrammeName().send_keys(Keys.DELETE)
for _ in range(len(pageProgrammeManagement.getInputProgrammeName().get_attribute("value"))):
pageProgrammeManagement.getInputProgrammeName().send_keys(Keys.BACKSPACE)
assert "Programme Name is required" in pageProgrammeManagement.getLabelProgrammeName().text.split("\n")
pageProgrammeManagement.getInputProgrammeName().send_keys(test_data["program_name"])
# 2nd step (Time Series Fields)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
from time import sleep

from django.conf import settings

import pytest
from selenium.webdriver.common.by import By

Expand Down Expand Up @@ -32,10 +30,12 @@


@pytest.fixture
def clear_downloaded_files() -> None:
def clear_downloaded_files(download_path: str) -> None:
for file in os.listdir(download_path):
os.remove(os.path.join(download_path, file))
yield
for file in os.listdir(settings.DOWNLOAD_DIRECTORY):
os.remove(os.path.join(settings.DOWNLOAD_DIRECTORY, file))
for file in os.listdir(download_path):
os.remove(os.path.join(download_path, file))


@pytest.fixture
Expand Down Expand Up @@ -114,7 +114,7 @@ def test_periodic_data_template_export_and_download(
string_attribute: FlexibleAttribute,
pageIndividuals: Individuals,
individual: Individual,
clear_downloaded_files: None,
download_path: str,
) -> None:
populate_pdu_with_null_values(program, individual.flex_fields)
individual.save()
Expand Down Expand Up @@ -149,9 +149,7 @@ def test_periodic_data_template_export_and_download(
pageIndividuals.getDownloadBtn(periodic_data_update_template.pk).click()
periodic_data_update_template.refresh_from_db()
assert (
pageIndividuals.check_file_exists(
os.path.join(settings.DOWNLOAD_DIRECTORY, periodic_data_update_template.file.file.name)
)
pageIndividuals.check_file_exists(os.path.join(download_path, periodic_data_update_template.file.file.name))
is True
)

Expand Down Expand Up @@ -260,6 +258,7 @@ def test_periodic_data_template_create_and_download(
pagePeriodicDataUpdateTemplates: PeriodicDatUpdateTemplates,
pagePeriodicDataUpdateTemplatesDetails: PeriodicDatUpdateTemplatesDetails,
individual: Individual,
download_path: str,
) -> None:
populate_pdu_with_null_values(program, individual.flex_fields)
individual.save()
Expand Down Expand Up @@ -294,8 +293,6 @@ def test_periodic_data_template_create_and_download(
pageIndividuals.getDownloadBtn(periodic_data_update_template.pk).click()
periodic_data_update_template.refresh_from_db()
assert (
pageIndividuals.check_file_exists(
os.path.join(settings.DOWNLOAD_DIRECTORY, periodic_data_update_template.file.file.name)
)
pageIndividuals.check_file_exists(os.path.join(download_path, periodic_data_update_template.file.file.name))
is True
)
Loading

0 comments on commit 2537a26

Please sign in to comment.