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启动浏览器,失败用例截图的更多相关文章

  1. selenium webdriver启动IE浏览器失败的解决办法

    通过selenium webdriver启动IE浏览器失败,报错:selenium.common.exceptions.WebDriverException: Message: Unexpected ...

  2. 执行testng appium用例失败,自动截图

    新建一个截图监听类ScreenShotListener ,重写onTestFailure方法,里面定义了 监听的driver ,截图文件路径和名称 package com.fsssc.htsgl.ut ...

  3. Unittest 支持 case 失败后自动截图功能的另外两种方式

    原生的unittest框架是不支持case失败后自动截图的功能的,网上看了大家的解决办法,大体上分为两种:1.要么加装饰器2.也有人封装断言这里我们看看还有没有其他的更加方便的方法值得大家一起探讨一下 ...

  4. 章节十七章、2- 给执行失败的case截图

    一.案例演示 1.首先我们把截图的方法单独进行封装方便以后调用. package utilities; import java.io.File; import java.io.IOException; ...

  5. selenium是如何启动浏览器的

    前几天有同学问到selenium是怎么样启动浏览器的(selenium启动浏览器的原理),当时稍微讲解了一下,不过自我感觉不够具体,现在特地把启动原理通过代码和一系列操作给串联起来,希望可以帮助大家更 ...

  6. Selenium2启动浏览器且加载插件

    一.SELENIUM2启动浏览器 注意: SELENIUM2在启动浏览器时,都是启动一个干净的没有任务 插件及cookies信息的浏览器,即使是你之前的浏览器有设置过代理,到自动化启动时,也是没有代理 ...

  7. 启动OracleDBConsoleorcl失败,提示错误代码2

    异常问题: 启动OracleDBConsoleorcl失败,提示错误代码2 原因分析: 由于更改计算机名导致的异常 解决方法: 1.管理员权限cmd下执行emctl start dbconsole 2 ...

  8. MyEclipse 启动 tomcate 失败 解决方法

    MyEclipse中启动tomcate失败 十月 20, 2015 9:53:04 下午 org.apache.catalina.startup.VersionLoggerListener log信息 ...

  9. 基于Selenium2+Java的UI自动化(2) - 启动浏览器

    一.准备工作 我们常用的浏览器主要有三个:chrome.Firefox.IE:其中chrome 和 IE 需要下载驱动程序,才能启动浏览器,注意驱动程序有32位和64位两种. 另外:如何查看本机的浏览 ...

随机推荐

  1. vue select中的option循环的时候,要使用 :value,不能使用 v-model

    <select class="classColor" @change="select" v-model="selectValue"&g ...

  2. laravel调度任务

    <?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule;use Illuminate\Foundation ...

  3. 安装redisPHP扩展

    1. "predis/predis":"~1.1@dev" 2.composer update 即可,这是给项目添加redis扩展 启动服务端 redis-se ...

  4. 【oauth2.0】【1】简单介绍

    含义: OAuth是一个关于授权(authorization)的开放网络标准,2.0是当前版本.不是技术,而是一项资源授权协议. OAuth在"客户端"与"服务提供商&q ...

  5. 将一台电脑上的虚拟机上的系统复制到另一台电脑的虚拟机上!!!and想询问大神们问题的解决办法??

    虚拟机是Oracle VM VitualBox版本 前段日子oracle老师让课下安装虚拟机+windows8+oracle数据库,但悲伤的我安了七八遍(重装系统+安装数据库+配置数据库)依旧在配置数 ...

  6. Units about ASM

    1.ASM Striping and Mirroring:ASM supports two levels of striping: fine striping and coarse striping. ...

  7. Python购物车

    product_list = [ ['Iphone',5888], ['Mac Air',8000], ['XiaoMi',19.9], ['coffee',30], ['Tesla',820000] ...

  8. 【转】JavaScript数组方法大全

    数组在笔试题中出现的概率最高的类型之一,JavaScript中的数组与其他语言中的数组有些区别,为了方便以后查看数组的方法,现将对数组的操作方法进行汇总整理. 数组创建 JavaScript中创建数组 ...

  9. 牛客网 PAT 算法历年真题 1010 : 月饼 (25)

    月饼 (25) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 月饼是中国人在中秋佳节时吃的一种传统食品,不同地 ...

  10. TCP/IP报文 三次握手 四次挥手

    1.TCP报文格式  TCP/IP协议的详细信息参看<TCP/IP协议详解>三卷本.下面是TCP报文格式图:图1 TCP报文格式  上图中有几个字段需要重点介绍下:  (1)序号:Seq序 ...