本文介绍了捕获失败的测试用例的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Selenium + Python中捕获失败的测试用例的屏幕截图?我可以获取页面的屏幕截图,但是在捕获失败的测试用例的屏幕截图时会遇到困难.

How can I capture a screenshot of the failed test cases in Selenium + Python? I am able to get screenshots of the page but is facing difficulty in capturing screenshots of failed test cases.

代码:

def test_2new(self):

   driver = self.driver driver.get("google.com")
   driver.find_element_by_name("q").send_keys("Automation")
   driver.set_page_load_timeout(10)
   driver.find_element_by_name("btnK").click()
   self.assertTrue("something.com" in self.driver.current_url)
   print("Google Search")

@classmethod
def tearDownClass(cls):

    # cls.driver.refresh()
    time.sleep(5)
    if sys.exc_info()[0]:
         cls.driver.save_screenshot(r"C:\Users\ANCP2\sample.png")

推荐答案

您可以这样做:

def test_2new(self):

    driver = self.driver
    driver.get("google.com")
    driver.set_page_load_timeout(10)
    driver.find_element_by_name("q").send_keys("Automation")
    driver.find_element_by_name("btnK").click()

    try:

        self.assertTrue("something.com" in self.driver.current_url)
        print("Google Search!")

    except AssertionError:

        driver.save_screenshot(r"C:\Users\ANCP2\sample.png")

希望它对您有帮助!

这篇关于捕获失败的测试用例的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 21:48