Skip to content

Commit 545152a

Browse files
committed
add exception wrapping
1 parent f35aec8 commit 545152a

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

conftest.py

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,68 @@
11
import logging
22
import os
33
import platform
4+
import datetime
45
from typing import Callable, List, Tuple
56

67
import pytest
78
from selenium import webdriver
89
from selenium.common.exceptions import WebDriverException
910
from selenium.webdriver.firefox.options import Options
11+
from selenium.webdriver import Firefox
1012

13+
def screenshot_content(driver: Firefox, opt_ci: bool, test_name: str) -> None:
14+
"""
15+
Screenshots the current browser, saves with appropriate test name and date for reference
16+
"""
17+
artifacts_loc = "artifacts" if opt_ci else ""
18+
current_time = str(datetime.datetime.now())
19+
filename = f"{test_name}_{current_time}_image"
20+
if not filename.endswith(".png"):
21+
filename = filename + ".png"
22+
artifacts_loc = ""
23+
if opt_ci:
24+
artifacts_loc = "artifacts"
25+
fullpath = os.path.join(artifacts_loc, filename)
26+
driver.save_screenshot(fullpath)
27+
return
28+
29+
def log_content(opt_ci: bool, driver: Firefox, test_name: str) -> None:
30+
"""
31+
Logs the current browser content, with the appropriate test name and date for reference.
32+
"""
33+
artifacts_loc = "artifacts" if opt_ci else ""
34+
current_time = str(datetime.datetime.now())
35+
fullpath_chrome = os.path.join(artifacts_loc, f"{test_name}_{current_time}_content.html")
36+
fullpath_content = os.path.join(artifacts_loc, f"{test_name}_{current_time}_chrome.html")
37+
38+
# Save Chrome context page source
39+
with open(fullpath_chrome, "w") as fh:
40+
with driver.context(driver.CONTEXT_CHROME):
41+
output_contents = driver.page_source
42+
fh.write(output_contents)
43+
44+
# Save Content context page source
45+
with open(fullpath_content, "w") as fh:
46+
output_contents = driver.page_source.replace("><", ">\n<")
47+
fh.write(output_contents)
48+
return
1149

1250
def pytest_exception_interact(node, call, report):
51+
"""
52+
Method that wraps all test execution, on any exception/failure an artifact with the information about the failure is kept.
53+
"""
1354
if report.failed:
1455
try:
15-
# Attempt to access the driver fixture from the node
56+
test_name = node.name
57+
logging.info(f"Handling exception for test: {test_name}")
1658
driver = node.funcargs["driver"]
17-
# Now you can interact with the driver, for example, log HTML content
59+
opt_ci = node.funcargs["opt_ci"]
1860
if driver:
19-
print("Logging current page HTML:", driver.page_source)
20-
except:
21-
pass
61+
log_content(opt_ci, driver, test_name)
62+
screenshot_content(driver, opt_ci, test_name)
63+
except Exception as e:
64+
logging.warning("Something went wrong with the exception catching.")
65+
raise e
2266

2367

2468
def pytest_addoption(parser):
@@ -140,7 +184,7 @@ def driver(
140184
set_prefs: List[Tuple],
141185
opt_ci: bool,
142186
opt_window_size: str,
143-
env_prep,
187+
env_prep
144188
):
145189
"""
146190
Return the webdriver object.
@@ -235,3 +279,27 @@ def faker_seed():
235279
@pytest.fixture(scope="session")
236280
def fillable_pdf_url():
237281
return "https://www.uscis.gov/sites/default/files/document/forms/i-9.pdf"
282+
283+
284+
def log_page_content(driver: webdriver.Firefox, opt_ci: bool):
285+
"""
286+
Function that saves the html content into the artifacts on a failed test
287+
"""
288+
def _log_page_content(opt_ci: bool):
289+
artifacts_loc = "artifacts" if opt_ci else ""
290+
fullpath_chrome = os.path.join(artifacts_loc, "page_source_chrome.html")
291+
fullpath_content = os.path.join(artifacts_loc, "page_source_content.html")
292+
293+
# Save Chrome context page source
294+
with open(fullpath_chrome, "w") as fh:
295+
driver.switch_to.context(driver.CONTEXT_CHROME)
296+
output_contents = driver.page_source.replace("><", ">\n<")
297+
fh.write(output_contents)
298+
299+
# Save Content context page source
300+
with open(fullpath_content, "w") as fh:
301+
driver.switch_to.context(driver.CONTEXT_CONTENT)
302+
output_contents = driver.page_source.replace("><", ">\n<")
303+
fh.write(output_contents)
304+
305+
return _log_page_content(opt_ci)

tests/security_and_privacy/test_file.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)