forked from DionisIno/luma_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
90 lines (77 loc) · 3.42 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import json
import os
import shutil
import allure
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from locators.sign_in_page_locators import SingInPageLocators as sil
from locators.header_page_locators import HeaderPageLocators as hpl
from data.data_urls import MAIN_PAGE_URL
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
@pytest.fixture(scope="function")
def driver():
print('\nstart browser...')
chrome_options = Options()
if 'CI' in os.environ:
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=chrome_options)
driver = webdriver.Chrome(service=ChromeService(), options=chrome_options)
driver.set_window_size(1900, 1000)
else:
# driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=chrome_options)
driver = webdriver.Chrome(service=ChromeService())
driver.maximize_window()
yield driver
print('\nquit browser...')
driver.quit()
@pytest.fixture()
def wait(driver):
wait = WebDriverWait(driver, 15)
yield wait
@pytest.fixture(scope="session")
def config():
current_dir = os.path.dirname(os.path.abspath(__file__))
config_file = os.path.join(current_dir, "config.json")
with open(config_file) as f:
config = json.load(f)
return config
@pytest.fixture(scope="function")
def sing_in(driver, config):
driver.get(MAIN_PAGE_URL)
sing_in_button = wait(driver, config["timeout"]).until(EC.visibility_of_element_located(hpl.SIGN_IN))
sing_in_button.click()
email = wait(driver, config["timeout"]).until(EC.visibility_of_element_located(sil.CUSTOMER_EMAIL))
email.send_keys(config["email"])
password = wait(driver, config["timeout"]).until(EC.visibility_of_element_located(sil.CUSTOMER_PASSWORD))
password.send_keys(config["password"])
button = wait(driver, config["timeout"]).until(EC.element_to_be_clickable(sil.SIGN_IN_BUTTON))
button.click()
@pytest.fixture(scope="session", autouse=True)
def clear_allure_result_folder():
allure_result_folder = "allure_result"
if not os.path.exists(allure_result_folder):
os.makedirs(allure_result_folder)
else:
for file_name in os.listdir(allure_result_folder):
file_path = os.path.join(allure_result_folder, file_name)
if os.path.isfile(file_path):
os.remove(file_path)
yield
@allure.feature("Make a Screenshot")
def pytest_runtest_makereport(item, call):
if call.when == 'call':
if call.excinfo is not None:
try:
driver = item.funcargs['driver']
driver.save_screenshot('allure-results/screenshot.png')
allure.attach.file('allure-results/screenshot.png', name='Screenshot',
attachment_type=allure.attachment_type.PNG)
allure.attach(driver.page_source, name="HTML source", attachment_type=allure.attachment_type.HTML)
except Exception as e:
print(f"Failed to take screenshot: {e}")