pytest启动浏览器,失败用例截图
1、conftest.py
# coding:utf- from selenium import webdriver
import pytest driver = None @pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
当测试失败的时候,自动截图,展示到html报告中
:param item:
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', []) if report.when == 'call' or report.when == "setup":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
file_name = report.nodeid.replace("::", "_")+".png"
screen_img = _capture_screenshot()
if file_name:
html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:600px;height:300px;" ' \
'onclick="window.open(this.src)" align="right"/></div>' % screen_img
extra.append(pytest_html.extras.html(html))
report.extra = extra def _capture_screenshot():
'''
截图保存为base64,展示到html中
:return:
'''
return driver.get_screenshot_as_base64() @pytest.fixture(scope='session', autouse=True)
def browser():
global driver
if driver is None:
driver =webdriver.Chrome()
return driver
2、test_01.py
from selenium import webdriver
import time def test_yoyo_01(browser): browser.get("https://www.baidu.com/")
time.sleep(2)
t = browser.title
assert t == "郭林莉"
3、test_03.py
from selenium import webdriver
import time def test_yoyo_01(browser): browser.get("https://www.baidu.com/")
time.sleep(2)
t = browser.title
assert "baidu" in t
4、cmd运行用例:pytest --html=report.html --self-contained-html
5、运行结果:

pytest启动浏览器,失败用例截图的更多相关文章
- selenium webdriver启动IE浏览器失败的解决办法
通过selenium webdriver启动IE浏览器失败,报错:selenium.common.exceptions.WebDriverException: Message: Unexpected ...
- 执行testng appium用例失败,自动截图
新建一个截图监听类ScreenShotListener ,重写onTestFailure方法,里面定义了 监听的driver ,截图文件路径和名称 package com.fsssc.htsgl.ut ...
- Unittest 支持 case 失败后自动截图功能的另外两种方式
原生的unittest框架是不支持case失败后自动截图的功能的,网上看了大家的解决办法,大体上分为两种:1.要么加装饰器2.也有人封装断言这里我们看看还有没有其他的更加方便的方法值得大家一起探讨一下 ...
- 章节十七章、2- 给执行失败的case截图
一.案例演示 1.首先我们把截图的方法单独进行封装方便以后调用. package utilities; import java.io.File; import java.io.IOException; ...
- selenium是如何启动浏览器的
前几天有同学问到selenium是怎么样启动浏览器的(selenium启动浏览器的原理),当时稍微讲解了一下,不过自我感觉不够具体,现在特地把启动原理通过代码和一系列操作给串联起来,希望可以帮助大家更 ...
- Selenium2启动浏览器且加载插件
一.SELENIUM2启动浏览器 注意: SELENIUM2在启动浏览器时,都是启动一个干净的没有任务 插件及cookies信息的浏览器,即使是你之前的浏览器有设置过代理,到自动化启动时,也是没有代理 ...
- 启动OracleDBConsoleorcl失败,提示错误代码2
异常问题: 启动OracleDBConsoleorcl失败,提示错误代码2 原因分析: 由于更改计算机名导致的异常 解决方法: 1.管理员权限cmd下执行emctl start dbconsole 2 ...
- MyEclipse 启动 tomcate 失败 解决方法
MyEclipse中启动tomcate失败 十月 20, 2015 9:53:04 下午 org.apache.catalina.startup.VersionLoggerListener log信息 ...
- 基于Selenium2+Java的UI自动化(2) - 启动浏览器
一.准备工作 我们常用的浏览器主要有三个:chrome.Firefox.IE:其中chrome 和 IE 需要下载驱动程序,才能启动浏览器,注意驱动程序有32位和64位两种. 另外:如何查看本机的浏览 ...
随机推荐
- jq 倒计时
引入jq 注 在IE和safari 如果时间出现NAN 将时间格式改为YYYY/MM/DD HH:MM:SS 例:var d2 = new Date("2019/02/18 15:59&qu ...
- Digital Deletions HDU - 1404
Digital deletions is a two-player game. The rule of the game is as following. Begin by writing down ...
- Leetcode 714 - Node
1. 题目要求 Your are given an array of integers prices, for which the i-th element is the price of a giv ...
- P4238 【模板】多项式求逆 ntt
题意:求多项式的逆 题解:多项式最高次项叫度deg,假设我们对于多项式\(A(x)*B(x)\equiv 1\),已知A,求B 假设度为n-1,\(A(x)*B(x)\equiv 1(mod x^{\ ...
- 使用maven命令把jar包加入maven仓库
命令:mvn install:install-file -Dfile=D:\jar包路径xxxx.jar -DgroupId=根目录文件夹名字 -DartifactId=子目录文件夹 -Dversi ...
- SecureCRT自动记录日志
From: http://lzj0470.iteye.com/blog/1189368 今天在推特上看到有人谈起SecureCRT日志记录的问题,貌似很多人都有这习惯 我是开始工作后才使用Secure ...
- 1004. Max Consecutive Ones III最大连续1的个数 III
网址:https://leetcode.com/problems/max-consecutive-ones-iii/ 参考:https://leetcode.com/problems/max-cons ...
- c++ 软件版本比较函数
// 版本号拆分为数组 void splitToInt(string str , vector<int> *v1, char delim ){ // 拆分 string strTmp; s ...
- js重写系统的弹框
//调用系统的弹框,不显示地址 window.alert = function(name){ var iframe = document.createElement("IFRAME& ...
- 隔行变色&&鼠标移入变色
<html lang="en"> <head> <meta charset="UTF-8"> <title>Do ...