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. java读取ACCESS数据库的简单示例

    java读取ACCESS数据库的简单示例 虽然简单,对初学者来说,如果没有一段可以成功执行的代码供参考,还真难调试 先用ACCESS建一个数据库 DB1.MDB,里面有一表"table1&q ...

  2. php中Redis的扩展

    首先要下载 php_redis.dll 和 php_igbinary.dll 在官网(https://windows.php.net/downloads/pecl/snaps/redis/3.1.4/ ...

  3. php 递归删除文件夹

    /*** 递归删除文件方法一 param $path 文件路径 **/ function delAll($path){ $path=str_replace('\\','/',$path);//去除反斜 ...

  4. Subordinates CodeForces - 737C (树,构造)

    大意: 求构造一棵树, 每个节点回答它的祖先个数, 求最少打错次数. 挺简单的一个构造, 祖先个数等价于节点深度, 所以只需要确定一个最大深度然后贪心即可. 需要特判一下根的深度, 再特判一下只有一个 ...

  5. 5月21 回话控制SESSION COOKIE

    百科 HTTP普及(无状态性) 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP ...

  6. ORACLE SQL 函数 INITCAP()

    INITCAP() 假设c1为一字符串.函数INITCAP()是将每个单词的第一个字母大写,其它字母变为小写返回. 单词由空格,控制字符,标点符号等非字母符号限制. select initcap('h ...

  7. Pick-up sticks

    Pick-up sticks Stan has n sticks of various length. He throws them one at a time on the floor in a r ...

  8. PHP指针相关函数

    1.each each — 返回数组中当前的键/值对并将数组指针向前移动一步 $arr = array("one", "two", "three&qu ...

  9. 转: Linux mount/unmount命令

    https://blog.csdn.net/okhymok/article/details/76616892 楼主具体哪里转的 我不清楚 好像没看到原始出处 开机自动挂载 如果我们想实现开机自动挂载某 ...

  10. sql server 中进行除法运算时,如何得到结果是小数形式呢?

    我们正常进行除法运算时,sql默认是返回一个四舍五入的数 比如12除以5,17除以3 --算法1:返回结果:2 需要的是2.40 ) as 结果1 --算法2:返回结果:5 需要的是5.67 ) as ...