温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

appium自动化,失败自动截图

发布时间:2020-06-29 21:56:26 来源:网络 阅读:437 作者:知止内明 栏目:编程语言

失败自动截图


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=\"***\"  onMouseover=\"this.width=353; this.height=613\" onMouseout=\"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


    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;
    }

appium自动化,失败自动截图

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI