3
3
import os
4
4
import platform
5
5
import re
6
- import sys
7
6
from typing import Callable , List , Tuple
8
7
9
8
import pytest
@@ -19,18 +18,10 @@ def screenshot_content(driver: Firefox, opt_ci: bool, test_name: str) -> None:
19
18
"""
20
19
Screenshots the current browser, saves with appropriate test name and date for reference
21
20
"""
22
- artifacts_loc = "artifacts" if opt_ci else ""
23
21
current_time = str (datetime .datetime .now ())
24
22
current_time = re .sub (r"[^\w_. -]" , "_" , current_time )
25
23
filename = f"{ test_name } _{ current_time } _image"
26
- if not filename .endswith (".png" ):
27
- filename = filename + ".png"
28
- artifacts_loc = ""
29
- if opt_ci :
30
- artifacts_loc = "artifacts"
31
- fullpath = os .path .join (artifacts_loc , filename )
32
- driver .save_screenshot (fullpath )
33
- return
24
+ _screenshot (filename , driver , opt_ci )
34
25
35
26
36
27
def log_content (opt_ci : bool , driver : Firefox , test_name : str ) -> None :
@@ -67,10 +58,9 @@ def pytest_exception_interact(node, call, report):
67
58
if report .failed :
68
59
try :
69
60
test_name = node .name
70
- logging .info (f"Handling exception for test: { test_name } " )
71
- print (
72
- f"NODE LOGS HERE { node .funcargs } \n THE FAILED TEST: { test_name } " ,
73
- file = sys .stderr ,
61
+ logging .error (f"Handling exception for test: { test_name } " )
62
+ logging .error (
63
+ f"NODE LOGS HERE { node .funcargs } \n THE FAILED TEST: { test_name } "
74
64
)
75
65
driver = node .funcargs .get ("driver" )
76
66
opt_ci = node .funcargs .get ("opt_ci" )
@@ -127,6 +117,17 @@ def pytest_addoption(parser):
127
117
)
128
118
129
119
120
+ def _screenshot (filename : str , driver : Firefox , opt_ci : bool ):
121
+ if not filename .endswith (".png" ):
122
+ filename = filename + ".png"
123
+ artifacts_loc = ""
124
+ if opt_ci :
125
+ artifacts_loc = "artifacts"
126
+ fullpath = os .path .join (artifacts_loc , filename )
127
+ driver .save_screenshot (fullpath )
128
+ return fullpath
129
+
130
+
130
131
@pytest .fixture ()
131
132
def opt_headless (request ):
132
133
return request .config .getoption ("--run-headless" )
@@ -267,20 +268,10 @@ def screenshot(driver: webdriver.Firefox, opt_ci: bool) -> Callable:
267
268
Factory fixture that returns a screenshot function.
268
269
"""
269
270
270
- def _screenshot (filename : str ) -> str :
271
- """
272
- Given a short filename, save a screenshot and return the image's full path.
273
- """
274
- if not filename .endswith (".png" ):
275
- filename = filename + ".png"
276
- artifacts_loc = ""
277
- if opt_ci :
278
- artifacts_loc = "artifacts"
279
- fullpath = os .path .join (artifacts_loc , filename )
280
- driver .save_screenshot (fullpath )
281
- return fullpath
271
+ def screenshot_wrapper (filename : str ) -> str :
272
+ return _screenshot (filename , driver , opt_ci )
282
273
283
- return _screenshot
274
+ return screenshot_wrapper
284
275
285
276
286
277
@pytest .fixture ()
@@ -296,28 +287,3 @@ def faker_seed():
296
287
@pytest .fixture (scope = "session" )
297
288
def fillable_pdf_url ():
298
289
return "https://www.uscis.gov/sites/default/files/document/forms/i-9.pdf"
299
-
300
-
301
- def log_page_content (driver : webdriver .Firefox , opt_ci : bool ):
302
- """
303
- Function that saves the html content into the artifacts on a failed test
304
- """
305
-
306
- def _log_page_content (opt_ci : bool ):
307
- artifacts_loc = "artifacts" if opt_ci else ""
308
- fullpath_chrome = os .path .join (artifacts_loc , "page_source_chrome.html" )
309
- fullpath_content = os .path .join (artifacts_loc , "page_source_content.html" )
310
-
311
- # Save Chrome context page source
312
- with open (fullpath_chrome , "w" ) as fh :
313
- driver .switch_to .context (driver .CONTEXT_CHROME )
314
- output_contents = driver .page_source .replace ("><" , ">\n <" )
315
- fh .write (output_contents )
316
-
317
- # Save Content context page source
318
- with open (fullpath_content , "w" ) as fh :
319
- driver .switch_to .context (driver .CONTEXT_CONTENT )
320
- output_contents = driver .page_source .replace ("><" , ">\n <" )
321
- fh .write (output_contents )
322
-
323
- return _log_page_content (opt_ci )
0 commit comments