Skip to content

Commit c44931c

Browse files
authored
Merge pull request #388 from NHSDigital/fix-safari-downloads
Fix Safari tests
2 parents c16b066 + c1d95b5 commit c44931c

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

mavis/test/pages/programmes.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pandas as pd
22
from playwright.sync_api import Page, expect
3+
from io import StringIO
34

45
from ..data import TestData
56
from ..models import ReportFormat, Programme
@@ -129,12 +130,24 @@ def verify_report_format(self, programme: Programme, report_format: ReportFormat
129130
def _download_and_verify_report_headers(self, expected_headers: str):
130131
_file_path = f"working/rpt_{get_current_datetime()}.csv"
131132

132-
with self.page.expect_download() as download_info:
133-
self.continue_button.click()
134-
download = download_info.value
135-
download.save_as(_file_path)
133+
browser = getattr(self.page.context, "browser", None)
134+
browser_type_name = getattr(
135+
getattr(browser, "browser_type", None), "name", None
136+
)
137+
138+
# Playwrights webkit browser always opens CSVs in the browser, unlike Chromium and Firefox
139+
if browser_type_name == "webkit":
140+
self.click_continue()
141+
csv_content = self.page.locator("pre").inner_text()
142+
_actual_df = pd.read_csv(StringIO(csv_content))
143+
self.page.go_back()
144+
else:
145+
with self.page.expect_download() as download_info:
146+
self.click_continue()
147+
download = download_info.value
148+
download.save_as(_file_path)
149+
_actual_df = pd.read_csv(_file_path)
136150

137-
_actual_df = pd.read_csv(_file_path)
138151
actual_headers = ",".join(_actual_df.columns.tolist())
139152
_e_not_a = [
140153
h for h in expected_headers.split(",") if h not in actual_headers.split(",")

mavis/test/pages/school_moves.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from datetime import date
2-
from pathlib import Path
32
from typing import Optional
3+
import pandas as pd
4+
from pandas import DataFrame
5+
from io import StringIO
46

57
from ..step import step
68

@@ -67,10 +69,22 @@ def enter_date_range(
6769

6870
self.continue_button.click()
6971

70-
def confirm(self) -> Path:
71-
with self.page.expect_download() as download_info:
72+
def confirm(self) -> DataFrame:
73+
browser = getattr(self.page.context, "browser", None)
74+
browser_type_name = getattr(
75+
getattr(browser, "browser_type", None), "name", None
76+
)
77+
78+
# Playwrights webkit browser always opens CSVs in the browser, unlike Chromium and Firefox
79+
if browser_type_name == "webkit":
7280
self.confirm_button.click()
73-
return download_info.value.path()
81+
csv_content = self.page.locator("pre").inner_text()
82+
self.page.go_back()
83+
return pd.read_csv(StringIO(csv_content))
84+
else:
85+
with self.page.expect_download() as download_info:
86+
self.confirm_button.click()
87+
return pd.read_csv(download_info.value.path())
7488

7589

7690
class ReviewSchoolMovePage:

tests/test_school_moves.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import pandas
21
from playwright.sync_api import expect
32
from mavis.test.data import ClassFileMapping
43
import pytest
@@ -91,9 +90,9 @@ def test_download(
9190
):
9291
school_moves_page.click_download()
9392
download_school_moves_page.enter_date_range()
94-
path = download_school_moves_page.confirm()
93+
school_moves_csv = download_school_moves_page.confirm()
9594

96-
actual_headers = set(pandas.read_csv(path).columns)
95+
actual_headers = set(school_moves_csv.columns)
9796
expected_headers = {
9897
"NHS_REF",
9998
"SURNAME",

0 commit comments

Comments
 (0)