当前位置: 首页 > 编程日记 > 正文

5个最佳的Android测试框架

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

谷歌的Android生态系统正在不断地迅速扩张。有证据表明,新的移动OEM正在攻陷世界的每一个角落,不同的屏幕尺寸、ROM /固件、芯片组以及等等等等,层出不穷。于是乎,对于Android开发人员而言,处理存储碎片变得越来越困窘。

不过幸运的是,Android(还有iOS)开发人员可以无限制地访问一些先进的基于云的解决方案,如Testdroid Cloud,就可以在大规模的真实设备上执行自动化测试以确保质量,赞吧。此外,不同的Android测试框架的出现也大大减轻了Android开发人员的负担。

今天,我们就要说说5款最常用的Android测试框架,并且每个框架都给出了基本的代码示例。

1.Robotium

不可否认,Robotium曾是Android世界之初使用最广泛的Android测试框架,风靡一时。由于它与Android有着相似的Selenium,所以它能够使得API的测试变得简单起来。

Robotium是一个扩展于JUnit的开源库,运用多种有用的方法来支持Android UI测试。它提供的强大的自动化黑箱测试范例,可用于Android应用(原生的和混合的)和web测试。只要源代码允许,你就可以通过Robotium写功能、系统和验收测试方案,以及测试应用。

Robotium的代码示例:

// Public void for the operation
public void testRecorded() throws Exception {
// Wait for the text 'Hello!' to be shown for newbie
if (solo.waitForText("Hello!")) {
// R class ID identifier for 'Sign in' - and click it
solo.clickOnView(solo.findViewById("com.twitter.android.R.id.sign_in"));
// R class ID identifier for entering username
solo.enterText((EditText) solo.findViewById("com.twitter.android.R.id.login_username"),"username");
// R class ID identifier for entering password
solo.enterText((EditText) solo.findViewById("com.twitter.android.R.id.login_password"),"password");
// R class ID identifier for clicking log in
solo.clickOnView(solo.findViewById("com.twitter.android.R.id.login_login"));
// Wait until log in is done
solo.waitForActivity("HomeTabActivity");
}
// Activate the text field to compose a tweet
solo.clickOnView(solo.findViewById("com.twitter.android.R.id.menu_compose_tweet"));
// Type the tweet
solo.enterText((EditText) solo.findViewById("com.twitter.android.R.id.edit"), "Testdroid");
// Tweeting!
solo.clickOnView(solo.findViewById("com.twitter.android.R.id.composer_post"));
}

为了给大家提供便捷,还有一个用Robotium构建的用于测试脚本创建的一个非常棒的记录工具——Testdroid Recorder。当你在真实设备上执行实际行动时,它可以记录你的每一个步骤和每一个行为,并转换成JavaScript,以便于你进一步的修改。

并且,你还可以全权下载和使用它的扩展库——ExtSolo,它里面包含了多种还没有被纳入到Robotium中的实用方法,例如:

  • 支持任意分辨率的x、Y点击自动缩放
  • 多路径拖动
  • 测试故障时自动截图
  • 模拟地点
  • 更改设备语言
  • 控制WiFi连接

官方网站:https://code.google.com/p/robotium/

2.uiautomator

虽然Robotium是一个很好的测试框架,但是uiautomator能让你在测试Android应用和Android游戏时做得更多。谷歌的测试框架允许你在一个或多个设备上测试原生Android应用的用户界面(UI)。Uiautomator的另一个优点是,它运行的JUnit测试用例是有特殊权限的,这意味着测试用例可以跨越不同的进程。它还提供了五种不同的类给开发人员使用:

com.android.uiautomator.core.UiCollection;
com.android.uiautomator.core.UiDevice;
com.android.uiautomator.core.UiObject;
com.android.uiautomator.core.UiScrollable;
com.android.uiautomator.core.UiSelector

遗憾的是,uiautomator只能工作于API16或更高级别的Android设备上。它的另一个缺点是不支持web视图,也没有办法直接访问Android对象。

uiautomator的代码示例:

// Public void for the operation
public void testSignInAndTweet() throws Exception {
// Starting application:
getUiDevice().wakeUp(); // Press Home button to ensure we're on homescreen
getUiDevice().pressHome(); // Select 'Apps' and click button
new UiObject(new UiSelector().description("Apps")).click(); // Select 'Twitter' and click
new UiObject(new UiSelector().text("Twitter")).click(); // Locate and select 'Sign in'
UiSelector signIn = new UiSelector().text("Sign In"); // If button is available, click
UiObject signInButton = new UiObject(signIn);
if (signInButton.exists()) {
signInButton.click(); // Set the username
new UiObject(new
UiSelector().className("android.widget.EditText").instance(0)).setText("username");
new UiObject(new
UiSelector().className("android.widget.EditText").instance(1)).setText("password");
new UiObject(new UiSelector().className("android.widget.Button").
text("Sign In").instance(0)).click(); // Wait Sign in progress window
getUiDevice().waitForWindowUpdate(null, 2000); // Wait for main window
getUiDevice().waitForWindowUpdate(null, 30000);
}
new UiObject(new UiSelector().description("New tweet")).click(); // Typing text for a tweet
new UiObject(new UiSelector().className("android.widget.LinearLayout").instance(8)).
setText("Awesome #Testdroid!"); // Tweeting!
new UiObject(new UiSelector().text("Tweet")).click();

官方网站:http://developer.android.com/tools/help/uiautomator/index.html

3.Espresso

Espresso是由Google开源的一款最新的Android自动化测试框架,有助于于开发人员和测试人员锤炼出中意的用户界面。Espresso的API体积小、可预见、简单易学,构建在Android仪表框架的基础上。使用它,能让你快速编写出简洁可靠的Android UI测试。它支持API level 8级(Froyo)、10(Gingerbread),和15(Ice Cream Sandwich)及后续。

一方面它相当可靠,因为和UI线程是同步的,另一方面又非常之快,因为没有任何睡眠的必要(当某个毫秒,应用程序空转时,运行测试)。不过它同样不支持web视图。

Espresso的代码示例:

public void testEspresso() {
// Check if view with the text 'Hello.' is shown
onView(withText("Hello.")).check(matches(isDisplayed()));
// R class ID identifier for 'Sign in' - and click it
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/sign_in", null, null))).perform(click());
// R class ID identifier for entering username
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/login_username", null, null))).perform((typeText("username")));
// R class ID identifier for entering password
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/login_password", null, null))).perform((typeText("password")));
// R class ID identifier for clicking log in
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/login_login", null, null))).perform(click());
// Activate the text field to compose a tweet
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/menu_compose_tweet", null, null))).perform(click());
// Type the tweet
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/edit", null, null))).perform((typeText(”#Testdroid")));
// Tweeting!
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/composer_post", null, null))).perform(click());
}

官方网站:https://code.google.com/p/android-test-kit/wiki/Espresso

4.Calabash

Calabash是一款跨平台的自动化测试框架,支持Android和iOS原生和混合的应用程序。Calabash易于理解的语法,使得即使是非技术人员也可以在这两个移动平台上为app创建和执行自动化验收测试。Calabash的测试描述于Cucumber,然后在运行时转化为Robotium或Frank。它支持约80种不同的自然语言指令(控制器),并且可以使用Ruby和Java实现新的控制器。

Calabash的代码示例:

Feature: Login feature
Scenario: As a valid user I can log into my app
I wait for text "Hello"
Then I press view with id "Sign in"
Then I enter text "username" into "login_username"
Then I enter text "password" into "login_password"
Then I wait for activity "HomeTabActivity"
Then I press view with id "menu_compose_tweet"
Then I enter text "Testdroid" into field with id "edit"
Then I press view with id "composer_post"

官方网站:http://calaba.sh/

5.Appium

Appium是一款移动的自动化测试框架(和工具),支持iOS和Android原生和混合的移动Web应用程序。它内部使用的JSONWireProtocol通过Selenium的WebDriver,来与iOS和Android应用进行交互。它通过uiautomator(API level 16或更高)和Seledroid(API level 低于16)支持Android,通过UI Automation支持iOS,还有Android和iOS都支持的移动web如Selenium driver。

Appium的最大优点在于你几乎可以用任意一种编程语言(例如,Java、Objective-C、JavaScript、PHP、Ruby、Python和C#等)来编写Appium脚本而不必选择工具,兼容最重要的平台(Android和iOS)而不必安装和配置设备适应测试等等。并且,如果你熟悉Selenium的话,那么使用Appium用于移动app测试对你而言将是轻而易举的一件事。因为它们使用相同的WebDriver,并且以同样的方式使用DesiredCapabilities。所以Appium与Selenium在配置应用程序运行时有诸多相似之处。

Appium的代码示例:

# wait for hello
sleep(3)
textFields = driver.find_elements_by_tag_name('textField')
assertEqual(textFields[0].get_attribute("value"), "Hello")
# click sign-in button
driver.find_elements_by_name('Sign in')[0].click()
# find the text fields again, and enter username and password
textFields = driver.find_elements_by_tag_name('textField')
textFields[0].send_keys("twitter_username")
textFields[1].send_keys("passw0rd")
# click the Login button (the first button in the view)
driver.find_elements_by_tag_name('button')[0].click()
# sleep
sleep(3)
# click the first button with name "Compose"
driver.find_elements_by_name('Compose')[0].click()
# type in the tweet message
driver.find_elements_by_tag_name('textField')[0].send_keys(”#Testdroid is awesome!")
# press the Send button
driver.find_elements_by_name('Send')[0].click()
# exit
driver.quit()

官方网站:http://appium.io/

总结

以上就是我们列出的5款最棒的测试框架,可用于日常的Android构建,创立和修改。当然,每一种框架都有其优势和缺陷。Appium可以同时测试你的Android和iOS版本。但如果你是一个忠实的Android开发人员只开发安卓版本的app,那么,使用Robotium就很不错的。Testdroid Recorder还可为我们在生成测试脚本节省大量的时间和金钱(这是免费的哦!)。因此,好好思考下你的测试需求——功能测试、兼容性测试、UI测试等等——然后为自己选取最适合和最佳的Android测试框架。

转载于:https://my.oschina.net/zhugenqiang/blog/698409

相关文章:

【CTF】实验吧 凯撒变异

通过分析可以知道前四个“afZ_”四个的ASCII码值与“flag”的ASCII码值依次相差5&#xff0c;6&#xff0c;7&#xff0c;8。 #include <stdio.h> #include <string.h> int main () {char str[40]"afZ_r9VYfScOeO_UL^RWUc";int i0,j5;while(i<strlen…

ant design pro (八)构建和发布

一、概述 原文地址&#xff1a;https://pro.ant.design/docs/deploy-cn 二、详细 2.1、构建 当项目开发完毕&#xff0c;只需要运行一行命令就可以打包你的应用&#xff1a; npm run build 由于 Ant Design Pro 底层使用的 roadhog 工具&#xff0c;已经将复杂的流程封装完毕&a…

Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存

Linux进程间通信--进程&#xff0c;信号&#xff0c;管道&#xff0c;消息队列&#xff0c;信号量&#xff0c;共享内存 参考&#xff1a;《linux编程从入门到精通》,《Linux C程序设计大全》,《unix环境高级编程》 参考&#xff1a;C和指针学习 说明&#xff1a;本文非常的长…

PgSQL · 实战经验 · 如何预测Freeze IO风暴

背景和原理 有没有被突发的IO惊到过&#xff0c;有没有见到过大量的autovacuum for prevent wrap。 PostgreSQL 的版本冻结是一个比较蛋疼的事情&#xff0c;为什么要做版本冻结呢&#xff1f; 因为PG的版本号是uint32的&#xff0c;是重复使用的&#xff0c;所以每隔大约20亿…

【CTF】实验吧 传统知识+古典密码

对照顺序写下&#xff1a; 根据对应的干支得到 28 30 23 8 17 10 16 30 甲子 所有的数加60 得到 88 90 83 68 77 70 76 90 找到ASCII码对照表可得到XZSDMFLZ 题干中提到古典密码&#xff08;常用的就是栅栏密码和凯撒密码&#xff09; 栅栏密码&#xff08;两栏&#…

NSSize 尺寸

前言 结构体&#xff0c;这个结构体用来表示事物的宽度和高度。typedef CGSize NSSize;struct CGSize {CGFloat width;CGFloat height; };typedef struct CGSize CGSize; 1、NSSize 结构体变量的创建与调用 // NSSize 结构体变量的创建与赋值// 先定义变量&#xff0c;再赋值 N…

Android中对Log日志文件的分析[转]

一&#xff0c;Bug出现了&#xff0c; 需要“干掉”它 bug一听挺吓人的&#xff0c;但是只要你懂了&#xff0c;android里的bug是很好解决的&#xff0c;因为android里提供了LOG机制&#xff0c;具体的底层代码&#xff0c;以后在来分析&#xff0c;只要你会看bug&#xff0c;a…

Unix下C程序内存泄漏检测工具Valgrind安装与使用

Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。 Valgrind的最初作者是Julian Seward&#xff0c;他于2006年由于在开发Valgrind上的工作获得了第二届Google-OReilly开源代码奖。 Valgrind遵守GNU通用公共许可证条款&#xff0c;是一款自由软件。 官网…

【CTF】实验吧 robomunication

用audacity软件&#xff0c;猜测是摩斯密码 听到的都是“bi”或者“bu”&#xff0c;这里用b代表“bi”&#xff0c;“p”代表“bu” bbbb b bpbb bpbb ppp bpp bbbb bp p bb bbb p bbbb b pbp b pbpp bb p bb bbb (p b bb) ppp ppp bppb pbbb b b bppb 打括号那里显得较分散一…

Mac原生Terminal快速登录ssh

1. 创建rsa key 在终端中输入以下命令&#xff1a; ssh-keygen -t rsa完成之后可以在~/.ssh目录下找到公钥和私钥 如果你与我一样有使用gitlab&#xff0c;那么这个秘钥应该已经存在了&#xff0c;所以就不用重新建立了。 2.上传公钥到服务器 有教程会说&#xff0c;用scp或者类…

Java开发环境的搭建以及使用eclipse从头一步步创建java项目

原文&#xff1a;出自本人的Linux博客http://blog.csdn.net/unix21/article/details/18813173 一、Java 开发环境的搭建 这里主要说windows环境下怎么配置Java环境。如果是Linux环境参考本博客另一篇文章即可&#xff1a;Linux环境安装卸载JDK1.首先安装JDK java的SDK简称JDK。…

全球首届APMCon,带你给“应用性能”把把脉

前段时间&#xff0c;美国苹果公司应用程序购买商店和手机等一系列应用因技术故障中断服务&#xff0c;持续了约两个半小时。故障发生后&#xff0c;世界多地苹果用户纷纷吐槽无法购买和更新手机应用、无法备份等。其实&#xff0c;这不是苹果公司在线服务第一次掉线&#xff0…

【CTF】实验吧 The Flash-14

标题的提示是&#xff1a;闪电侠的第十四集用到的加密方式&#xff08;看来写CTF题要无所不知&#xff0c;不然咋能想到是一部剧&#xff09; 根据两两一组将数据分类 54 43 32 52 22 44 55 34 22 51 52 22 44 34 22 23 11 34 12 按照上表的对应关系可以得到…

XML 标签 首字母转换为大写

2019独角兽企业重金招聘Python工程师标准>>> public static String xmlTagCapitalize(String xmlStr) {String regex "<(/*[A-Za-z])>";regex "<([^>]*)>";Matcher matcher Pattern.compile(regex).matcher(xmlStr);StringBu…

简析 .NET Core 构成体系

简析 .NET Core 构成体系 Roslyn 编译器RyuJIT 编译器CoreCLR & CoreRTCoreFX(.NET Core Libraries).NET Core 代码开发、部署、运行过程总结前文介绍了.NET Core 在整个.NET 平台所处的地位&#xff0c;以及与.NET Framework的关系(原文链接)&#xff0c;本文将详细介绍.N…

【CTF】实验吧 奇怪的短信

和实验吧 The Flash-14有些类似&#xff0c;总共的数字数目是偶数&#xff0c;所以两两分开&#xff0c;题干中的“短信”是提示&#xff0c;观察两两分组的第二个数字没有超过四的&#xff0c;可以想到手机上的九键 例如第一组数&#xff1a;33 对应的是F&#xff0c;最后全部…

mybatis结合log4j打印SQL日志

mybatis结合log4j打印SQL日志1.Maven引用jar包 默认的mybatis不能打印出SQL日志&#xff0c;不便于查看调试&#xff0c;需要结合log4jdbc-log4j2就可以完整的输入SQL的调试信息。 pom.xml 配置maven&#xff0c;注意以下3个都需要<dependency><groupId>org.bgee.l…

EOS Chain/Wallet RPC API的PHP开发包

2019独角兽企业重金招聘Python工程师标准>>> 介绍一个EOS Chain/Wallet RPC API的PHP开发包。 开始 你可以查看EOS的RPC API参考&#xff0c;但要注意缺少一些较新的方法。Wallet RPC API实现EOS v1.1.0 of RPC API reference。此外&#xff0c;这些文档中的一些示例…

深入浅出理解Paxos算法

Paxos算法是莱斯利兰伯特&#xff08;英语&#xff1a;Leslie Lamport&#xff0c;LaTeX中的「La」&#xff09;于1990年提出的一种基于消息传递且具有高度容错特性的一致性算法。 Paxos算法一开始非常难以理解&#xff0c;但是一旦理解其实也并不难&#xff0c;之所以难理解其…

显示界面的普通仓库

实际脚本如下 procedure xianshi_PTCK(Npc: TNormNpc; Player: TPlayObject);procedure CWPRPTCK_QWP(Npc: TNormNpc; Player: TPlayObject; Args: TArgs); beginplayer.TakebackStorageItem(Args.Int[0]);cangku.xianshi_PTCK(npc,player); end; procedure xianshi_PTCK(Np…

【CTF】实验吧 围在栅栏中的爱

对摩斯密码进行解码&#xff1a;kiqlwtfcqgnsoo QWE是键盘上的前三个&#xff0c;ABC是26个字母的前三个。所以&#xff0c;二者有这样的对应关系。 #include <stdio.h> #include <string.h> int main () {char zc[]"abcdefghijklmnopqrstuvwxyz"; cha…

nginx tomcat https

1.首先确保机器上安装了openssl和openssl-devel #yum install openssl #yum install openssl-devel2. server {listen 443 ssl;server_name vota.swmmotors.com.cn;ssl_certificate cert/vota.swmmotors.com.cn_bundle.crt; #当前conf/目录下ssl_certificate_…

Spring4实战学习笔记

《Spring4实战 第4版》2016年4月新出版的&#xff0c;之前的第三版看起来还是不错的&#xff0c;所以看到新版就直接买下来。 英文版源码地址&#xff1a;Spring in Action, Fourth Edition Covers Spring 4 1.IOC装配Bean 参考【Spring实战4 2.2】&#xff0c;作者提倡无XML…

vmstat 命令

2019独角兽企业重金招聘Python工程师标准>>> 1.用法 vmstat [-a] [-n] [-S unit] [delay [ count]] vmstat [-s] [-n] [-S unit] vmstat [-m] [-n] [delay [ count]] vmstat [-d] [-n] [delay [ count]] vmstat [-p disk partition] [-n] [delay [ count]] vmstat […

【CTF】实验吧 疑惑的汉字

考察的是当铺密码&#xff1a; 王夫 井工 夫口 由中人 井中 夫夫 由中大&#xff1a;67 84 70 123 82 77 125 当铺密码就是一种将中文和数字进行转化的密码&#xff0c;算法相当简单:当前汉字有多少笔画出头&#xff0c;就是转化成数字几。

Date PHP

转载于:https://www.cnblogs.com/liuliang389897172/p/10087895.html

Java多线程的11种创建方式以及纠正网上流传很久的一个谬误

创建线程比较传统的方式是继承Thread类和实现Runnable&#xff0c;也可以用内部类&#xff0c;Lambda表达式&#xff0c;线程池&#xff0c;FutureTask等。 经常面试会问到继承Thread类和实现Runnable的区别&#xff0c;然后网上会流传如下这样的说法&#xff0c;这是错误的。…

【CTF】实验吧 古典密码

一共是35个字符分成5*7或者7*5 最终选择5行7列 首先变动第一行的位置&#xff0c;然后根据第一行变动的位置&#xff0c;依次变动下面的行 OCU{CFT ELXOUYD ECTNGAH OHRNFIE NM}IOTA CTF{COU LTYOUEX CHANGET HEINFOR MATION} CTF{COULTYOUEXCHANGETHEINFORMATION}

对比React Native、dcloud、LuaView三个框架技术(内部)

转载自&#xff1a;http://www.jianshu.com/p/ee1cdb33db8d主要对比React Native和5SDK&#xff08;就是dcloud的SDK&#xff09;两个&#xff1a; 开发语言&#xff1a;三个都是用其他语言来统一开发IOS、android应用的框架技术&#xff0c;其中&#xff0c;React Native是使用…

spring boot 临时文件过期

2019独角兽企业重金招聘Python工程师标准>>> 第一种方案&#xff1a;-Djava.io.tmpdir /xxx 第二种方案&#xff1a; 线上的系统中不能上传文件了&#xff0c;出现如下错误&#xff1a; org.springframework.web.multipart.MultipartException: Could not parse mu…