Selenium技巧- 抓图并保存到TestNG报告中(转)

上一篇 / 下一篇  2014-07-30 16:43:46 / 个人分类:Selenium2

Selenium技巧- 抓图并保存到TestNG报告中

地址:http://hi.baidu.com/yongfeiuall/item/4c0212efc79088f02a09a488

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

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

//Capture screenshot
publicString captureScreenShot()
{
    String dir ="screenshot";
    String date =newSimpleDateFormat("yyyyMMdd").format(newDate());
    String time =newSimpleDateFormat("HHmmss").format(newDate());
    String screenShotPath = dir + File.separator + date + File.separator + time +".png";
    try
    {
        File source = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(source,newFile(screenShotPath));
        screenShotPath = screenShotPath.substring(screenShotPath.indexOf("\\"));
    }
    catch(IOException e)
    {
        screenShotPath ="Failed to capture screenshot: "+ e.getMessage();
    }
    returnscreenShotPath;
}
 
//Write to TestNG
publicvoidwriteToTestNG(String proMessage) {
    String png = captureScreenShot();
    Reporter.log("["+ logTime +"] "+ proMessage);
 
    String log =newFile("screenshot").getAbsolutePath();
 
    Reporter.log("<br/><img src=\""+ log  +"/"+ png +"\" />"); 
}

用下面的方法来调用:

@Test
publicvoidsearch()
{
    openURL();
    BaiduSearch yy =newBaiduSearch(driver);
    yy.searchFor("searchTest");
    
    writeToTestNG("testing ");
      
    driver.quit();
}

运行结果如下图所示:


TAG:

 

评分:0

我来说两句

Open Toolbar