Помогите пожалуйста с получением скриншотов при падении ui тестов.
conftest.py имеет следующий код:
import allure
from selenium import webdriver
import pytest
import os
import time
from datetime import datetime
import pytest
import os
from selenium import webdriver as selenium_webdriver
from selenium.webdriver.chrome.options import Options
@pytest.fixture
def setup(browser, headless):
if browser == 'chrome':
os_name = os.name
this_folder = os.path.dirname(os.path.abspath(__file__))
full_folder = this_folder + "/Drivers/"
#setup chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("no-sandbox")
if headless == "yes":
chrome_options.add_argument("--headless")
if os_name == "nt":
full_path = os.path.join(full_folder, 'chromedriver.exe')
selenium_webdriver = webdriver.Chrome(full_path, chrome_options=chrome_options)
else:
full_path = os.path.join(full_folder, 'chromedriver_linux')
selenium_webdriver = webdriver.Chrome(full_path, chrome_options=chrome_options)
elif browser=='firefox':
driver = webdriver.Firefox('c://seleniumdrivers//geckodriver.exe')
print("Launching firefox browser.........")
selenium_webdriver.implicitly_wait(10)
selenium_webdriver.maximize_window()
yield selenium_webdriver
selenium_webdriver.quit()
# This will get the value from CLI /hooks
def pytest_addoption(parser):
parser.addoption('--browser', default='chrome')
parser.addoption('--environment', default='https://dev.test.ru/')
parser.addoption('--headless', default='no')
@pytest.fixture()
def browser(request):
# This will return the Browser value to setup method
return request.config.getoption("--browser")
@pytest.fixture()
def headless(request):
# This will return the Headless value to setup method
return request.config.getoption("--headless")
@pytest.fixture()
def environment(request):
# This will return the Environment value to test methodes
return request.config.getoption("--environment")
########### pytest HTML Report ################
# It is hook for Adding Environment info to HTML Report
def pytest_configure(config):
config._metadata['Project Name'] = 'HP'
config._metadata['Module Name'] = 'Authorization'
config._metadata['QA Engineer'] = 'Jon'
# It is hook for delete/Modify Environment info to HTML Report
@pytest.mark.optionalhook
def pytest_metadata(metadata):
metadata.pop("JAVA_HOME", None)
metadata.pop("Plugins", None)
я пытаюсь добавить следующий метод для снятия скриншотов при падении тестов и добавлении скриншота в allure репорт
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
rep = outcome.get_result()
if rep.when == 'call' and rep.failed:
mode = 'a' if os.path.exists('failures') else 'w'
try:
selenium_webdriver.save_screenshot("d:\screens")
allure.attach(
selenium_webdriver.get_screenshot_as_png(),
name='screenshot',
attachment_type=allure.attachment_type.PNG
)
except Exception as e:
print('Fail to take screen-shot: {}'.format(e))
но selenium_webdriver в данном методе не инициализирован, это не тот экземпляр который в методе setup. Прошу подсказать как правильно изменить данный класс, чтобы добавить снятие скриншотов.
Вот фрагмент одного теста:
@allure.feature('test_add_favorite_attribute')
def test_add_favotite_attribute_for_comment(self, setup, environment):
self.driver = setup
self.live_type_edit_message = LiveTypeEditMessagePage(self.driver)
self.login_page = LoginPage(self.driver)
self.live_tape = LiveTapeList(self.driver)
self.live_tape_message_page = LiveTapeViewMessagePage(self.driver)
with allure.step('I open the login form'):
self.driver.get(environment)
with allure.step('I fill out the login form'):
self.login_page.setUserName(self.userlogin)
self.login_page.setUserPassword(self.password)
self.login_page.clickLogin()
with allure.step('I choose a company'):
...