在Selenium python中无法看到测试用例的失败屏幕截图

dgtucam1  于 2023-02-21  发布在  Python
关注(0)|答案(1)|浏览(159)

我尝试使用www.example.com _screenshot()方法保存selenium python项目中失败测试用例的屏幕截图driver.save。我指定了我的文件夹作为参数。但是在我的项目中看不到失败的屏幕截图。以下是代码。

self.driver.save_screenshot(".//Screenshots//"+"test_homePageTitle.png")

以下是绝对路径-/Users/cherry/Documents/Selenium pj/nopcommerceApp2/Screenshots
我想将失败的测试用例保存为特定文件夹中的屏幕截图
下面是完整的代码:

import time
from pageObjects.loginPage import LoginPage
from utilities.readProperties import ReadConfig

class Test_001_Login:
    baseURL = ReadConfig.getApplicationURL()
    username = ReadConfig.getUseremail()
    password = ReadConfig.getPassword()

    def test_homePageTitle(self, setup):
        self.driver = setup
        self.driver.get(self.baseURL)
        act_title = self.driver.title
        self.driver.close()
        if act_title == "Your store. Login":
            assert True
        else:
            time.sleep(5)
            # self.driver.save_screenshot("./nopcommerceApp2/Screenshots" + "test_homePageTitle.png")
            self.driver.save_screenshots('.//Screenshots//"+"test_homePageTitle.png')
            self.driver.close()
            assert False

    def test_login(self, setup):
        self.driver = setup
        self.driver.get(self.baseURL)
        time.sleep(10)
        self.lp = LoginPage(self.driver)
        self.lp.setUserName(self.username)
        self.lp.setPassword(self.password)
        self.lp.clickLogin()
        act_title = self.driver.title
        self.driver.close()
        if act_title == "Dashboard / nopCommerce administration":
            assert True
        else:
          time.sleep(5)
            self.driver.save_screenshot(".\\Screenshots\\" + "test_login.png")
            self.driver.close()
            assert False
mkshixfv

mkshixfv1#

这不是一个可持续的方式。2取而代之的是,你可以使用这样的报告插件。
我可以建议你pytest-html

pip install pytest-html

然后将愚蠢的代码添加到www.example.com中conftest.py

# to add screenshot for failed steps
@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_state = hasattr(report, 'wasxfail')
        if (report.skipped and xfail_state) or (report.failed and not xfail_state):
            mydriver = item.funcargs['driver']
            screenshot = mydriver.get_screenshot_as_base64()
            extra.append(pytest_html.extras.image(screenshot, ''))
    report.extra = extra

相关问题