-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
65 lines (50 loc) · 2.16 KB
/
conftest.py
File metadata and controls
65 lines (50 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import pytest
from selenium import webdriver
from datetime import datetime
from utils.utils import take_screenshot
#Defing the variables used in different modules
def pytest_configure(config):
"""Hook to configure the test report settings."""
config.option.html_path = f"reports/test_report_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.html"
config.option.self_contained_html = True # Embed assets into the report
def pytest_html_report_title(report):
"""Customize the title of the HTML report."""
report.title = "Automated SauceDemo Test Report"
def pytest_html_results_summary(prefix, summary, postfix):
"""Customize the summary section of the HTML report."""
prefix.extend(["<p><strong>Test Executed By:</strong> Nikesh(QA Team)</p>"])
prefix.extend(["<p><strong>Project:</strong> SauceDemo Selenium Automation Suite</p>"])
def pytest_html_results_table_header(cells):
"""Modify the report table header."""
cells.insert(2, "Description") # Adding a new column for test descriptions
def pytest_html_results_table_row(report, cells):
"""Modify the report table row."""
cells.insert(2, report.nodeid) # Populate the test description column
@pytest.fixture(scope="function")
def driver():
"""Fixture to initialise Webdriver and clean up after test."""
URL_LOGIN = "https://www.saucedemo.com/"
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(URL_LOGIN)
yield driver
driver.quit()
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item,call):
"""
Pytest hook to take screenshots when a test fails
"""
# Execute the test first.
outcome = yield
report = outcome.get_result()
test_name = report.nodeid.replace("::","_").replace("/",'_').replace('.py','') #Clean up for the test name
# Check if the test failed
if report.when == "call" and report.failed:
# Get the driver
try:
driver =item.funcargs.get("driver", None)
if driver:
take_screenshot(driver, test_name)
except Exception as e:
print(f"Failed to capture the screenshot :{e}") # error message