Skip to content

Commit 0e2596e

Browse files
init
1 parent fed0e1f commit 0e2596e

14 files changed

+104
-202
lines changed

conftest.py

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import pytest
2+
from playwright.sync_api import sync_playwright
23

34
from libs import CurrentExecution as ce
45
from libs import file_ops as fo
5-
from libs.constants import workflow_type
66
from libs.wrappers import *
77

88

@@ -12,31 +12,82 @@ def pytest_addoption(parser):
1212

1313
@pytest.fixture(scope="session")
1414
def start_exe_session(request):
15-
_browser_name = request.config.getoption("browser_or_device")
16-
ce.current_browser_name = _browser_name
1715
ce.get_env_values()
16+
ce.reset_environment()
1817
ce.session_screenshots_dir = create_session_screenshot_dir()
19-
ce.start_browser(browser_name=_browser_name)
20-
yield
21-
ce.quit_browser()
18+
ce.current_browser_name = request.config.getoption("browser_or_device")
2219

2320

24-
@pytest.fixture
25-
def create_browser_page(start_exe_session):
26-
ce.start_test(w_type=workflow_type.APPLICATION)
27-
yield ce.page
28-
ce.end_test()
21+
@pytest.fixture(scope="session")
22+
def start_playwright():
23+
with sync_playwright() as _playwright:
24+
_playwright.selectors.set_test_id_attribute("data-qa")
25+
yield _playwright
26+
27+
28+
@pytest.fixture()
29+
def start_mavis(start_exe_session, start_playwright):
30+
_browser, _context = start_browser(pw=start_playwright, browser_or_device=ce.current_browser_name)
31+
ce.page = _context.new_page()
32+
ce.page.goto(url=ce.service_url)
33+
yield
34+
close_browser(browser=_browser, page=ce.page)
2935

3036

3137
@pytest.fixture
32-
def start_consent_workflow(start_exe_session):
33-
ce.start_test(w_type=workflow_type.PARENTAL_CONSENT)
34-
yield ce.page
35-
ce.end_test()
38+
def start_consent_workflow(start_exe_session, start_playwright):
39+
_browser, _context = start_browser(pw=start_playwright, browser_or_device=ce.current_browser_name)
40+
ce.page = _context.new_page()
41+
ce.page.goto(url=ce.parental_consent_url)
42+
yield
43+
close_browser(browser=_browser, page=ce.page)
3644

3745

3846
def create_session_screenshot_dir() -> str:
3947
if ce.capture_screenshot_flag:
4048
_session_name = f"{get_new_datetime()}-{ce.current_browser_name}"
4149
fo.file_operations().create_dir(dir_path=f"screenshots/{_session_name}")
4250
return f"screenshots/{_session_name}"
51+
52+
53+
def start_browser(pw, browser_or_device: str):
54+
_http_credentials = {
55+
"username": ce.base_auth_username,
56+
"password": ce.base_auth_password,
57+
}
58+
try:
59+
match browser_or_device.lower():
60+
case "iphone_12":
61+
_browser = pw.webkit.launch(headless=ce.headless_mode)
62+
_context = _browser.new_context(**pw.devices["iPhone 12"], http_credentials=_http_credentials)
63+
case "iphone_11":
64+
_browser = pw.chromium.launch(channel="chrome", headless=ce.headless_mode)
65+
_context = _browser.new_context(**pw.devices["iPhone 11"], http_credentials=_http_credentials)
66+
case "pixel_5":
67+
_browser = pw.webkit.launch(headless=ce.headless_mode)
68+
_context = _browser.new_context(**pw.devices["Pixel 5"], http_credentials=_http_credentials)
69+
case "s9+":
70+
_browser = pw.chromium.launch(channel="chromium", headless=ce.headless_mode)
71+
_context = _browser.new_context(**pw.devices["Galaxy S9+"], http_credentials=_http_credentials)
72+
case "chrome":
73+
_browser = pw.chromium.launch(channel="chrome", headless=ce.headless_mode)
74+
_context = _browser.new_context(http_credentials=_http_credentials)
75+
case "msedge":
76+
_browser = pw.chromium.launch(channel="msedge", headless=ce.headless_mode)
77+
_context = _browser.new_context(http_credentials=_http_credentials)
78+
case "firefox":
79+
_browser = pw.firefox.launch(headless=ce.headless_mode)
80+
_context = _browser.new_context(http_credentials=_http_credentials)
81+
case _: # Desktop Chromium for all other cases
82+
_browser = pw.chromium.launch(headless=ce.headless_mode)
83+
_context = _browser.new_context(http_credentials=_http_credentials)
84+
return _browser, _context
85+
except Exception as e:
86+
raise AssertionError(f"Error launching {browser_or_device}: {e}")
87+
88+
89+
def close_browser(browser, page):
90+
if page.get_by_role("button", name="Log out").is_visible():
91+
page.get_by_role("button", name="Log out").click()
92+
page.close()
93+
browser.close()

libs/__init__.py

Lines changed: 4 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import os
22

33
from dotenv import load_dotenv
4-
from playwright.sync_api import sync_playwright
54

65
from libs import api_ops
7-
from libs.constants import workflow_type
86

97

108
class CurrentExecution:
119
page = None
12-
environment = None
10+
service_url: str = ""
1311
base_auth_username: str = ""
1412
base_auth_password: str = ""
1513
current_browser_name: str = ""
1614
headless_mode: bool = False
17-
playwright: bool = None
1815
session_screenshots_dir: str = ""
1916
screenshot_sequence: int = 0
2017
capture_screenshot_flag: bool = False
@@ -24,18 +21,10 @@ class CurrentExecution:
2421
reset_endpoint: str = ""
2522
api_token: str = ""
2623

27-
@staticmethod
28-
def start_test(w_type: workflow_type):
29-
CurrentExecution.start_page(w_type=w_type)
30-
31-
@staticmethod
32-
def end_test():
33-
CurrentExecution.close_page()
34-
3524
@staticmethod
3625
def get_env_values():
3726
load_dotenv()
38-
CurrentExecution.environment = os.getenv("TEST_URL")
27+
CurrentExecution.service_url = os.getenv("TEST_URL")
3928
CurrentExecution.base_auth_username = os.getenv("TEST_USERNAME")
4029
CurrentExecution.base_auth_password = os.getenv("TEST_PASSWORD")
4130
CurrentExecution.login_username = os.getenv("LOGIN_USERNAME")
@@ -47,144 +36,6 @@ def get_env_values():
4736
CurrentExecution.api_token = os.getenv("API_TOKEN")
4837

4938
@staticmethod
50-
def start_browser(browser_name: str):
51-
CurrentExecution.playwright = sync_playwright().start()
52-
CurrentExecution.playwright.selectors.set_test_id_attribute("data-qa")
53-
match browser_name.lower():
54-
case "chromium":
55-
CurrentExecution.launch_chromium()
56-
case "msedge":
57-
CurrentExecution.launch_edge()
58-
case "firefox":
59-
CurrentExecution.launch_firefox()
60-
case "chrome": # Google Chrome for all other cases
61-
CurrentExecution.launch_chrome()
62-
case _: # Mobile browsers
63-
CurrentExecution.launch_mobile_browser(device_name=browser_name)
64-
65-
@staticmethod
66-
def start_page(w_type: workflow_type):
67-
CurrentExecution.page = CurrentExecution.context.new_page()
68-
match w_type.lower():
69-
case workflow_type.PARENTAL_CONSENT:
70-
CurrentExecution.page.goto(url=CurrentExecution.parental_consent_url)
71-
case _:
72-
CurrentExecution.reset_upload_data()
73-
CurrentExecution.page.goto(url=CurrentExecution.environment)
74-
75-
@staticmethod
76-
def quit_browser():
77-
CurrentExecution.browser.close()
78-
79-
@staticmethod
80-
def close_page():
81-
if CurrentExecution.page.get_by_role("button", name="Log out").is_visible():
82-
CurrentExecution.page.get_by_role("button", name="Log out").click()
83-
CurrentExecution.page.close()
84-
85-
@staticmethod
86-
def launch_chromium():
87-
try:
88-
CurrentExecution.browser = CurrentExecution.playwright.chromium.launch(
89-
headless=CurrentExecution.headless_mode, args=["--fullscreen"]
90-
)
91-
CurrentExecution.context = CurrentExecution.browser.new_context(
92-
http_credentials={
93-
"username": CurrentExecution.base_auth_username,
94-
"password": CurrentExecution.base_auth_password,
95-
}
96-
)
97-
except Exception as e:
98-
raise AssertionError(f"Error launching Chromium: {e}")
99-
100-
@staticmethod
101-
def launch_edge():
102-
try:
103-
CurrentExecution.browser = CurrentExecution.playwright.chromium.launch(
104-
channel="msedge", headless=CurrentExecution.headless_mode, args=["--fullscreen"]
105-
)
106-
CurrentExecution.context = CurrentExecution.browser.new_context(
107-
http_credentials={
108-
"username": CurrentExecution.base_auth_username,
109-
"password": CurrentExecution.base_auth_password,
110-
}
111-
)
112-
113-
except Exception as e:
114-
raise AssertionError(f"Error launching Edge: {e}")
115-
116-
@staticmethod
117-
def launch_firefox():
118-
try:
119-
CurrentExecution.browser = CurrentExecution.playwright.firefox.launch(
120-
headless=CurrentExecution.headless_mode, args=["--fullscreen"]
121-
)
122-
CurrentExecution.context = CurrentExecution.browser.new_context(
123-
http_credentials={
124-
"username": CurrentExecution.base_auth_username,
125-
"password": CurrentExecution.base_auth_password,
126-
}
127-
)
128-
129-
except Exception as e:
130-
raise AssertionError(f"Error launching Firefox: {e}")
131-
132-
@staticmethod
133-
def launch_chrome():
134-
try:
135-
CurrentExecution.browser = CurrentExecution.playwright.chromium.launch(
136-
channel="chrome", headless=CurrentExecution.headless_mode, args=["--fullscreen"]
137-
)
138-
CurrentExecution.context = CurrentExecution.browser.new_context(
139-
http_credentials={
140-
"username": CurrentExecution.base_auth_username,
141-
"password": CurrentExecution.base_auth_password,
142-
}
143-
)
144-
145-
except Exception as e:
146-
raise AssertionError(f"Error launching Chrome: {e}")
147-
148-
@staticmethod
149-
def launch_mobile_browser(device_name: str):
150-
_http_credentials = {
151-
"username": CurrentExecution.base_auth_username,
152-
"password": CurrentExecution.base_auth_password,
153-
}
154-
try:
155-
match device_name.lower():
156-
case "iphone_12":
157-
CurrentExecution.browser = CurrentExecution.playwright.webkit.launch(
158-
headless=CurrentExecution.headless_mode
159-
)
160-
CurrentExecution.context = CurrentExecution.browser.new_context(
161-
**CurrentExecution.playwright.devices["iPhone 12"], http_credentials=_http_credentials
162-
)
163-
case "iphone_11":
164-
CurrentExecution.browser = CurrentExecution.playwright.chromium.launch(
165-
channel="chrome", headless=CurrentExecution.headless_mode
166-
)
167-
CurrentExecution.context = CurrentExecution.browser.new_context(
168-
**CurrentExecution.playwright.devices["iPhone 11"], http_credentials=_http_credentials
169-
)
170-
case "pixel_5":
171-
CurrentExecution.browser = CurrentExecution.playwright.webkit.launch(
172-
headless=CurrentExecution.headless_mode
173-
)
174-
CurrentExecution.context = CurrentExecution.browser.new_context(
175-
**CurrentExecution.playwright.devices["Pixel 5"], http_credentials=_http_credentials
176-
)
177-
case _:
178-
CurrentExecution.browser = CurrentExecution.playwright.chromium.launch(
179-
channel="chromium", headless=CurrentExecution.headless_mode
180-
)
181-
CurrentExecution.context = CurrentExecution.browser.new_context(
182-
**CurrentExecution.playwright.devices["Galaxy S9+"], http_credentials=_http_credentials
183-
)
184-
except Exception as e:
185-
raise AssertionError(f"Error launching device browser {device_name}: {e}")
186-
187-
@staticmethod
188-
def reset_upload_data():
39+
def reset_environment():
18940
_headers = {"Authorization": CurrentExecution.api_token}
190-
# _ = api_ops.api_operations().api_get(endpoint=CurrentExecution.reset_endpoint, header=_headers, param=None)
41+
_ = api_ops.api_operations().api_get(endpoint=CurrentExecution.reset_endpoint, header=_headers, param=None)

requirements.txt

1.15 KB
Binary file not shown.

tests/test_0_smoke.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_smoke_verify_packages(self):
3535
# CHECK APPLICATION ACCESS
3636
@pytest.mark.smoke
3737
@pytest.mark.order(3)
38-
def test_smoke_homepage_loads(self, create_browser_page):
38+
def test_smoke_homepage_loads(self, start_mavis):
3939
self.po.verify(
4040
locator="heading",
4141
property=object_properties.TEXT,

tests/test_1_login.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class Test_Regression_Login:
1717
@pytest.mark.mobile
1818
@pytest.mark.order(101)
1919
@pytest.mark.parametrize("user,pwd,expected_message", test_parameters)
20-
def test_reg_invalid_login(self, create_browser_page, user, pwd, expected_message):
20+
def test_reg_invalid_login(self, start_mavis, user, pwd, expected_message):
2121
self.login_page.perform_invalid_login(user=user, pwd=pwd, expected_message=expected_message)
2222

2323
@pytest.mark.login
2424
@pytest.mark.mobile
2525
@pytest.mark.order(102)
26-
def test_reg_home_page_links(self, create_browser_page):
26+
def test_reg_home_page_links(self, start_mavis):
2727
self.login_page.perform_valid_login()
2828
self.dashboard_page.verify_all_expected_links()

tests/test_2_sessions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ class Test_Regression_Sessions:
1010

1111
@pytest.mark.sessions
1212
@pytest.mark.order(201)
13-
def test_reg_create_valid_session(self, create_browser_page):
13+
def test_reg_create_valid_session(self, start_mavis):
1414
self.login_page.perform_valid_login()
1515
self.dashboard_page.click_sessions()
1616
self.sessions_page.schedule_a_valid_session()
1717

1818
@pytest.mark.sessions
1919
@pytest.mark.order(202)
20-
def test_reg_delete_all_sessions(self, create_browser_page):
20+
def test_reg_delete_all_sessions(self, start_mavis):
2121
self.login_page.perform_valid_login()
2222
self.dashboard_page.click_sessions()
2323
self.sessions_page.delete_all_sessions()
2424

2525
@pytest.mark.sessions
2626
@pytest.mark.order(203)
27-
def test_reg_create_invalid_session(self, create_browser_page):
27+
def test_reg_create_invalid_session(self, start_mavis):
2828
self.login_page.perform_valid_login()
2929
self.dashboard_page.click_sessions()
3030
self.sessions_page.create_invalid_session()

tests/test_3_consent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_reg_parental_consent_workflow(self, start_consent_workflow, scenario_da
2222
@pytest.mark.consent
2323
@pytest.mark.mobile
2424
@pytest.mark.order(302)
25-
def test_reg_gillick_consent(self, create_browser_page):
25+
def test_reg_gillick_consent(self, start_mavis):
2626
self.login_page.perform_valid_login()
2727
self.dashboard_page.click_sessions()
2828
self.sessions_page.set_gillick_competency_for_student()

tests/test_4_cohorts.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ class Test_Regression_Cohorts:
1111

1212
@pytest.mark.cohorts
1313
@pytest.mark.order(401)
14-
def test_reg_cohort_upload_positive(self, create_browser_page):
14+
def test_reg_cohort_upload_positive(self, start_mavis):
1515
self.login_page.perform_valid_login()
1616
self.dashboard_page.click_programmes()
1717
self.programmes_page.upload_cohorts(file_paths=test_data_file_paths.COHORTS_POSITIVE)
1818

1919
@pytest.mark.cohorts
2020
@pytest.mark.order(402)
21-
def test_reg_cohort_upload_negative(self, create_browser_page):
21+
def test_reg_cohort_upload_negative(self, start_mavis):
2222
self.login_page.perform_valid_login()
2323
self.dashboard_page.click_programmes()
2424
self.programmes_page.upload_cohorts(file_paths=test_data_file_paths.COHORTS_NEGATIVE)
2525

2626
@pytest.mark.cohorts
2727
@pytest.mark.order(403)
28-
def test_reg_cohorts_file_structure(self, create_browser_page):
28+
def test_reg_cohorts_file_structure(self, start_mavis):
2929
self.login_page.perform_valid_login()
3030
self.dashboard_page.click_programmes()
3131
self.programmes_page.upload_invalid_cohorts(file_paths=test_data_file_paths.COHORTS_INVALID_STRUCTURE)
3232

3333
@pytest.mark.cohorts
3434
@pytest.mark.order(404)
35-
def test_reg_cohorts_no_record(self, create_browser_page):
35+
def test_reg_cohorts_no_record(self, start_mavis):
3636
self.login_page.perform_valid_login()
3737
self.dashboard_page.click_programmes()
3838
self.programmes_page.upload_invalid_cohorts(file_paths=test_data_file_paths.COHORTS_HEADER_ONLY)
3939

4040
@pytest.mark.cohorts
4141
@pytest.mark.order(405)
42-
def test_reg_cohorts_empty_file(self, create_browser_page):
42+
def test_reg_cohorts_empty_file(self, start_mavis):
4343
self.login_page.perform_valid_login()
4444
self.dashboard_page.click_programmes()
4545
self.programmes_page.upload_invalid_cohorts(file_paths=test_data_file_paths.COHORTS_EMPTY_FILE)

0 commit comments

Comments
 (0)