APP自动化框架LazyAndroid使用手册(3)--核心API介绍
作者:黄书力
概述
在前一篇博文中,简要介绍了一款安卓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使用手册(2)--元素自动抓取
作者:黄书力 概述 前面的一篇博文简要介绍了安卓自动化测试框架LazyAndroid的组成结构和基本功能,本文将详细描述此框架中元素自动抓取工具lazy-uiautomaterviewer的使用方法. ...
- APP自动化框架LazyAndroid使用手册(4)--测试模板工程详解
概述 前面的3篇博文分别对lazyAndroid的框架简介.元素抓取和核心API进行了说明,本文将基于框架给出的测试模板工程,详细阐述下使用该框架进行安卓UI自动化测试的步骤. 模板工程 先来看一下模 ...
- APP自动化框架LazyAndroid使用手册(1)--框架简介
作者:cryanimal QQ:164166060 APP自动化简介 APP自动化,即通过自动化的方式,对APP施行一系列的仿按键输入.触摸屏输入.手势输入等操作,以达到对APP的功能进行自动化测试 ...
- Web自动化框架LazyUI使用手册(3)--单个xpath抓取插件详解(selenium元素抓取,有此插件,便再无所求!)
概述 前面的一篇博文粗略介绍了基于lazyUI的第一个demo,本文将详细描述此工具的设计和使用. 元素获取插件:LazyUI Elements Extractor,作为Chrome插件,用于抓取页面 ...
- App 自动化框架设计思路
最近在整理和学习Appium+Java 自动化框架,对APP自动化框架的部分设想参考了一些文章,先进行整理下: 框架的思路一: 思考引入:https://www.cnblogs.com/yunfeio ...
- APP自动化框架-ATX原理解析及JAVA版客户端
作为网易开源的ATX APP自动化测试框架,对比现有的macaca自动化框架/Appium自动化框架,最大的特别就是在于可远程进行自动化测试 先给大家看一张我自己梳理的框架架构图 框架巧妙点: 1. ...
- Web自动化框架LazyUI使用手册(2)--先跑起来再说(第一个测试用例-百度搜索)
作者:cryanimal QQ:164166060 上篇文章中,简要介绍了LazyUI框架,本文便来演示,如何从无到有快速搭建基于lazyUI的工程,并成功运行第一个测试用例. 本文以百度搜索为例,选 ...
- hibernate框架学习第二天:核心API、工具类、事务、查询、方言、主键生成策略等
核心API Configuration 描述的是一个封装所有配置信息的对象 1.加载hibernate.properties(非主流,早期) Configuration conf = new Conf ...
- Web自动化框架LazyUI使用手册(1)--框架简介
作者:cryanimal QQ:164166060 web端自动化简介 web端自动化,即通过自动化的方式,对Web页面施行一系列的仿鼠标键盘操作,以达到对Web页面的功能进行自动化测试的目的. 其一 ...
随机推荐
- [翻译] TensorFlow Programmer's Guide之Frequently Asked Questions(问得频率最多的几个问题)
目录: 特点和兼容性(Features and Compatibility) 建立一个TensorFlow图(Building a TensorFlow graph) 运行一个TensorFlow计算 ...
- Spring-cloud(四)服务发现与消费:ribbon的使用
说明: ribbon是spring-cloud中作为服务消费者的一种角色,客户端可以通过它来对服务提供者的服务进行消费, 比如本例中是服务提供者注册到注册中心,服务提供者提供了一个服务接口,返回一个h ...
- C++中 return,break,continue的用法
引用:https://blog.csdn.net/smf0504/article/details/51315835 https://blog.csdn.net/ting_junhui/article/ ...
- Python 字符串常见的27个操作
有字符串 mystr = "hello world itcast and itcastcpp",以下是常见的操作: 1. mystr.find(str, start=0, end= ...
- “百度杯”CTF比赛 九月场_Code(PhpStorm)
题目在i春秋ctf大本营 打开链接是一张图片,审查元素发现关键词base64,图片的内容都以base64加密后的形式呈现,查看url形式,应该是一个文件读取的漏洞 这里我们可以采用url/index. ...
- python3全栈开发-什么是粘包、粘包现象、如何解决粘包
一.粘包现象 让我们基于tcp先制作一个远程执行命令的程序(1:执行错误命令 2:执行ls 3:执行ifconfig) 注意注意注意: res=subprocess.Popen(cmd.decode( ...
- [NOIp 2016]换教室
Description 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有 $2n$ 节课程安排在 $n$ 个时间段上.在第 $i$($1 \leq ...
- poj2406 连续重复子串
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 41110 Accepted: 17099 D ...
- hdu 1542 线段树扫描(面积)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- nginx负载均衡及详细配置
接上篇nginx配置,然后再准备两台web服务器: nginx服务器:192.168.0.241 web1:192.168.0.141 web2:192.168.0.142 一.两台web服务器先安装 ...