APP自动化框架LazyAndroid使用手册(3)--核心API介绍

2022-12-01,,,,

作者:黄书力

概述

在前一篇博文中,简要介绍了一款安卓UI自动化测试框架LazyAndroid

(http://blog.csdn.net/kaka1121/article/details/53204150)。本文将在此基础上,对框架的核心的API进行说明。

核心API介绍

•LazyDriver 的3个构造函数。

可以分别针对不同的应用场景选择使用不同的构造函数。使用场景及各参数的意义,代码注释中写得比较清楚了。

    /**
     * app测试默认driver
     *
     * @param AppName
     *            (for exapmle: "SimpleApp.apk")
     * @param packageName
     *            (for exapmle: "simple.app")
     * @param activityName
     *            (activityName must begin with ".", for exapmle:
     *            ".SimpleAppActivity")
     * @param platformVersion
     *            (for exapmle: "4.2.2")
     * @return an AppiumDriver object
     * @throws MalformedURLException
     */
    public LazyDriver(String AppName, String packageName, String activityName,
            String platformVersion) throws MalformedURLException {

        DesiredCapabilities capabilities = new DesiredCapabilities();
        setBasicCapabilities(AppName, packageName, activityName,
                platformVersion, capabilities);

        driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),
                capabilities);
    }

    /**
     * 可以选择测试开始前是否卸载app重新安装,是否清理app中的缓存数据
     *
     * @param AppName
     *            (for exapmle: "SimpleApp.apk")
     * @param packageName
     *            (for exapmle: "simple.app")
     * @param activityName
     *            (activityName must begin with ".", for exapmle:
     *            ".SimpleAppActivity")
     * @param platformVersion
     *            (for exapmle: "4.2.2")
     * @param bReset
     * @return an AppiumDriver object
     * @throws MalformedURLException
     */
    public LazyDriver(String AppName, String packageName, String activityName,
            String platformVersion, Boolean bReset)
            throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        setBasicCapabilities(AppName, packageName, activityName,
                platformVersion, capabilities);

        if (bReset)
            capabilities.setCapability("fullReset", "True"); // 测试开始前,卸载app重新安装,清除app数据文件
        else
            capabilities.setCapability("noReset", "True");

        driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),
                capabilities);
    }

    /**
     * 浏览器测试使用的driver
     *
     * @param browseType
     * @param platformVersion
     * @throws MalformedURLException
     */
    public LazyDriver(String browseType, String platformVersion)
            throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        setBasicCapabilities(browseType, platformVersion, capabilities);

        driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),
                capabilities);
    }

•手机基本操作

LazyDriver中封装了安装/重置apk、截屏、获取/设置屏幕方向、按键、回退、滑动、触摸等手机基本操作。

    /* ###################### 手机基本操作 #####################*/

    /**
     * 安装 android apk
     *
     * @param appPath
     */
    public void installApp(String appPath);

    /**
     * 关闭 android apk
     */
    public void closeApp();

    /**
     * 重置 android apk
     */
    public void resetApp();

    /**
     * 截屏
     *
     * @param screenSavePath
     */
    public void doScreenshot(String screenSavePath);

    /**
     * 获取屏幕方向(水平,垂直)
     *
     * @return orientation:LANDSCAPE("landscape"),PORTRAIT("portrait")
     */
    public ScreenOrientation getOrientation();

    /**
     * 设置屏幕方向
     *
     * @param orientation
     *            :LANDSCAPE("landscape"),PORTRAIT("portrait") )
     */
    public void setOrientation(ScreenOrientation orientation);

    /**
     * 按键操作
     *
     * @param key:
     * AndroidKeyCode int BACK = 4; int BACKSPACE = 67;
     * int DEL =67;
     * int ENTER = 66; int HOME = 3; int MENU = 82; int
     * SETTINGS = 176; int SPACE = 62;
     * 

    public void sendKeyEvent(int key);

    /**
     * 回退到上一个页面
     */
    public void goBack();

    /**
     * 从(fromX, fromY)滑动到(toX, toY)
     *
     * @param during
     */
    public void swipeUp;

    /**
     * 向上滑
     *
     * @param during
     */
    public void swipeUp(int during);

    /**
     * 向上滑 default during = 500
     */
    public void swipeUp();

    /**
     * 向下滑
     *
     * @param during
     */
    public void swipeDown(int during);

    /**
     * 向下滑 default during = 500
     */
    public void swipeDown();

    /**
     * 向左滑
     *
     * @param during
     */
    public void swipeToLeft(int during);

    /**
     * 向左滑 default during = 500
     */
    public void swipeToLeft();

    /**
     * 向右滑
     *
     * @param during
     */
    public void swipeToRight(int during);

    /**
     * 向右滑 default during = 500
     */
    public void swipeToRight();

    /**
     * 触摸
     *
     * @param el
     */
    public void touch(WebElement el);

    /**
     * 触摸
     *
     * @param x
     * @param y
     */
    public void touch(int x, int y);

    /**
     * 长按
     *
     * @param el
     */
    public void longTouch(WebElement el);
    /**
     * 长按
     *
     * @param x
     * @param y
     */
    public void longTouch(int x, int y);

    /**
     * 点击element控件中心点按下,duration*5毫秒秒后松开,如此重复fingers次。
     *
     * @param fingers
     * @param element
     * @param duration
     */
    public void tap(int fingers, WebElement element, int duration);

    /**
     * 点击(x,y)点按下,duration*5毫秒后松开,如此重复fingers次。
     *
     * @param fingers
     * @param x
     * @param y
     * @param duration
     */
    public void tap(int fingers, int x, int y, int duration);

•安卓常见控件的封装

各个控件类位于lazy.android.controls中;控件的通用方法见基类AbstractControl。其余的没有封装的控件,均可以通过View或者直接使用WebElement来操作。

•带重试机制的元素查找

      使用如下API进行元素查找,如果查找失败,会在设置的时间范围和重试周期内重复查找,如果查找成功则直接返回,可以大大减少因为网络原因导致的元素查找失败,同时又能避免为了解决超时查找而针对所有元素查找都强制pause几秒钟导致的时间开销。

    /* #################### Find 系列操作 ###################### */

    /**
     * 带超时重试机制的元素查找
     *
     * @param aDriver
     * @param xpath
     * @return
     */
    public WebElement findElementByXpath(String xpath);

    /**
     * 带超时重试机制的元素查找
     *
     * @param aDriver
     * @param xpath
     * @param timeout
     * @return
     */
    public WebElement findElementByXpath(String xpath, Long timeout);

    /**
     * 带超时重试机制的元素查找
     *
     * @param aDriver
     * @param xpath
     * @param timeout
     * @param stepInterval
     * @return
     */
    public WebElement findElementByXpath(String xpath, Long timeout,Long stepInterval);

    /**
     * 根据文字查找控件。遍历GlobalSettings.AndroidCtrType定义的安卓常用的6种控件类型,分别生成xpath进行查找,耗时较长
     * !
     *
     * @param aDriver
     * @param text
     * @param timeout
     * @return
     */
    public WebElement findElementByText(String text);

    /**
     * 根据文字和控件类型查找控件。需要传入准确的安卓控件名称,耗时短,成功率高!
     *
     * @param aDriver
     * @param text
     * @param controlTypeName:
     * "TextView", "Button", "CheckBox", "RadioButton",
     * "ImageView", "ToggleButton", ...
     * @return
     */
    public WebElement findElementByText(String text, String controlTypeName);

    /**
     * 带超时重试机制的文字捕获
     *
     * @param expectExist
     * @param
     * @param timeout
     * @throws InterruptedException
     * @throws NumberFormatException
     */
    public void expectTextExistOrNot(boolean expectExist, String text,
            int timeout);

    /**
     * 带超时重试机制的文字捕获
     *
     * @param expectExist
     * @param text
     * @throws NumberFormatException
     * @throws InterruptedException
     */
    public void expectTextExistOrNot(boolean expectExist, String text);

    /**
     * 带超时重试机制的控件查找——通过xpath
     *
     * @param aDriver
     * @param xpath
     * @param timeout
     * @param stepInterval
     * @return
     */
    private WebElement findElement(String xpath, Long timeout, Long stepInterval);

    /**
     * 带超时重试机制的控件查找——通过xpath list
     *
     * @param aDriver
     * @param xpathArray2
     *            :xpath arrayList
     * @return
     */
    private WebElement findElement(List<String> xpathArray2);

    /**
     * 判断控件是否存在,不带超时重试机制
     * @return
     */
    public boolean isExists();
    /**
     * 判断控件是否存在——不带超时重试机制
     * @param xpath
     * @return
     */
    public boolean isElementPresent(String xpath);
    /**
     * 带超时重试机制的控件存在情况判断
     * @param expectExist
     * @param xpathArray
     * @param timeout
     */
    public void expectElementExistOrNot(boolean expectExist, int timeout);
    /**
     * 带超时重试机制的控件存在情况判断
     * @param expectExist
     * @param xpathArray
     * @param timeout
     */
    public void expectElementExistOrNot(boolean expectExist);
    /**
     * 将控件通过xpath蜕化为WebElement对象
     * @return
     */
    public WebElement toWebElement();

总结

LazyAndroid解决了安卓UI自动化测试实施过程中存在的测试工具学习成本高,控件定位耗时长、准确率低,安卓的具体控件操作方法生疏等诸多问题,简化了appiumDriver中Capabilities的繁琐设置、手机的滑动、按键等基本操作,增加了元素查找的重试机制、异常处理截屏等,能提高安卓自动化测试的实施效率。

    后续将会给出基于LazyAndroid的测试模板工程,并进行详细阐述。

APP自动化框架LazyAndroid使用手册(3)--核心API介绍的相关教程结束。

《APP自动化框架LazyAndroid使用手册(3)--核心API介绍.doc》

下载本文的Word格式文档,以方便收藏与打印。