Appium自動化中截圖的問題
阿新 • • 發佈:2017-10-18
操作 過程 rtt 工具 storage cap 測試報告 ren date
在用Appium做UI自動化過程中,大家會發現測試報告很重要,而在測試報告中截圖很重要。
因為很多公司都是用Jenkins作為持續集成工具,所以要讓執行自動化測試的人看明白自動化在跑什麽,哪裏失敗了,關鍵節點都需要截圖。
怎麽做呢,目前項目中是這麽實現的:
1.實現截圖功能類:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static String screenShot(ShipperAndroidEmulator ae) {
String dir = "screenshot" ; // TODO
String time = new SimpleDateFormat( "yyyyMMdd-HHmmss" ).format( new Date());
String screenShotPath = dir + File.separator + time + ".png" ;
AndroidDriver augmentedDriver = null ;
augmentedDriver = ae.getAndroid();
try {
File sourceFile = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(sourceFile, new File(screenShotPath));
} catch (Exception e) {
e.printStackTrace();
return "Failed to screenshot" ;
}
return screenShotPath.replace( "\\" , "/" );
}
|
2.用screenShot實現錯誤處理類:
1 2 3 4 5 6 7 8 9 10 |
private void handleFailure(String notice) { String png = LogTools.screenShot( this );
String log = notice + " >> capture screenshot at " + png;
logger.error(log);
if (GlobalSettings.baseStorageUrl.lastIndexOf( "/" ) == GlobalSettings.baseStorageUrl.length()) {
GlobalSettings.baseStorageUrl = GlobalSettings.baseStorageUrl.substring( 0 , GlobalSettings.baseStorageUrl.length() - 1 );
}
Reporter.log(log + "<br/><img src=\"" + GlobalSettings.baseStorageUrl + "/" + png + "\" />" );
Assert.fail(log);
}
|
3.對所有appium界面操作類進行處理:
1 2 3 4 5 6 7 8 9 10 11 12 |
public void click(By by) {
expectElementExistOrNot( true , by, timeout);
try {
clickTheClickable(by,System.currentTimeMillis(), 2500 );
handleSuccess( "Succeed to click " + by);
} catch (Exception e){
e.printStackTrace();
handleFailure( "Failed to click " + by);
}
logger.info( "Clicked " + by);
}
|
4. 不能點擊時候重試點擊操作
private void clickTheClickable(By byElement, long startTime, int timeOut) throws Exception {
try {
findElementBy(byElement).click();
} catch (Exception e) {
if (System.currentTimeMillis() - startTime > timeOut) {
logger.warn(byElement+ " is unclickable");
throw new Exception(e);
} else {
Thread.sleep(500);
logger.warn(byElement + " is unclickable, try again");
clickTheClickable(byElement, startTime, timeOut);
}
}
Appium自動化中截圖的問題