-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
54 lines (44 loc) · 1.86 KB
/
conftest.py
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
import pytest
from selenium import webdriver
driver = None
def pytest_addoption(parser):
parser.addoption("--browser_name", action="store", default="chrome")
@pytest.fixture(scope="class")
def setup(request):
global driver
browser_name = request.config.getoption("--browser_name")
if browser_name == "chrome":
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
elif browser_name == "firefox":
driver = webdriver.Firefox(executable_path="C:\\geckodriver.exe")
elif browser_name == "ie":
driver = webdriver.Ie(executable_path="C:\\IEDriverServer.exe")
elif browser_name == "edge":
driver = webdriver.Edge(executable_path="C:\\msedgedriver.exe")
driver.maximize_window()
driver.get("https://rahulshettyacademy.com/angularpractice/")
request.cls.driver = driver
yield
driver.close()
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
Extends the PyTest Plugin to take and embed screenshot in html report, whenever test fails.
:param item:
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call' or report.when == "setup":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
file_name = report.nodeid.replace("::", "_") + ".png"
_capture_screenshot(file_name)
if file_name:
html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
'onclick="window.open(this.src)" align="right"/></div>' % file_name
extra.append(pytest_html.extras.html(html))
report.extra = extra
def _capture_screenshot(name):
driver.get_screenshot_as_file(name)