失败自动截图
@H_404_2@
public class MyTestngListener extends TestListenerAdapter {
private static Logger logger = Logger.getLogger(MyTestngListener.class);
public static final String CONfig = "config.properties";
@Override
public void onTestFailure(ITestResult result) {
super.onTestFailure(result);
logger.info(result.getName() + " Failure");
WaitUtil.sleep(2000);
AppiumDriver driver = DriverBase.getDriver();
File srcFile = driver.getScreenshotAs(OutputType.FILE);
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hhmmss");
File location = new File(AndroidCapabilityType.LOCAL_SCREEN_FILE_URL);
if (!location.exists()) {
location.mkdirs();
}
String dest = result.getmethod().getRealClass().getSimpleName() + "." + result.getmethod().getmethodName();
String s = dest + "_" + dateFormat.format(new Date()) + ".png";
File targetFile =
new File(location + "/" + s);
LogUtil.info("截图位置:");
Reporter.log("<font color=\"#FF0000\">截图位置</font><br /> " + targetFile.getPath());
LogUtil.info("------file is ---- " + targetFile.getPath());
try {
FileUtils.copyFile(srcFile, targetFile);
} catch (IOException e) {
e.printstacktrace();
}
logTestEnd(result, "Failed");
//报告截图后面显示
Reporter.log("<img src=\"./screenshots/" + s + "\" width=\"64\" height=\"64\" alt=\"***\" onm ouSEOver=\"this.width=353; this.height=613\" onm ouSEOut=\"this.width=64;this.height=64\" />");
}
/**
* 在用例执行结束时,打印用例的执行结果信息
*/
protected void logTestEnd(ITestResult tr, String result) {
Reporter.log(String.format("=============Result: %s=============", result), true);
}
@Override
public void onTestSkipped(ITestResult tr) {
super.onTestSkipped(tr);
logger.info(tr.getName() + " Skipped");
//takeScreenShot(tr);
}
@Override
public void onTestSuccess(ITestResult tr) {
super.onTestSuccess(tr);
logger.info(tr.getName() + " Success");
}
@Override
public void onTestStart(ITestResult tr) {
super.onTestStart(tr);
logger.info(tr.getName() + " Start");
}
@Override
public void onFinish(ITestContext testContext) {
super.onFinish(testContext);
ArrayList<ITestResult> testsToBeRemoved = new ArrayList<ITestResult>();
// collect all id's from passed test
Set<Integer> passedTestIds = new HashSet<Integer>();
for (ITestResult passedTest : testContext.getpassedTests().getAllResults()) {
logger.info("PassedTests = " + passedTest.getName());
passedTestIds.add(getId(passedTest));
}
Set<Integer> FailedTestIds = new HashSet<Integer>();
for (ITestResult FailedTest : testContext.getFailedTests().getAllResults()) {
logger.info("FailedTest = " + FailedTest.getName());
// id = class + method + dataprovider
int FailedTestId = getId(FailedTest);
// if we saw this test as a Failed test before we mark as to be deleted
// or delete this Failed test if there is at least one passed version
if (FailedTestIds.contains(FailedTestId) || passedTestIds.contains(FailedTestId)) {
testsToBeRemoved.add(FailedTest);
} else {
FailedTestIds.add(FailedTestId);
}
}
// finally delete all tests that are marked
for (Iterator<ITestResult> iterator = testContext.getFailedTests().getAllResults().iterator(); iterator.hasNext(); ) {
ITestResult testResult = iterator.next();
if (testsToBeRemoved.contains(testResult)) {
logger.info("Remove repeat Fail Test: " + testResult.getName());
iterator.remove();
}
}
}
private int getId(ITestResult result) {
int id = result.getTestClass().getName().hashCode();
id = id + result.getmethod().getmethodName().hashCode();
id = id + (result.getParameters() != null ? Arrays.hashCode(result.getParameters()) : 0);
return id;
}
}
通过启动后获取driver
@H_404_2@
public static AndroidDriver<AndroidElement> driver;
/**
* @param port :服务器启动的端口号,系统自动获取
* @param udid :手机设备号:系统自动化获取
* @param apk :自动化运行的APK包,系统会根据该地址获取包名与actiber
* @param serverUrl :客户端ip地址默认 127.0.0.1
* @param flag :true 卸掉有重新安装与运行后自动化卸掉包。false 直接安装运行
* @return
*/
public static AndroidDriver<AndroidElement> initDriver(String port, String udid, String apk, String serverUrl, boolean flag) {
ArrayList<String> packAct = OperationalCmd.getPackAct(apk);
// File app = new File(".\\apk\\20171026.apk");
DesiredCapabilities caps = new DesiredCapabilities();
//自动安装
if (flag) {
caps.setCapability(MobileCapabilityType.APP, apk);
//结束后会卸载程序
caps.setCapability(MobileCapabilityType.FULL_RESET, AndroidCapabilityType.FULL_RESET);
}
caps.setCapability(AndroidMobileCapabilityType.APPLICATION_NAME, udid);
//PLATFORM_NAME: 平台名称
caps.setCapability(AndroidMobileCapabilityType.PLATFORM_NAME, AndroidCapabilityType.PLATFORM_NAME);
// UDID:设置操作手机的唯一标识,android手机可以通过adb devices查看
caps.setCapability(MobileCapabilityType.DEVICE_NAME, udid);
//NEW_COMMAND_TIMEOUT: appium server和脚本之间的 session超时时间
caps.setCapability(AndroidCapabilityType.NEW_COMMAND_TIMEOUT, AndroidCapabilityType.NEW_COMMAND_TIMEOUT);
//APP_PACKAG:Android应用的包名
caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, packAct.get(0));
//APP_ACTIVITY :启动app的起始activity
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, packAct.get(1));
//UNICODE_KEYBOARD:1、中文输入不支持,2、不用它键盘会弹出来,说不定会影响下一步操作.需要注意设置后,需要将手机的输入法进行修改
caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, AndroidCapabilityType.UNICODE_KEY_BOARD);
//Reset_KEYBOARD:是否重置输入法
caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, AndroidCapabilityType.RESET_KEY_BOARD);
//NO_SIGN:跳过检查和对应用进行 debug 签名的
caps.setCapability(AndroidMobileCapabilityType.NO_SIGN, AndroidCapabilityType.NO_SIGN);
try {
//appium测试服务的地址
driver = new AndroidDriver<>(new URL(serverUrl + ":" + port + "/wd/hub"), caps);
} catch (MalformedURLException e) {
e.printstacktrace();
}
return driver;
}
/**
* 截图使用
*
* @return
*/
public static AppiumDriver getDriver() {
return driver;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。