这里不讲解怎么在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:5. Java分布式架构

    什么是分布式架构 分布式系统(distributed system)是建立在网络之上的软件系统. 内聚性是指每一个数据库分布节点高度自治,有本地的数据库管理系统. 透明性是指每一个数据库分布节点对用户 ...

  2. 【记录】编译安装 YAML 扩展

    转自:https://learnku.com/articles/30985 Yaml,专门用来写配置文件的语言   依赖安装 $ sudo apt-get install libyaml-dev   ...

  3. C++11 thread condition_variable mutex 综合使用

    #include <mutex> #include <condition_variable> #include <chrono> #include <thre ...

  4. centos 安装go

    [root@golong-learning src]# wget https://dl.google.com/go/go1.12.7.linux-amd64.tar.gz # 下载 [root@gol ...

  5. 【51nod】2590 持续讨伐

    [51nod]2590 持续讨伐 挣扎着卡了卡常过了 记\(dp[i][j]\)为到第\(i\)位,和第\(i\)位相连的部分长度\(x^{j}\)乘上之前部分所有方案\(x^{K}\)总和 转移用二 ...

  6. c# 金钱大写转小写工具类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. [DEBUG] java中用Runtime调用python 简单程序输出null

    今天需要在java中调用python脚本,首先考虑的是java自带的Runtime 在ubuntu和win10下分别测试,发现win10报错 java源代码 @Test public void tes ...

  8. Go-函数高级使用-条件分支-包管理-for循环-switch语句-数组及切片-与或非逻辑符

    目录 科普 python 注释 # 函数高级 if else if else 包管理 下载第三方包 比较热门的框架 for 循环 for 循环的几种写法 switch 语句 数组及数组切片 数组迭代 ...

  9. Codeforces 1097F. Alex and a TV Show

    传送门 由于只要考虑 $\mod 2$ 意义下的答案,所以我们只要维护一堆的 $01$ 容易想到用 $bitset$ 瞎搞...,发现当复杂度 $qv/32$ 是可以过的... 一开始容易想到对每个集 ...

  10. IDEA中安装go插件,如何能够配置go SDK?

    最近在学习go语言,一个是因为区块链的技术热潮,另一个是接手的项目有用到go写多线程高并发,因此决定自学go. 第一个遇到的问题就是环境! 通过一个晚上的摸索,大概步骤如下: 在IDEA中先打开set ...