您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源功能測(cè)試工具 > Selenium
Selenium遇到異常自動(dòng)截圖
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2015/9/23 14:35:03 ] 推薦標(biāo)簽:TestNG 功能測(cè)試工具

  2.使用testNG的TestListenerAdapter
  a.先建一個(gè)類(lèi)繼承TestListenerAdapter,并重寫(xiě)onTestFailure方法
package com.screenshot.exception;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.apache.velocity.runtime.log.LogManager;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import com.screenshot.singleton.TestBase;
/**
* @author QiaoJiaofei
* @version 創(chuàng)建時(shí)間:2015年8月24日 下午6:33:44
* 類(lèi)說(shuō)明
*/
public class UseTestNg extends TestListenerAdapter{
@Override
public synchronized void onTestFailure(ITestResult result) {
Object currentClass = result.getInstance();
WebDriver webDriver = ((TestUsNg) currentClass).getDriver();
if (webDriver != null)
{
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
String dateString = formatter.format(currentTime);
File scrFile = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
try {
File screenshot = new File("D:/ddd/"
+ dateString  + ".png");
FileUtils.copyFile(scrFile,screenshot);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
  b.創(chuàng)建測(cè)試類(lèi),注意需要在測(cè)試類(lèi)中寫(xiě)getDriver()方法
package com.screenshot.exception;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
/**
* @author QiaoJiaofei
* @version 創(chuàng)建時(shí)間:2015年8月24日 下午6:43:44
* 類(lèi)說(shuō)明
*/
public class TestUsNg {
private WebDriver dr;
public WebDriver getDriver() {
return dr;
}
@Test
public void f() {
String key = "webdriver.chrome.driver";
String value = "D:/BaiduYunDownload/selenium/chromedriver.exe";
System.setProperty(key, value);
dr = new ChromeDriver();
System.out.println(5/0);
}
}
  C.在testng的xml文件中添加監(jiān)聽(tīng)
  <listeners>
  <listener class-name="com.screenshot.exception.UseTestNg" />
  </listeners>
  3.如何將生成的圖片連接到reportNG中,將下面的代碼放入上面相應(yīng)重寫(xiě)的方法中,圖片路徑與上述代碼中生成的圖片結(jié)合一起。
  String imgName = "";//圖片路徑
  Reporter.log("<a href=./img/" + imgName + " target=_blank>Failed Screen Shot</a>", true);
  4.使用Robot主動(dòng)截圖,這種可以在自己想截圖的時(shí)候調(diào)用該方法即可截當(dāng)前界面
packagecom.screenshot.book;
importjava.awt.Rectangle;
importjava.awt.Robot;
importjava.awt.Toolkit;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjavax.imageio.ImageIO;
importorg.testng.annotations.Test;
/**
*@authorQiaoJiaofei
*@version創(chuàng)建時(shí)間:2015年8月26日下午7:40:34
*類(lèi)說(shuō)明
*/
publicclassTestRobot{
@Test
publicvoidtakeScreenShotMethod(){
try{
Thread.sleep(3000);
BufferedImageimage=newRobot().createScreenCapture(newRectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image,"jpg",newFile("D:/ddd/screenshot.jpg"));
}
catch(Exceptione){
e.printStackTrace();
}
}
}
  備注:
  使用junit自動(dòng)截圖,可以使用Rule,由于我用的是testNG,所以沒(méi)有調(diào)試junit的方法。詳細(xì)參考:http://stackoverflow.com/questions/20995722/when-does-
onexception-get-triggered-in-webdrivereventlistener
@Rule
public TestRule testWatcher = new TestWatcher() {
@Override
public void succeeded(Description test){
for (LogEntry log : driver.manage().logs().get(LogType.DRIVER).getAll()) {
System.out.println("Level:" + log.getLevel().getName());
System.out.println("Message:" + log.getMessage());
System.out.println("Time:" + log.getTimestamp());
System.out.println("-----------------------------------------------");
}
System.out.println();
@Override
public void failed(Throwable t, Description test) {
String testName = test.getClassName();
String subTestName = test.getMethodName();
String screenShotName = String.format("%s\%s", testName, screenShotName);
if (driver instanceof TakesScreenshot) {
File tempFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
System.out.println(">>>>>>>>>>LOGS: " + yourDirForImages + "\" + screenShotName + ".png");
FileUtils.copyFile(tempFile, new File(String.format("%s.png", screenShotName)));
} catch (IOException e) {
e.printStackTrace();
}
}

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd