189 8069 5689

appium自动化,失败自动截图

失败自动截图

十多年专业网站建设公司历程,坚持以创新为先导的网站服务,服务超过数千家企业及个人,涉及网站设计、重庆APP开发、微信开发、平面设计、互联网整合营销等多个领域。在不同行业和领域给人们的工作和生活带来美好变化。


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("截图位置
" + targetFile.getPath()); LogUtil.info("------file is ---- " + targetFile.getPath()); try { FileUtils.copyFile(srcFile, targetFile); } catch (IOException e) { e.printStackTrace(); } logTestEnd(result, "Failed"); //报告截图后面显示 Reporter.log("\"***\""); } /** * 在用例执行结束时,打印用例的执行结果信息 */ 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 testsToBeRemoved = new ArrayList(); // collect all id's from passed test Set passedTestIds = new HashSet(); for (ITestResult passedTest : testContext.getPassedTests().getAllResults()) { logger.info("PassedTests = " + passedTest.getName()); passedTestIds.add(getId(passedTest)); } Set failedTestIds = new HashSet(); 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 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


    public static AndroidDriver driver;

    /**
     * @param port      :服务器启动的端口号,系统自动获取
     * @param udid      :手机设备号:系统自动化获取
     * @param apk       :自动化运行的APK包,系统会根据该地址获取包名与actiber
     * @param serverUrl :客户端ip地址默认 127.0.0.1
     * @param flag      :true 卸掉有重新安装与运行后自动化卸掉包。false 直接安装运行
     * @return
     */
    public static AndroidDriver initDriver(String port, String udid, String apk, String serverUrl, boolean flag) {
        ArrayList 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;
    }

appium自动化,失败自动截图


新闻标题:appium自动化,失败自动截图
本文来源:http://cdxtjz.com/article/jsecjs.html

其他资讯