Skip to content

Commit 4228991

Browse files
New: [AEA-4609] - Move UI tests into existing regression test pack (#182)
## Summary - ✨ New Feature ### Details Added playwright and an example test for the CPST-UI
1 parent 6583d21 commit 4228991

File tree

8 files changed

+145
-33
lines changed

8 files changed

+145
-33
lines changed

.github/workflows/regression_tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ on:
1616
description: 'The product we are testing'
1717
type: choice
1818
options:
19+
- CPTS-UI
1920
- EPS-FHIR
2021
- EPS-FHIR-PRESCRIBING
2122
- EPS-FHIR-DISPENSING

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ repos:
6060
language: system
6161

6262
fail_fast: false
63-
default_stages: [pre-commit, commit]
63+
default_stages: [pre-commit, pre-commit]

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Change the `env` variable accordingly to either `INT` or `INTERNAL-DEV`.
8282
If you wish to test a different product i.e. `PFP-APIGEE` then you must change `product=` and `--tags` respectively.
8383

8484
### Method 5:
85-
Run the tests by pushing changes to github in a pull request and running the regression tests job.
85+
Run the tests by pushing changes to github in a pull request and running the regression tests job.
8686
You can do this by the browser or by running this
8787
```
8888
BRANCH=fix_tests_take_2
@@ -114,12 +114,20 @@ This process will stop after the first program detects an error or if Black modi
114114
You may need to run this multiple times to ensure everything is ok before committing.
115115

116116

117-
### Generating the allure report from a github test run
118-
To generate and view the results of a github test run, first authenticate to github by running this and following instructions
117+
### Generating the allure report from a GitHub test run
118+
To generate and view the results of a GitHub test run, first authenticate to GitHub by running this and following instructions
119119
```
120120
gh auth login
121121
```
122-
Then download the alluere results by noting the github run id in a browser and running this
122+
Then download the allure results by noting the GitHub run ID in a browser and running this
123123
```
124-
GITHUB_RUN_ID=11523235428 make download-allure-report
124+
GITHUB_RUN_ID=11523235428 make download-allure-report
125125
```
126+
127+
# UI testing
128+
This pack has recently been updated to include UI-based testing using Playwright for CPTS-UI. It will run headless using the Chrome browser
129+
130+
## Recording new tests:
131+
Playwright contains a handy (but not perfect) feature which will record actions you make and give you the code for them
132+
to begin, run the command: <br />
133+
`playwright codegen`

features/cpts_ui/home_page.feature

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@cpts_ui @home @regression @blocker @smoke @ui
2+
@allure.tms:https://nhsd-jira.digital.nhs.uk/browse/AEA-4460
3+
Feature: I can visit the Clinical Prescription Tracker Service Website
4+
Scenario: user can navigate to the Clinical Prescription Tracker Service Website homepage
5+
When I go to the homepage
6+
Then I am on the homepage

features/environment.py

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@
55

66
from behave.model import Scenario
77
from dotenv import load_dotenv
8-
8+
from playwright.sync_api import sync_playwright
99
from methods.api import eps_api_methods
1010

1111
load_dotenv(override=True)
12-
12+
global _page
1313
INTERNAL_QA_BASE_URL = "https://internal-qa.api.service.nhs.uk/"
1414
INTERNAL_DEV_BASE_URL = "https://internal-dev.api.service.nhs.uk/"
1515
INT_BASE_URL = "https://int.api.service.nhs.uk/"
1616
SANDBOX_DEV_BASE_URL = "https://internal-dev-sandbox.api.service.nhs.uk/"
1717
SANDBOX_INT_BASE_URL = "https://sandbox.api.service.nhs.uk/"
1818
REF_BASE_URL = "https://ref.api.service.nhs.uk/"
1919

20+
AWS_BASE_URL = ".eps.national.nhs.uk/"
2021
PFP_AWS_PR_URL = "https://pfp-{{aws_pull_request_id}}.dev.eps.national.nhs.uk/"
2122
PFP_AWS_SANDBOX_PR_URL = (
2223
"https://pfp-{{aws_pull_request_id}}-sandbox.dev.eps.national.nhs.uk/"
2324
)
25+
CPTS_UI_PR_URL = "https://cpt-ui-{{aws_pull_request_id}}.dev.eps.national.nhs.uk/"
26+
CPTS_UI_SANDBOX_PR_URL = (
27+
"https://cpt-ui-{{aws_pull_request_id}}sandbox.dev.eps.national.nhs.uk/"
28+
)
2429

25-
26-
ENVS = {
30+
APIGEE_ENVS = {
2731
"INTERNAL-DEV": INTERNAL_DEV_BASE_URL,
2832
"INTERNAL-QA": INTERNAL_QA_BASE_URL,
2933
"INT": INT_BASE_URL,
@@ -32,15 +36,26 @@
3236
"SANDBOX": SANDBOX_INT_BASE_URL,
3337
}
3438

39+
AWS_ENVS = {
40+
"INTERNAL-DEV": f".dev{AWS_BASE_URL}",
41+
"INTERNAL-QA": f".qa{AWS_BASE_URL}",
42+
"INT": f".int{AWS_BASE_URL}",
43+
"REF": f".ref{AWS_BASE_URL}",
44+
"INTERNAL-DEV-SANDBOX": f".sandbox{AWS_BASE_URL}",
45+
"SANDBOX": f".sandbox.dev{AWS_BASE_URL}",
46+
}
47+
3548
CIS2_USERS = {
3649
"prescriber": {"user_id": "656005750107", "role_id": "555254242105"},
3750
"dispenser": {"user_id": "555260695103", "role_id": "555265434108"},
3851
}
3952
LOGIN_USERS = {"user_id": "9449304130"}
4053

4154
REPOS = {
55+
"CPTS-UI": "https://github.com/NHSDigital/eps-prescription-tracker-ui",
4256
"EPS-FHIR": "https://github.com/NHSDigital/electronic-prescription-service-api",
43-
# TODO Add EPS Dispensing and Prescribing repo URLs
57+
"EPS-FHIR-PRESCRIBING": "https://github.com/NHSDigital/electronic-prescription-service-api",
58+
"EPS-FHIR-DISPENSING": "https://github.com/NHSDigital/electronic-prescription-service-api",
4459
"PFP-APIGEE": "https://github.com/NHSDigital/prescriptions-for-patients",
4560
"PFP-AWS": "https://github.com/NHSDigital/prescriptionsforpatients",
4661
"PSU": "https://github.com/NHSDigital/eps-prescription-status-update-api",
@@ -54,6 +69,7 @@
5469
JWT_PRIVATE_KEY = os.getenv("JWT_PRIVATE_KEY")
5570
JWT_KID = os.getenv("JWT_KID")
5671

72+
CPTS_UI_PREFIX = "cpt-ui"
5773
EPS_FHIR_SUFFIX = "electronic-prescriptions"
5874
EPS_FHIR_PRESCRIBING_SUFFIX = "fhir-prescribing"
5975
EPS_FHIR_DISPENSING_SUFFIX = "fhir-dispensing"
@@ -76,25 +92,41 @@ def count_of_scenarios_to_run(context):
7692

7793

7894
def before_all(context):
95+
product = context.config.userdata["product"].upper()
7996
if count_of_scenarios_to_run(context) != 0:
8097
env = context.config.userdata["env"].upper()
81-
product = context.config.userdata["product"].upper()
82-
context.eps_fhir_base_url = os.path.join(select_base_url(env), EPS_FHIR_SUFFIX)
98+
context.cpts_ui_base_url = f"https://{CPTS_UI_PREFIX}" + select_aws_base_url(
99+
env
100+
)
101+
context.eps_fhir_base_url = os.path.join(
102+
select_apigee_base_url(env), EPS_FHIR_SUFFIX
103+
)
83104
context.eps_fhir_prescribing_base_url = os.path.join(
84-
select_base_url(env), EPS_FHIR_PRESCRIBING_SUFFIX
105+
select_apigee_base_url(env), EPS_FHIR_PRESCRIBING_SUFFIX
85106
)
86107
context.eps_fhir_dispensing_base_url = os.path.join(
87-
select_base_url(env), EPS_FHIR_DISPENSING_SUFFIX
108+
select_apigee_base_url(env), EPS_FHIR_DISPENSING_SUFFIX
88109
)
89-
context.pfp_base_url = os.path.join(select_base_url(env), PFP_SUFFIX)
90-
context.psu_base_url = os.path.join(select_base_url(env), PSU_SUFFIX)
110+
context.pfp_base_url = os.path.join(select_apigee_base_url(env), PFP_SUFFIX)
111+
context.psu_base_url = os.path.join(select_apigee_base_url(env), PSU_SUFFIX)
91112
if PULL_REQUEST_ID:
92113
pull_request_id = PULL_REQUEST_ID.lower()
93114
if "pr-" in pull_request_id:
94115
get_url_with_pr(context, env, product)
95116
else:
96117
raise RuntimeError("no tests to run. Check your tags and try again")
118+
if product == "CPTS-UI":
119+
global _page
120+
playwright = sync_playwright().start()
121+
browser = playwright.chromium.launch(
122+
headless=True, slow_mo=1000, channel="chrome"
123+
)
124+
context.page = browser.new_page()
125+
_page = context.page
126+
set_page(context, _page)
127+
97128
eps_api_methods.calculate_eps_fhir_base_url(context)
129+
print("CPTS-UI: ", context.cpts_ui_base_url)
98130
print("EPS: ", context.eps_fhir_base_url)
99131
print("EPS-PRESCRIBING: ", context.eps_fhir_prescribing_base_url)
100132
print("EPS-DISPENSING: ", context.eps_fhir_dispensing_base_url)
@@ -120,23 +152,44 @@ def get_url_with_pr(context, env, product):
120152
context.pfp_base_url = os.path.join(
121153
INTERNAL_DEV_BASE_URL, f"{PFP_SUFFIX}-{PULL_REQUEST_ID}"
122154
)
123-
if product == "PFP-AWS":
124-
context.pfp_base_url = os.path.join(
125-
INTERNAL_DEV_BASE_URL, f"{PFP_SUFFIX}-{PULL_REQUEST_ID}"
126-
)
127-
if env == "INTERNAL-DEV":
128-
context.pfp_base_url = PFP_AWS_PR_URL.replace(
129-
"{{aws_pull_request_id}}", PULL_REQUEST_ID
130-
)
131-
if env == "INTERNAL-DEV-SANDBOX":
132-
context.pfp_base_url = PFP_AWS_SANDBOX_PR_URL.replace(
133-
"{{aws_pull_request_id}}", PULL_REQUEST_ID
134-
)
135-
136155
if product == "PSU":
137156
context.psu_base_url = os.path.join(
138157
INTERNAL_DEV_BASE_URL, f"{PSU_SUFFIX}-{PULL_REQUEST_ID}"
139158
)
159+
if product == "PFP-AWS":
160+
handle_pfp_aws_pr_url(context, env)
161+
if product == "CPTS-UI":
162+
handle_cpt_ui_pr_url(context, env)
163+
164+
165+
def handle_cpt_ui_pr_url(context, env):
166+
assert PULL_REQUEST_ID is not None
167+
context.cpts_ui_base_url = (
168+
f"https://{CPTS_UI_PREFIX}-{PULL_REQUEST_ID}{select_apigee_base_url(env)}"
169+
)
170+
if env == "INTERNAL-DEV":
171+
context.cpts_ui_base_url = CPTS_UI_PR_URL.replace(
172+
"{{aws_pull_request_id}}", PULL_REQUEST_ID
173+
)
174+
if env == "INTERNAL-DEV-SANDBOX":
175+
context.cpts_ui_base_url = CPTS_UI_SANDBOX_PR_URL.replace(
176+
"{{aws_pull_request_id}}", PULL_REQUEST_ID
177+
)
178+
179+
180+
def handle_pfp_aws_pr_url(context, env):
181+
assert PULL_REQUEST_ID is not None
182+
context.pfp_base_url = os.path.join(
183+
INTERNAL_DEV_BASE_URL, f"{PFP_SUFFIX}-{PULL_REQUEST_ID}"
184+
)
185+
if env == "INTERNAL-DEV":
186+
context.pfp_base_url = PFP_AWS_PR_URL.replace(
187+
"{{aws_pull_request_id}}", PULL_REQUEST_ID
188+
)
189+
if env == "INTERNAL-DEV-SANDBOX":
190+
context.pfp_base_url = PFP_AWS_SANDBOX_PR_URL.replace(
191+
"{{aws_pull_request_id}}", PULL_REQUEST_ID
192+
)
140193

141194

142195
def after_all(context):
@@ -167,6 +220,8 @@ def after_all(context):
167220
if os.path.exists(directory_path) and os.path.isdir(directory_path):
168221
print(f"Directory '{directory_path}' exists. Deleting...")
169222
shutil.rmtree(directory_path)
223+
if _page:
224+
_page.close()
170225

171226

172227
def setup_logging(level: int = logging.INFO):
@@ -179,9 +234,16 @@ def setup_logging(level: int = logging.INFO):
179234
)
180235

181236

182-
def select_base_url(env):
183-
if env in ENVS:
184-
return ENVS[env]
237+
def select_apigee_base_url(env):
238+
if env in APIGEE_ENVS:
239+
return APIGEE_ENVS[env]
240+
else:
241+
raise ValueError(f"Unknown environment or missing base URL for: {env} .")
242+
243+
244+
def select_aws_base_url(env):
245+
if env in AWS_ENVS:
246+
return AWS_ENVS[env]
185247
else:
186248
raise ValueError(f"Unknown environment or missing base URL for: {env} .")
187249

@@ -199,3 +261,11 @@ def write_properties_file(file_path, properties_dict):
199261
with open(file_path, "w") as file:
200262
for key, value in properties_dict.items():
201263
file.write(f"{key}={value}\n")
264+
265+
266+
def get_page(self):
267+
return self._page
268+
269+
270+
def set_page(self, _page):
271+
self._page = _page

features/steps/home_page_steps.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# pylint: disable=no-name-in-module
2+
from behave import when, then # pyright: ignore [reportAttributeAccessIssue]
3+
from pages.home_page import HomePage
4+
5+
6+
@when("I go to the homepage")
7+
def goto_page(context):
8+
context.page.goto(context.cpts_ui_base_url + "site/")
9+
10+
11+
@then("I am on the homepage")
12+
def verify_on_page(context):
13+
home_page = HomePage(context.page)
14+
home_page.verify_header_link()

pages/home_page.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from playwright.sync_api import Page, expect
2+
3+
4+
class HomePage:
5+
6+
def __init__(self, page: Page):
7+
self.page = page
8+
9+
def verify_header_link(self):
10+
expect(
11+
self.page.get_by_role("link", name="Clinical prescription")
12+
).to_be_visible()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pytest-nhsd-apim = "^3.3.15"
2929
requests-oauthlib = "^2.0.0"
3030
nhs-number-generator = {git = "https://github.com/Iain-S/nhs_number_generator.git"}
3131
pyright = "^1.1.377"
32+
playwright = "^1.48.0"
3233

3334

3435
[tool.poetry.group.dev.dependencies]

0 commit comments

Comments
 (0)