作者:黄书力

概述

在前一篇博文中,简要介绍了一款安卓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介绍的更多相关文章

  1. APP自动化框架LazyAndroid使用手册(2)--元素自动抓取

    作者:黄书力 概述 前面的一篇博文简要介绍了安卓自动化测试框架LazyAndroid的组成结构和基本功能,本文将详细描述此框架中元素自动抓取工具lazy-uiautomaterviewer的使用方法. ...

  2. APP自动化框架LazyAndroid使用手册(4)--测试模板工程详解

    概述 前面的3篇博文分别对lazyAndroid的框架简介.元素抓取和核心API进行了说明,本文将基于框架给出的测试模板工程,详细阐述下使用该框架进行安卓UI自动化测试的步骤. 模板工程 先来看一下模 ...

  3. APP自动化框架LazyAndroid使用手册(1)--框架简介

    作者:cryanimal  QQ:164166060 APP自动化简介 APP自动化,即通过自动化的方式,对APP施行一系列的仿按键输入.触摸屏输入.手势输入等操作,以达到对APP的功能进行自动化测试 ...

  4. Web自动化框架LazyUI使用手册(3)--单个xpath抓取插件详解(selenium元素抓取,有此插件,便再无所求!)

    概述 前面的一篇博文粗略介绍了基于lazyUI的第一个demo,本文将详细描述此工具的设计和使用. 元素获取插件:LazyUI Elements Extractor,作为Chrome插件,用于抓取页面 ...

  5. App 自动化框架设计思路

    最近在整理和学习Appium+Java 自动化框架,对APP自动化框架的部分设想参考了一些文章,先进行整理下: 框架的思路一: 思考引入:https://www.cnblogs.com/yunfeio ...

  6. APP自动化框架-ATX原理解析及JAVA版客户端

    作为网易开源的ATX APP自动化测试框架,对比现有的macaca自动化框架/Appium自动化框架,最大的特别就是在于可远程进行自动化测试 先给大家看一张我自己梳理的框架架构图 框架巧妙点: 1. ...

  7. Web自动化框架LazyUI使用手册(2)--先跑起来再说(第一个测试用例-百度搜索)

    作者:cryanimal QQ:164166060 上篇文章中,简要介绍了LazyUI框架,本文便来演示,如何从无到有快速搭建基于lazyUI的工程,并成功运行第一个测试用例. 本文以百度搜索为例,选 ...

  8. hibernate框架学习第二天:核心API、工具类、事务、查询、方言、主键生成策略等

    核心API Configuration 描述的是一个封装所有配置信息的对象 1.加载hibernate.properties(非主流,早期) Configuration conf = new Conf ...

  9. Web自动化框架LazyUI使用手册(1)--框架简介

    作者:cryanimal QQ:164166060 web端自动化简介 web端自动化,即通过自动化的方式,对Web页面施行一系列的仿鼠标键盘操作,以达到对Web页面的功能进行自动化测试的目的. 其一 ...

随机推荐

  1. sort()与sorted()区分开

    列表的排序方法是sort 可用list.sort() sorted()是BIF不能用list.sorted() 引发的异常AttributeError: 'list' object has no at ...

  2. jquery mouseout mouseover 多次执行

    用jquery,mouseout,mouseover,随着鼠标移动,事件被触发了多次(冒泡),换成js onmouseover,onmouseout也是一样.最终的解决办法是,用jquery,mous ...

  3. [LeetCode] Find Duplicate Subtrees 寻找重复树

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

  4. shell入门笔记2:字符串、数组、echo与printf

    说明: 本文是关于http://c.biancheng.net/cpp/shell/的相关笔记 shell字符串 字符串可以用单引号,也可以用双引号,也可以不用引号. 1 #!/bin/bash 2 ...

  5. hibernate--HelloWorld

    本次学习版本:hibernate-release-5.2.6.Final,要求java 1.8 和JDBC 4.2. hibernate是一个开放源代码的对象关系映射框架.对JDBC进行了非常轻量的封 ...

  6. codefroces 946F Fibonacci String Subsequences

    Description定义$F(x)$为$F(x−1)$与$F(x−2)$的连接(其中$F(0)="0"$,$F(1)="1"$)给出一个长度为$n$的$01$ ...

  7. NOIP 2009 最优贸易

    题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 为双向通行的道路 ...

  8. BZOJ3129: [Sdoi2013]方程

    拓展Lucas+容斥原理 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cs ...

  9. if(/专线$/.test(name))讲解

    如图: 这条语句的意思是:匹配以"专线"结尾的name字符串,满足条件的返回true,否则返回false

  10. git pull 报错 You have not concluded your merge (MERGE_HEAD exists).

    git pull时报错 解决方案: