标签:方式 col width tomat des shu driver 分享图片 desire
1、创建监听器类TestNGListener,重写onTestFailure方法,里面定义了 监听的driver ,截图文件路径和名称
public class TestNGListener extends TestListenerAdapter{ @Override public void onTestFailure(ITestResult tr){ try { super.onTestFailure(tr); //调用屏幕截图方法 captureScreenShot(tr,tr.getMethod().getMethodName()); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } } public void captureScreenShot(ITestResult result,String name) { AppiumDriver driver = AppiumDemo.getDriver(); File srcFile = driver.getScreenshotAs(OutputType.FILE); String filepath = "E:\\appimage\\education_test"; DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss"); try { FileUtils.copyFile(srcFile, new File(filepath + File.separator +name+"-"+ dateFormat.format(new Date()) + ".png")); } catch (IOException e1) { e1.printStackTrace(); } } }
2、driver的来源
public class AppiumDemo {
public static AppiumDriver<WebElement> driver;
@BeforeClass
public void setup() throws Exception{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(CapabilityType.BROWSER_NAME,"");
caps.setCapability("platformName","Android"); //测试平台
caps.setCapability("deviceName","127.0.0.1:62025"); //指定测试机的ID
caps.setCapability("appPackage","ai.avatec.android"); //被测APP包名
caps.setCapability("appActivity","ai.avatec.android.ui.main.view.MainActivity"); //被测APP入口activity
caps.setCapability("appWaitActivity","ai.avatec.android.ui.main.view.MainActivity");
caps.setCapability("noSign",true); //noSign表示不重签名,设置为true表示不重签名app
caps.setCapability(AndroidMobileCapabilityType.NO_SIGN,true);
//UNICODE_KEYBOARD表示unicode输入法,支持中文输入,支持键盘隐藏
caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD,true);
//重置输入法为设备原来的
caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD,true);
caps.setCapability("automationName", "UiAutomator2");
//每次启动时覆盖session,否则第二次后运行会报错不能新建session
caps.setCapability("sessionOverride",true);
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps){
@Override
public WebElement scrollTo(String text) {
return null;
}
@Override
public WebElement scrollToExact(String text) {
return null;
}
public void rotate(DeviceRotation deviceRotation) {
}
public DeviceRotation rotation() {
return null;
}
};
}
public static AppiumDriver getDriver()
{
return driver;
}
}
3、配置监听器
有两种配置方法
(1)有两种配置方法在需要捕捉的类上方
(2)在testng.xml里配置监听器
标签:方式 col width tomat des shu driver 分享图片 desire
原文地址:https://www.cnblogs.com/fulucky/p/9240096.html