博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[唐胡璐]Selenium技巧- 抓图并保存到TestNG报告中
阅读量:4346 次
发布时间:2019-06-07

本文共 1350 字,大约阅读时间需要 4 分钟。

 这里不讲解怎么在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();
}

运行结果如下图所示:

d833c895d143ad4b658746f081025aafa40f0650.jpg

转载于:https://www.cnblogs.com/yongfeiuall/p/4134147.html

你可能感兴趣的文章
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
IOS内存管理
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>