App的自动化测试框架

移动App自动化测试(一)

目前移动App的自动化测试框架比较多,比如:Robotium、Expresso等,很多大公司甚至都会有自己的一套自动化测试框架。这篇文章简单Android自动化测试框架,iOS自动化测试框架也会少量提到。

  1. Monkey是Android SDK自带的测试工具,在测试过程中会向系统发送伪随机的用户事件流,如按键输入、触摸屏输入、手势输入等),实现对正在开发的应用程序进行压力测试,也有日志输出。实际上该工具只能对程序做一些压力测试,由于测试事件和数据都是随机的,不能自定义,所以有很大的局限性。

  2. MonkeyRunner也是Android SDK提供的测试工具。严格意义上来说MonkeyRunner其实是一个Api工具包,比Monkey强大,可以编写测试脚本来自定义数据、事件。缺点是脚本用Python来写,对测试人员来说要求较高,有比较大的学习成本。

  3. Instrumentation是早期Google提供的Android自动化测试工具类,虽然在那时候JUnit也可以对Android进行测试,但是Instrumentation允许你对应用程序做更为复杂的测试,甚至是框架层面的。通过Instrumentation你可以模拟按键按下、抬起、屏幕点击、滚动等事件。Instrumentation是通过将主程序和测试程序运行在同一个进程来实现这些功能,你可以把Instrumentation看成一个类似Activity或者Service并且不带界面的组件,在程序运行期间监控你的主程序。缺点是对测试人员来说编写代码能力要求较高,需要对Android相关知识有一定了解,还需要配置AndroidManifest.xml文件,不能跨多个App。

  4. UiAutomator也是Android提供的自动化测试框架,基本上支持所有的Android事件操作,对比Instrumentation它不需要测试人员了解代码实现细节(可以用UiAutomatorviewer抓去App页面上的控件属性而不看源码)。基于Java,测试代码结构简单、编写容易、学习成本,一次编译,所有设备或模拟器都能运行测试,能跨App(比如:很多App有选择相册、打开相机拍照,这就是跨App测试)。缺点是只支持SDK 16(Android 4.1)及以上,不支持Hybird App、WebApp。

  5. Espresso是Google的开源自动化测试框架。相对于Robotium和UIAutomator,它的特点是规模更小、更简洁,API更加精确,编写测试代码简单,容易快速上手。因为是基于Instrumentation的,所以不能跨App。配合Android Studio来编写测试的简单例子

  6. Selendroid:也是基于Instrumentation的测试框架,可以测试Native App、Hybird App、Web App,但是网上资料较少,社区活跃度也不大。

  7. Robotium也是基于Instrumentation的测试框架,目前国内外用的比较多,资料比较多,社区也比较活跃。缺点是对测试人员来说要有一定的Java基础,了解Android基本组件,不能跨App。

  8. Athrun是淘宝出的一个移动测试框架/平台,同时支持iOS和Android。Android部分也是基于Instrumentation,在Android原有的ActivityInstrumentationTestCase2类基础上进行了扩展,提供一整套面向对象的API。这里有详细介绍。

  9. UIAutomation是iOS平台下的测试框架,目前用的比较多的框架。

  10. Appium是最近比较热门的框架,社区也很活跃。后一章我会重点介绍这个自动化测试框架。

移动App自动化测试(二)

上一篇介绍了App自动化测试的目前主流的几种方式,那么这篇我就来介绍下Appium

什么是Appium?

Appium是Sauce Labs公司的一个移动测试框架,该公司专注于Web、Mobile和桌面应用的自动化测试,为企业开发者提供Web应用测试平台。

它的优点:

  • 开源;

  • 支持Native App、Hybird App、Web App;

  • 支持Android、iOS、Firefox OS;

  • Server也是跨平台的,你可以使用Mac OS X、Windows或者Linux;

它的哲理是:

  • 用Appium自动化测试不需要重新编译App;

  • 支持很多语言来编写测试脚本,Java、Javascript、PHP、Python、C#、Ruby等主流语言;

  • 不需要为了自动化测试来重造轮子,因为扩展了WebDriver。(WebDriver是测试WebApps的一种简单、快速的自动化测试框架,所以有Web自动化测试经验的测试人员可以直接上手);

  • 移动端自动化测试应该是开源的;

它的设计理念:

  • Client/Server架构,运行的时候Server端会监听Client端发过来的命令,翻译这些命令发送给移动设备或模拟器,然后移动设备或模拟器做出响应的反应。正是因为这种架构,所以Client可以使用Appium client libraries多种语言的测试脚本,而且Server端完全可以部署在服务器上,甚至云服务器。

  • Session,每个Client连接到Server以后都会有一个Session ID,而且Client发送命令到Server端都需要这个Session ID,因为这个seesion id代表了你所打开的浏览器或者是移动设备的模拟器。所以你甚至可以打开N个Session,同时测试不同的设备或模拟器。

  • Desired Capabilities,其实就是一个键值对,设置一些测试的相关信息来告诉Server端,我们需要测试iOS、还是Android,或者换是WebApp等信息。

  • Appium Server,使用Node.js写的,所以可以直接用NPM来进行安装。

  • Appium Client,可以使用Java、PHP、Python、Javascript、C#、Ruby等主流语言来编写测试用例。

相关限制:

  • 如果你在Windows使用Appium,你没法使用预编译专用于OS X的.app文件,因为Appium依赖OS X专用的库来支持iOS测试,所以在Windows平台你不能测试iOS Apps。这意味着你只能通过在Mac上来运行iOS测试。

总结:

  • 在iOS部分是封装了UIAutomation;Android 4.2以上是用UiAutomator,Android 2.3 ~ 4.1用的是 Instrumentation,也就说Appium同时封装了UiAutomator和Instrumentation。所以Appium拥有了以上几大框架的所有优点:能跨App,支持Native App、Hybird App、Web App的测试;不仅支持N种语言来编写你的测试脚本,而且可以让开发人员完全脱离源代码来编写。

移动App自动化测试(三)

Appium Server端的简单安装

Appium是支持Mac、Windows和Linux的。

  1. 安装Node.js:Mac推荐使用Homebrew,Windows可以直接官网下载;如果是Windows用户,把你的安装路径添加到环境变量方便使用(默认是在C:Program FilesNodejs)npm。

  2. 安装Git。

  3. 安装JDK,并设置JAVA_HOME环境变量。

  4. 安装Android SDK,并设置ANDROID_HOME环境变量;把sdk下的tools和platform-tools也设置环境变量。

安装好以上所有依赖,就可以安装Appium Server了。Windows和Mac下都有2种使用安装方式:

  1. 使用命令行安装:

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">npm install -g appium appium &</pre>

2. 或者直接下载GUI桌面应用。


Appium Server配置和使用

以下步骤以Mac下的Appium GUI为例(Windows下的Appium GUI使用可以参考官网文档):

  1. 打开Appium,点击Doctor按钮。可以检测上面安装依赖项是否成功安装
移动App自动化测试(三)

2.然后点击Android图标的配置,选择你要测试的Apk包。一旦选择好APK包,这里的Package和Launch Activity直接会在下拉框显示出来,个人觉得这是Appium框架的非常大的优点,无需APK源码,减少与开发人员的沟通成本。

移动App自动化测试(三)

3. 然后配置Advanced中的SDK路径和签名keystore信息(其实可以不配置签名这一项也可以测试APK包,但是签名项验证测试也还是非常重要的)。

移动App自动化测试(三)

4. 其他设置可以暂时都默认,然后直接运行Launch。运行的相关信息和错误日志都会直接显示在这里:

移动App自动化测试(三)

到这一步为止Appium Server已经成功启动了。


Appium Client的配置

Appium Client的安装可以用Maven来构建:

  1. Maven配置代码:

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>2.1.0</version></dependency></pre>

2. 或者直接下载jar包,添加到测试项目中。


利用UiAutomatorviewer来获取控件的信息

编写测试用例需要模拟各种按键点击、滚动、输入文字等信息,前面已经提到使用Appium的测试框架可以脱离源代码,那么是如何获取Apk上的控件信息呢?这里介绍UiAutomatorviewer这个Android SDK自带的工具。

  1. 如果Android SDK下的tools路径有添加到环境变量,直接可以在命令行输入:

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">$ uiautomatorviewer</pre>

没有添加环境变量,直接在SDK目录tools/下面有个uiautomatorviewer,双击打开。

2. 把要测试的Apk安装到模拟器或者手机设备中,如果是手机设备用USB连接到PC。

3.然后使用uiautomatorviewer查看控件信息,用我自己的Demo为例:

移动App自动化测试(三)

从右下控件属性框可以看到控件的任何属性。下一篇我将会为大家介绍如何利用这些属性编写自动化测试脚本

移动App自动化测试(四)

通过之前几篇大家应该对App自动化测试有个基本了解,也对Appium这个框架有了一定认识。

这一篇是这一系列的最后一篇,教大家怎么写一个测试脚本,用的语言是Java。

我用Java写了3个测试脚本,分别测试Native App、Hybird App、Web App。用TestNG单元测试框架。

测试Native App

一个TestNG测试脚本需要3个必要的部分:

  • setup(),用于测试前初始化工作:

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">@BeforeClasspublic void setup() throws Exception {//你要测试的APKFile appDir = new File("Apk");File app = new File(appDir, "Kuozhiv2.apk");DesiredCapabilities capabilities = new DesiredCapabilities();capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "");//测试平台capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"Google_Nexus_4");//Android版本capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");//Package namecapabilities.setCapability(MobileCapabilityType.APP_PACKAGE,"com.edusoho.kuozhi");//Launch Activitycapabilities.setCapability(MobileCapabilityType.APP_ACTIVITY,".KuozhiActivity");capabilities.setCapability(MobileCapabilityType.APP,app.getAbsolutePath());driver = new AndroidDriver(new URL("//127.0.0.1:4723/wd/hub"),capabilities);driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);System.out.print("App is launched!");}</pre>

  • tearDown(),用于测试完毕清理工作:

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">@AfterClasspublic void tearDown() throws Exception {driver.quit();}</pre>

  • 至少1个test()测试用例:

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">@Testpublic void splashTest() throws Exception {Thread.sleep(2000);int Y = driver.manage().window().getSize().getHeight();int X = driver.manage().window().getSize().getWidth();int sX = (int) (X * 0.8);int sY = (int) (Y * 0.5);int eX = (int) (X * 0.2);int eY = sY;int duration = 600;//第一个模拟滑动driver.swipe(sX, sY, eX, eY, duration);//第二个模拟滑动driver.swipe(sX, sY, eX, eY, duration);WebElement enterMainActivityElement = driver.findElement(By .id("com.edusoho.kuozhi:id/splash_ok_btn"));enterMainActivityElement.click();WebElement enterSchoolElement = driver.findElement(By .id("com.edusoho.kuozhi:id/qr_other_btn"));enterSchoolElement.click();}</pre>

这个测试NativeApp的脚本比较简单:滑动2次Splash,点击进入,登录等事件。注:抓取控件可以根据ID,如果控件没有ID还可以用XPath。

测试效果:

移动App自动化测试(四)

测试Hybird App

在测试Hybird App的时候,有一点需要注意:

只有测试设备或者模拟器版本是Android 4.4(API 19)及以上,Appium才能探测到一个Activity中的WebView控件,如果低于Android 4.4(API 19),那么改用Selendroid模式。因为在4.4上WebView是Chromium内核,之前都是Webkit内核。

如何定位WebView中页面的元素?

UiAutomatorViewer只能定位Activity中的UI元素,WebView已经是最小单元UI,不能解析WebView中页面元素。这里我使用Chrome DevTools中的inspect来解析WebView中的UI元素。

移动App自动化测试(四)
  • setup()初始化部分代码:

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">@BeforeClasspublic void setup() throws Exception {//other code ...//如果你测试的设备的Android版本低于4.4,那么Automation Name要改成"Selendroid",Appium Server端也要改成"Selendroid"capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,"Appium");//other code ...}</pre>

  • test()测试用例部分代码:

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">@Testpublic void webViewLogin() throws Exception {//other code ...Set<String> contextNames = driver.getContextHandles();for (String contextName : contextNames) { System.out.println("--->" + contextName); if (contextName.contains("NATIVE_APP")) { srtActivityContextString = contextName; } if (contextName.contains("WEBVIEW")) { strWebViewContext = contextName; }}driver.context(strWebViewContext);//把driver切换到WebView的context,再抓取WebView内页面的UI元素//other code ...}</pre>

测试效果:

移动App自动化测试(四)

测试Web App

严格来说,这部分测试可以交给Web测试人员,因为测试WAP页面,也是Web测试人员的一部分工作。不过作为App测试人员,也可以掌握这部分技能。

用Appium框架测试WebApp的时候有以下几点需要注意:

  • 目前Appium的WebApp测试只支持Chrome浏览器。

  • Chrome在x86模拟器上除非自己编译,否则是装不上或者存在各种问题的。请用ARM模拟器或真机,或者直接使用selenium测试框架直接对Web进行测试。因为我用的Genymotion模拟器,是x86的,所以发现了这个坑。可以在官网文档看到这个说明。

  • setup()测试初始化之前,需要指定浏览器名称,如下代码:

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">@BeforeClasspublic void setup() throws Exception {//other code ...capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "chrome");//other code ...}</pre>

  • driver需要指定webview的url,如下代码:

<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; overflow-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">@Testpublic void registerTest() throws Exception {//other code ...driver.get(web_app_url;}</pre>

测试效果:

移动App自动化测试(四)

通过这3个测试脚本,我们应该基本掌握了移动App测试脚本的编写。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容