We use the pytest library for automation test. We need to take screen shots for fail case. I want to use mydriver
variable in pytest_runtest_makereport
method to take screen shots with this variable for fail case. How can I do this?
Additional: We don't use the conftest.py file. Can we use the pytest_runtest_makereport
method without conftest.py? We use just a sampleTest.py file.
sampleTest.py:
import time
from appium import webdriver
import pytest
@pytest.yield_fixture(scope="function")
def driver():
"""Setup for the test"""
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '11.0'
desired_caps['deviceName'] = 'Samsung Galaxy A70'
# Since the app is already installed launching it using package and activity name
desired_caps['appPackage'] = 'com.example.mapkitbaseline'
desired_caps['appActivity'] = 'com.example.mapkitbaseline.ui.CreationMapActivity'
mydriver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
yield mydriver
mydriver.quit()
@pytest.mark.usefixtures("driver")
def test_func1(driver):
""""Testing the HMS MapKit demo app"""
driver.implicitly_wait(30)
time.sleep(5)
# Assert that MapKit->.ui.CreationMapActivity->btnMapCreation4 button text is equal to "suuportfragmentx"
elmnt = driver.find_element_by_id('com.example.mapkitbaseline:id/btnMapCreation4')
assert elmnt.get_attribute('text').lower() == "supportmapfragmentfffffffffx"
print(elmnt.get_attribute('text'))
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == "call":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
# I need to use mydriver variable this here. This code doesn't work.
screenshot = mydriver.get_screenshot_as_base64()
extra.append(pytest_html.extras.image(screenshot, ''))
# extra.append(pytest_html.extras.html('<style> #results-table{ position: relative; position:absolute !important;top:300px !important; left:100px !important; width: 50% !important; z-index: -1 !important;}</style>'))
report.extra = extra
from How to pass fixture object to pytest_runtest_makereport?
No comments:
Post a Comment