-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconftest.py
50 lines (43 loc) · 1.65 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
import os
import shutil
from datetime import datetime
import allure
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
@pytest.fixture(scope='function')
def driver():
print('\nstart browser...')
chrome_options = Options()
if 'CI' in os.environ:
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
driver.set_window_size(1920, 1080)
else:
chrome_options.add_argument("--window-size=1920,1080")
# chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
yield driver
print('\nquit browser...')
driver.quit()
@pytest.fixture(scope="session", autouse=True)
def clear_allure_results_folder():
allure_report_dir = "allure-results"
if os.path.exists(allure_report_dir):
for file_name in os.listdir(allure_report_dir):
file_path = os.path.join(allure_report_dir, file_name)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print(f"Failed to delete {file_path}. Reason: {e}")
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item):
outcome = yield
report = outcome.get_result()
if report.when == 'call' and report.failed:
driver = item.funcargs['driver']
attach = driver.get_screenshot_as_png()
allure.attach(attach, name=f"Screenshot {datetime.today()}", attachment_type=allure.attachment_type.PNG)