这里不讲解怎么在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. 最新 用友网络java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.用友网络等10家互联网公司的校招Offer,因为某些自身原因最终选择了用友网络.6.7月主要是做系统复习.项目复盘.Leet ...

  2. Red And Green

    #include <stdio.h> #include <string.h> #define LENGTH 50 /* * 1.字符序列中有一个字符肯定是分界点,它的左边全为红 ...

  3. 破周三,前不着村后不着店的,只好学pandas了,你该这么学,No.9

    如果图片无法观看,请移步 https://blog.csdn.net/hihell 周三了,一个星期最难的一天 大中间的,今天还这么热 5月份,36度的高温 天空飘过几个字 屋里学pandas最得劲 ...

  4. Mybatis之自动生成

    使用Mybatis来自动生成我们的dao接口,mapper文件和实体类. 1.pom.xml依赖: <dependencies> <dependency> <groupI ...

  5. shell 监控

    #!/bin/shsource /etc/profileserverName=$1dingDingName=$2 #获取内存情况memory=(`free | awk 'NR==2{print $2, ...

  6. Docker 安装 Tomcat

    查找Docker Hub上的tomcat镜像 docker search tomcat 取官方的镜像 docker pull tomcat 使用tomcat镜像 创建目录tomcat,用于存放后面的相 ...

  7. [转帖]SPARC简介

    https://www.cnblogs.com/chaohm/p/5674886.html 1.    概述 SPARC(Scalable Processor ARChitecture,可扩展处理器架 ...

  8. Nginx04---实现直播

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

  9. .Net C# Dictionary 和参数字符串互转

    #region Parse #region Dictionary Parse To String /// <summary> /// Dictionary Parse To String ...

  10. 用Activator.CreateInstance代替new实现类的实例化

    一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例. 这在工厂模式中是非常有用的 这样,可以使程序有更高的扩展性,例如,,下面的例子 如果现在有一个类,专门用来计算交通工具的速度, ...