1
1
import logging
2
2
import os
3
3
import platform
4
+ import datetime
4
5
from typing import Callable , List , Tuple
5
6
6
7
import pytest
7
8
from selenium import webdriver
8
9
from selenium .common .exceptions import WebDriverException
9
10
from selenium .webdriver .firefox .options import Options
11
+ from selenium .webdriver import Firefox
10
12
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
11
49
12
50
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
+ """
13
54
if report .failed :
14
55
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 } " )
16
58
driver = node .funcargs ["driver" ]
17
- # Now you can interact with the driver, for example, log HTML content
59
+ opt_ci = node . funcargs [ "opt_ci" ]
18
60
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
22
66
23
67
24
68
def pytest_addoption (parser ):
@@ -140,7 +184,7 @@ def driver(
140
184
set_prefs : List [Tuple ],
141
185
opt_ci : bool ,
142
186
opt_window_size : str ,
143
- env_prep ,
187
+ env_prep
144
188
):
145
189
"""
146
190
Return the webdriver object.
@@ -235,3 +279,27 @@ def faker_seed():
235
279
@pytest .fixture (scope = "session" )
236
280
def fillable_pdf_url ():
237
281
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 )
0 commit comments