When you do UI Automation with Appium, you'll find that test reports are important and important in test reports.
Because many companies use Jenkins as a continuous integration tool, the people who perform automated tests see what automation is running, where it fails, and what critical nodes are required for.
How to do it, the current project is so implemented:
1. Implement the function class:
| 12345678910111213141516171819 |
publicstaticString screenShot(ShipperAndroidEmulator ae) { String dir = "screenshot"; // TODO String time = newSimpleDateFormat("yyyyMMdd-HHmmss").format(newDate()); 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"; } returnscreenShotPath.replace("\\", "/");} |
2. Implement the error-handling class with screenshot:
| 12345678910 |
privatevoidhandleFailure(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/>+ GlobalSettings.baseStorageUrl + "/"+ png + "\" />"); Assert.fail(log);} |
3. All Appium interface operation classes are processed:
| 123456789101112 |
publicvoidclick(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. Do not click Time to re-pilot hit operation
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);
}
}
Problems in Appium automation