溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

appium自動化,失敗自動截圖

發(fā)布時間:2020-06-29 21:56:26 來源:網(wǎng)絡(luò) 閱讀:441 作者:知止內(nèi)明 欄目:編程語言

失敗自動截圖


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");
        //報(bào)告截圖后面顯示
        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\" />");

    }

    /**
     * 在用例執(zhí)行結(jié)束時,打印用例的執(zhí)行結(jié)果信息
     */
    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      :服務(wù)器啟動的端口號,系統(tǒng)自動獲取
     * @param udid      :手機(jī)設(shè)備號:系統(tǒng)自動化獲取
     * @param apk       :自動化運(yùn)行的APK包,系統(tǒng)會根據(jù)該地址獲取包名與actiber
     * @param serverUrl :客戶端ip地址默認(rèn) 127.0.0.1
     * @param flag      :true 卸掉有重新安裝與運(yùn)行后自動化卸掉包。false 直接安裝運(yùn)行
     * @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);
            //結(jié)束后會卸載程序
            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:設(shè)置操作手機(jī)的唯一標(biāo)識,android手機(jī)可以通過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應(yīng)用的包名
        caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, packAct.get(0));
        //APP_ACTIVITY :啟動app的起始activity
        caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, packAct.get(1));
        //UNICODE_KEYBOARD:1、中文輸入不支持,2、不用它鍵盤會彈出來,說不定會影響下一步操作.需要注意設(shè)置后,需要將手機(jī)的輸入法進(jìn)行修改
        caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, AndroidCapabilityType.UNICODE_KEY_BOARD);
        //Reset_KEYBOARD:是否重置輸入法
        caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, AndroidCapabilityType.RESET_KEY_BOARD);
        //NO_SIGN:跳過檢查和對應(yīng)用進(jìn)行 debug 簽名的
        caps.setCapability(AndroidMobileCapabilityType.NO_SIGN, AndroidCapabilityType.NO_SIGN);
        try {
            //appium測試服務(wù)的地址
            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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI