5款最常用的Android测试框架(含代码示例)

news2024/9/22 15:46:04

前言

今天,我们就要说说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测试框架。

PS:这里分享一套软件测试的自学教程合集。对于在测试行业发展的小伙伴们来说应该会很有帮助。除了基础入门的资源,博主也收集不少进阶自动化的资源,从理论到实战,知行合一才能真正的掌握。全套内容已经打包到网盘,内容总量接近500个G。【点击文末小卡片免费领取】

这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1264363.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Django二转Day02

http #1 http 是什么#2 http特点#3 请求协议详情 -请求首行---》请求方式,请求地址,请求协议版本 -请求头---》key:value形式 -referer:上一次访问的地址 -user-agenet:客户端类型 -name&#x…

Python编程控制Android手机操作技巧示例代码

文章目录 你应该拥有的东西截图TemplateMatching 滑动打电话给某人手机录屏打开手机发送 Whatsapp 消息关于Python技术储备一、Python所有方向的学习路线二、Python基础学习视频三、精品Python学习书籍四、Python工具包项目源码合集①Python工具包②Python实战案例③Python小游…

HttpRunner原来还能这么用,大开眼界!!!

hook机制 Httprunner 框架中的 hook 机制相当于unittest框架中的 setup , teardown 函数,用来进行测试用例执行之前的环境初始化以及测试用例执行完毕之后的环境清理操作。 httprunner 中的 hooks 机制可以用在测试用例层级也可以用在测试步骤层级,其关键…

【UE】绘制抛物线并投射物体

效果 步骤 1. 先新建父类为Actor的蓝图,这里命名为“BP_发射物” 打开“BP_发射物”,添加一个球形的静态网格体和一个发射物移动组件 2. 新建一个父类为角色的蓝图,这里命名为“BP_绘制抛物线” 打开“BP_绘制抛物线” 我们希望可以通过控制…

距离向量路由协议——IGRP和EIGRP

IGRP-内部网关路由协议 IGRP(Interior Gateway Routing Protocol,内部网关路由协议)是一种动态距离向量路由协议,它是Cisco公司在20世纪80年代中期设计的,是Cisco专用路由协议。目前在Cisco高版本的IOS已经对IGRP不提…

掌握 Node.js 事件循环,让代码更高效

🤍 前端开发工程师(主业)、技术博主(副业)、已过CET6 🍨 阿珊和她的猫_CSDN个人主页 🕠 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 🍚 蓝桥云课签约作者、已在蓝桥云…

socket 一个完整的不错的示例

从客户端向服务器端发送信息时&#xff0c;在服务器端有打印显示&#xff1b; 检测环境常用&#xff0c;备份一下 0&#xff0c;公共头文件代码 //config.h#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #inc…

Android中的多进程

在Android中也可以像pc一样开启多进程&#xff0c;这在android的编程中通常是比较少见的&#xff0c;以为在一个app基本上都是单进程工作就已经足够了&#xff0c;有一些特殊的场景&#xff0c;我们需要用多进程来做一些额外的工作&#xff0c;比如下载工作等。 在Android的An…

UI上传组件异步上传更改为同步

实现异步方法 JavaScript 异步 实现异步的五种实现方法_js异步-CSDN博客 这两种比较经常用。 因为上传组件是异步上传的通过Async和await配合使用可以上传完照片视频后返回的地址在继续走下去&#xff0c;而不是图片视频地址还未获取时就上传后端了。

leetcode算法之字符串

目录 1.最长公共前缀2.最长回文子串3.二进制求和4.字符串相乘 1.最长公共前缀 最长公共前缀 class Solution { public:string longestCommonPrefix(vector<string>& strs) {//法一&#xff1a;两两比较string ret strs[0];for(int i1;i<strs.size();i){ret f…

[栈溢出+参数跟踪] [ZJCTF 2019]Login

题目来源 buuctf——[ZJCTF 2019]Login 本题主要考察参数溯源的能力。 参考链接 https://zhuanlan.zhihu.com/p/570607303 题目信息 64位&#xff0c;ubuntu16&#xff0c;开了金丝雀 C风格的代码&#xff0c;并且将admin登录信息写入代码中。 溢出点不在这里&#xff0c;但是…

井盖倾斜怎么办?智能井盖传感器监测方法

井盖倾斜是一个紧迫的问题&#xff0c;如果不及时处理可能会导致道路安全性下降&#xff0c;进而增加车辆和行人发生意外的风险。为应对这一问题现已开发出智能井盖传感器&#xff0c;它可以持续监测井盖的状态&#xff0c;一旦发现倾斜等异常情况会立即发出警报。 在智慧城市的…

揭秘:软件测试中Web请求的完整流程!

在软件开发的过程中&#xff0c;测试是一个至关重要的环节。而在现代互联网应用中&#xff0c;Web请求是很常见的一个测试需求。本文将介绍Web请求的完整测试流程&#xff0c;帮助读者更好地理解软件测试的关键步骤。 一、测试准备阶段 在进行Web请求测试之前&#xff0c;测试…

【C】语言 sizeof和strlen的对比

目录 sizeof和strlen的对比 1.1 sizeof 1.2 strlen strlen的模拟实现 strlen介绍 1.3 sizeof 和 strlen的对比 sizeof和strlen的对比 1.1 sizeof 在学习操作符的时候&#xff0c;我们学习了 sizeof &#xff0c; sizeof 计算变量所占内存内存空间⼤⼩的&#xff0c;…

我试图通过这篇文章告诉你,什么是神奇的泛化调用。

关于 RPC 调用&#xff0c;大家肯定都是比较熟悉的了&#xff0c;就是在微服务架构下解决系统间通信问题的一个玩意。 其中的典型代表之一就是 Dubbo 了&#xff1a; 在微服务架构下&#xff0c;我们针对某个 RPC 接口&#xff0c;我们一般有两个角色。 服务消费者 (Dubbo Con…

【前端首屏加载速度优化(0): 谷歌浏览器时间参数】

DOMContentLoaded 浏览器已经完全加载了 HTML&#xff0c;DOM树构建完成&#xff0c;但是像是 <img> 和样式表等外部资源可能并没有下载完毕。 Load DOM树构建完成后&#xff0c;继续加载 html/css 中的外部资源&#xff0c;加载完成之后&#xff0c;视为页面加载完成。…

继电保护-变压器纵联差动保护MATLAB仿真模型

微❤关注“电气仔推送”获得资料&#xff08;专享优惠&#xff09; 原理概述 差动保护是在两端设置的保护&#xff0c;通过比较两端测回来的电气量&#xff0c;进而看是否需要动作&#xff0c;纵联差动保护是变压器主保护。 纵联差动保护基本原则 双绕组变压器实现纵联差动…

java反射和注解2-自定义注解

对反射有一定了解过后学习注解就会轻松许多&#xff0c;这篇文章会创建创建字段&#xff0c;方法&#xff0c;参数上面的注解&#xff0c;并且通过反射的形式将注解内容拿出来。 下面是自定义注解的简短讲解 1&#xff0c;自定义注解&#xff0c;创建一个只能修饰方法的注解 …

【外汇天眼】概率之道:规则交易背后的心态与风险控制

交易的结果&#xff0c;无外乎盈利和亏损。 对于这些结果&#xff0c;我们的心态管理很重要、很重要&#xff01; 这涉及到“保住江山”和“东山再起”。 先来说说交易盈利的结果 当一个人能够稳定盈利的时候&#xff0c;很容易产生自我麻痹、忘乎所以的飘飘然心态&#xff0…

C++基础 -9- 函数的默认参数

函数默认格式(图片代码段呈现) #include "iostream"using namespace std;void rlxy(int a100) {cout << a << endl; }int main() {rlxy();rlxy(99); }函数默认参数注意事项 函数的默认参数从左开始推导 错误写法 正确写法