Python 中 Selenium Webdriver 测试失败时的自动截图 [英] Automatic screenshots when test fail by Selenium Webdriver in Python

查看:67
本文介绍了Python 中 Selenium Webdriver 测试失败时的自动截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的 webdriver 测试失败(任何异常或断言错误),我想自动捕获屏幕截图.我正在使用 Python unittest 和 Selenium Webdriver.有没有人可以解决这个问题?

I want to automatic capturing screenshots if my webdriver tests failed (any exception or assertion error). I am using Python unittest and Selenium Webdriver. Does anyone have any solution to this problem?

推荐答案

在 Firefox 中做一些 webdriver 的东西...保存任何异常的截图到一个过时的图像文件:

do some webdriver stuff in Firefox... save screenshot on any exception to a dated image file:

from datetime import datetime
from selenium import webdriver

browser = webdriver.Firefox()

try:
    # do some webdriver stuff here
except Exception as e:
    print e
    now = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
    browser.get_screenshot_as_file('screenshot-%s.png' % now)

这篇关于Python 中 Selenium Webdriver 测试失败时的自动截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆