这里不讲解怎么在Eclipse安装配置TestNG,网上一搜一大把,大家自己去实践一下。

在这里主要说一下用Java来实现Selenium Webdriver的截图功能和把截图写到TestNG的报告中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Capture screenshot
public String captureScreenShot()
{
    String dir = "screenshot";
    String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
    String time = new SimpleDateFormat("HHmmss").format(new Date());
    String screenShotPath = dir + File.separator + date + File.separator + time + ".png";
    try
    {
        File source = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(source, new File(screenShotPath));
        screenShotPath = screenShotPath.substring(screenShotPath.indexOf("\\"));
    }
    catch(IOException e)
    {
        screenShotPath = "Failed to capture screenshot: " + e.getMessage();
    }
    return screenShotPath;
}
 
//Write to TestNG
public void writeToTestNG(String proMessage) {
    String png = captureScreenShot();
    Reporter.log("[" + logTime + "] " + proMessage);
 
    String log = new File("screenshot").getAbsolutePath();
 
    Reporter.log("<br/><img src=\"" + log  + "/" + png + "\" />"); 
}

用下面的方法来调用:

1
2
3
4
5
6
7
8
9
10
11
@Test
public void search()
{
    openURL();
    BaiduSearch yy = new BaiduSearch(driver);
    yy.searchFor("searchTest");
    
    writeToTestNG("testing ");
      
    driver.quit();
}

运行结果如下图所示:

[唐胡璐]Selenium技巧- 抓图并保存到TestNG报告中的更多相关文章

  1. [唐胡璐]Selenium技巧 - 利用MonteScreenRecorder录制视频

    我们可以用以下方式在Selenium Webdriver中capture video. 基本步骤: 从 http://www.randelshofer.ch/monte/,下载“MonteScreen ...

  2. [唐胡璐]Selenium技巧 - 处理Windows程序(进程)

    Selenium WebDriver java 提供了一个专门的WindowsUtils类去和Windows操作系统交互。 就像我们之前说过有时候跑完脚本后,IEDriverServer.exe进程没 ...

  3. [唐胡璐]Selenium技巧 - 定制元素属性检查,并写到ReportNG中

    QTP 和Selenium 都会有这种要检查某一个控件元素属性的情况,比如去检查一个Button的显示文字是什么? 为了更方便的书写程序,并优美的显示到HTML测试报告中,做了以下几个小小的封装,只是 ...

  4. [唐胡璐]Selenium技巧- Highlight页面元素

    大家都知道QTP的对象高亮显示功能特别强大, Selenium Webderiver也可以实现此功能。 高亮显示有时候对Debug还是相当有用的。 解决脚本: 调用脚本: 结果显示:

  5. [唐胡璐]Selenium技巧- IE浏览器Zoom和Protected Model Setting

    Selenium Webdriver在IE下跑脚本的时候要保证页面大小为100%,且要在IE internet options, selectSecurity tab and uncheck “Ena ...

  6. [唐胡璐]Selenium技巧- 如何处理Table

    由于webdriver中没有专门的table类,所以我们需要简单的封装出一个易用易扩展的Table类来帮助简化代码。 以下是我之前用C#语言来实现的一个简单的封装: 只是一个大概的思路,有些具体实现就 ...

  7. [唐胡璐]Selenium技巧- dataProvider实现数据驱动

    废话不多讲,直接进主题,怎么实现用Excel配置测试数据,用dataProvider来调用测试数据。 jxl目前来看只支持.xls格式的文件,所以我们采用Apache POI来实现对.xlsx的操作, ...

  8. [唐胡璐]Selenium技巧- Prop.Properties配置测试应用的环境和其他配置项

     prop.propertiesfile contains important info that needs to be changed before the test is run, such a ...

  9. [唐胡璐]Selenium技巧- ReportNG替换TestNG默认结果报告

    TestNG默认的报告虽然内容挺全,但是展现效果却不太理想,不易阅读。因此我们想利用ReportNG来替代TestNG默认的report。 什么是ReportNG呢?这里不多说,请直接参见:http: ...

随机推荐

  1. Django之session详解

    好文章来自追风的小蚂蚁:https://www.cnblogs.com/zhuifeng-mayi/p/9099811.html cookie:Cookie,有时也用其复数形式 Cookies,指某些 ...

  2. Netty学习篇②

    Channel.ChannelPipeline.ChannelHandlerContent发送数据的不同 // channel往回写数据 Channel channel = ctx.channel() ...

  3. Nginx04---实现直播

    比如树莓派开启一个直播服务如何开启: 百度搜索:搭建自己的直播服务器(nginx + RTMP)

  4. CentOS7下使用Harbor搭建Docker私有仓库

    相关资料: Harbor官方网站:https://goharbor.io/ Harbor Github地址:https://github.com/goharbor/harbor ⒈安装Docker(必 ...

  5. HDU 4614 线段树+二分查找

    Vases and Flowers 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4614 Problem Description Alice is s ...

  6. Vufuria入门 1 图片识别和选择

    Vufutia中的图片识别功能,底层主要是识别特征点来实现的.特征点,即那些棱角分明的点.尖锐的而不是圆滑的.对比度大的而不是小的. *** 步骤: 进入vofuria官网,登录,点击develop. ...

  7. LC 494. Target Sum

    问题描述 You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 ...

  8. Go语言注意事项

    必须恰当导入需要的包,缺少了必要的包或者导入了不需要的包,程序都无法编译通过.这项严格要求避免了程序开发过程中引入未使用的包(译注:Go语言编译过程没有警告信息,争议特性之一 import 声明必须跟 ...

  9. log4j application.properties 配置文件

    log4j.rootLogger = info,stdout log4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appende ...

  10. Springboot整合MybatisPlus

    目录 1.pom文件 2.创建CodeGenerator.java 3.在application.yml中配置mybatis-plus 4.创建MybatisPlusConfig.java文件 其他 ...