安卓自动化测试:Robotium(一)测试demo

1、下载Robotium
打开: http://code.google.com/p/robotium/
下载:robotium-solo-3.4.1.jar (写测试用例时需要依赖此包)
2、需要把APK重新签名,因为robotium要求被测应用和测试代码要有一致的签名, 所以我们需要把下载到的apk,通过re-sign.jar来产生debug key的apk,这个重新生成的apk就会跟测试项目签名一致了。下载完后,需要配置ANDROID_HOME,就是安卓SDK的位置,然后把APK拉到图标上,就会自动生成一个debug key的apk,如果无法直接单击re-sign.jar运行,需要切换到放置该jar文件的目录,cmd执行java -jar re-sign.jar产生新apk的过程中会弹出一个信息框,记得截下图,因为里面有两个信息我们等会的代码中需要用到安装产生的apk。然后连接测试机(或打开模拟器,模拟器器一定要打开才能安装成功),然后打开命令行 adb install xx_debug.apk(新生成apk的名称) , 或者双击apk文件也可以安装
2、创建测试工程
1)File -》New-》Other -》Android Test Project ,由于这是基于apk测试,选择this project
2)在工程中添加 android-junit-report-1.5.8.jar 以及 robotium-solo-5.6.3.jar 这两个包
3)Manifest.xml修改
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kamoCtest.login.hwj" //包名
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="13" />

<instrumentation
android:name="com.zutubi.android.junitreport.JUnitReportTestRunner" //导入单元测试报告jar
android:targetPackage="com.coolwi.km.user" /> //被测应用package名
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> //给予可写权限

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
4)新建测试类SHSCListActivity 继承ActivityInstrumentationTestCase2 ,编写测试代码,运行测试用例选择 Run AS->AndroidJUnitTest
5)记一个测试demo
/*
@测试模块:测试账号管理
@测试内容:账号管理模块中各个小模块链接点击正常不报错

*/
package com.coolwi.km.test;

import com.robotium.solo.*;
import android.test.ActivityInstrumentationTestCase2;

@SuppressWarnings("rawtypes")
public class SHSCListActivity extends ActivityInstrumentationTestCase2 {
private Solo solo;

private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.coolwi.km.activity.StartActivity";

private static Class<?> launcherActivityClass;
static{
try {
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

@SuppressWarnings("unchecked")
public SHSCListActivity() throws ClassNotFoundException {
super(launcherActivityClass);
}

public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation());
getActivity();
}

@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}

public void testRun() {
// Take screenshot
solo.takeScreenshot();
// Wait for activity: ‘com.coolwi.km.activity.StartActivity‘
solo.waitForActivity("StartActivity", 2000);

// Wait for activity: ‘com.coolwi.km.activity.LoginActivity‘
assertTrue("LoginActivity is not found!", solo.waitForActivity("LoginActivity"));
// Enter the text: ‘15088132075‘
solo.clearEditText((android.widget.EditText) solo.getView("et_phone"));
solo.enterText((android.widget.EditText) solo.getView("et_phone"), "15088132075");
// Click on Empty Text View
solo.clickOnView(solo.getView("et_pwd"));
// Enter the text: ‘123456ab‘
solo.clearEditText((android.widget.EditText) solo.getView("et_pwd"));
solo.enterText((android.widget.EditText) solo.getView("et_pwd"), "123456ab");
// Click on 登 录
solo.clickOnView(solo.getView("bt_ok"));
// Wait for activity: ‘com.coolwi.km.main.MainActivity‘
assertTrue("MainActivity is not found!", solo.waitForActivity("MainActivity"));
// Wait for dialog
solo.waitForDialogToOpen(5000);
// Set default small timeout to 10377 milliseconds
Timeout.setSmallTimeout(10377);
// Click on 账号管理
solo.clickOnView(solo.getView("radio4"));
//商户商城购买测试
// Click on 商户商城
solo.clickOnView(solo.getView("tv_shsc"));
// Wait for activity: ‘com.coolwi.km.main.account.shopping.SHSCListActivity‘
assertTrue("SHSCListActivity is not found!", solo.waitForActivity("SHSCListActivity"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_ddjl"));
// Wait for activity: ‘com.coolwi.km.main.account.shopping.SHSCMyOrderActivity‘
assertTrue("SHSCMyOrderActivity is not found!", solo.waitForActivity("SHSCMyOrderActivity"));
// Scroll to ImageView
android.widget.ListView listView0 = (android.widget.ListView) solo.getView(android.widget.ListView.class, 0);
solo.scrollListToLine(listView0, 0);
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on Empty Text View
solo.clickOnView(solo.getView("et_search"));
// Enter the text: ‘卡莫‘
solo.clearEditText((android.widget.EditText) solo.getView("et_search"));
solo.enterText((android.widget.EditText) solo.getView("et_search"), "卡莫");
// Take screenshot
solo.takeScreenshot();
// Click on 卡莫
solo.clickOnView(solo.getView("et_search"));
// Press search button
solo.pressSoftKeyboardSearchButton();
// Set default small timeout to 12368 milliseconds
Timeout.setSmallTimeout(12368);
// Click on 立即购买
solo.clickOnView(solo.getView("bt_pay"));
// Wait for activity: ‘com.coolwi.km.main.account.shopping.SHSCWebActivity‘
assertTrue("SHSCWebActivity is not found!", solo.waitForActivity("SHSCWebActivity"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Take screenshot
solo.takeScreenshot();
// Take screenshot
solo.takeScreenshot();
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
/*
// Wait for activity: ‘com.coolwi.km.activity.WebAppActivity‘
assertTrue("WebAppActivity is not found!", solo.waitForActivity("WebAppActivity"));*/
// Click on 商户协议
solo.clickOnView(solo.getView("tv_mdxy"));
// Take screenshot
solo.takeScreenshot();
// Wait for activity: ‘com.coolwi.km.activity.WebAppActivity‘
assertTrue("WebAppActivity is not found!", solo.waitForActivity("WebAppActivity"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
/* // Click on 认证信息
solo.clickOnView(solo.getView("tv_rzxx"));
// Wait for activity: ‘com.coolwi.km.main.account.ZZRZActivity‘
assertTrue("ZZRZActivity is not found!", solo.waitForActivity("ZZRZActivity"));*/
// Click on 认证信息
solo.clickOnView(solo.getView("tv_rzxx"));
// Wait for activity: ‘com.coolwi.km.main.account.ZZRZActivity‘
assertTrue("ZZRZActivity is not found!", solo.waitForActivity("ZZRZActivity"));
// Take screenshot
solo.takeScreenshot();
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 系统设置
solo.clickOnView(solo.getView("tv_xtsz"));

// Take screenshot
solo.takeScreenshot();
// Wait for activity: ‘com.coolwi.km.main.account.XTSZActivity‘
assertTrue("XTSZActivity is not found!", solo.waitForActivity("XTSZActivity"));
// Click on 修改密码
solo.clickOnView(solo.getView("rela_xgmm"));
// Wait for activity: ‘com.coolwi.km.main.account.UpdatePwdActivity‘
assertTrue("UpdatePwdActivity is not found!", solo.waitForActivity("UpdatePwdActivity"));
// Click on Empty Text View
solo.clickOnView(solo.getView("et_yzm"));
// Enter the text: ‘159753‘
solo.clearEditText((android.widget.EditText) solo.getView("et_yzm"));
solo.enterText((android.widget.EditText) solo.getView("et_yzm"), "159753");
// Click on Empty Text View
solo.clickOnView(solo.getView("et_pwd"));
// Enter the text: ‘123456ab‘
solo.clearEditText((android.widget.EditText) solo.getView("et_pwd"));
solo.enterText((android.widget.EditText) solo.getView("et_pwd"), "123456ab");
// Click on Empty Text View
solo.clickOnView(solo.getView("et_confirm_pwd"));
// Enter the text: ‘123456ab‘
solo.clearEditText((android.widget.EditText) solo.getView("et_confirm_pwd"));
solo.enterText((android.widget.EditText) solo.getView("et_confirm_pwd"), "123456ab");
// Click on 确 认
solo.clickOnView(solo.getView("bt_ok"));
// Click on 意见反馈
solo.clickOnView(solo.getView("rela_yjfk"));
// Wait for activity: ‘com.coolwi.km.main.account.FeedBackActivity‘
assertTrue("FeedBackActivity is not found!", solo.waitForActivity("FeedBackActivity"));
// Take screenshot
solo.takeScreenshot();
/* // Click on Empty Text View
solo.clickOnView(solo.getView("et_content"));*/
// Click on Empty Text View
solo.clickOnView(solo.getView("et_content"));
// Enter the text: ‘Gyg‘
solo.clearEditText((android.widget.EditText) solo.getView("et_content"));
solo.enterText((android.widget.EditText) solo.getView("et_content"), "Gyg");
// Click on 提 交
solo.clickOnView(solo.getView("bt_submit"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 使用帮助
solo.clickOnView(solo.getView("tv_help"));
// Wait for activity: ‘com.coolwi.km.activity.WebAppActivity‘
assertTrue("WebAppActivity is not found!", solo.waitForActivity("WebAppActivity"));
/* // Wait for activity: ‘com.coolwi.km.activity.WebAppActivity‘
assertTrue("WebAppActivity is not found!", solo.waitForActivity("WebAppActivity"));*/
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 账户充值
solo.clickOnView(solo.getView("tv_zhcz"));
// Wait for activity: ‘com.coolwi.km.main.account.ZHCZActivity‘
assertTrue("ZHCZActivity is not found!", solo.waitForActivity("ZHCZActivity"));
// Click on 100元
solo.clickOnView(solo.getView("bt1"));
// Enter the text: ‘100‘
solo.clearEditText((android.widget.EditText) solo.getView("et_amount"));
solo.enterText((android.widget.EditText) solo.getView("et_amount"), "100");
// Click on ImageView
solo.clickOnView(solo.getView("ib_czjl"));
// Wait for activity: ‘com.coolwi.km.activity.WebAppActivity‘
assertTrue("WebAppActivity is not found!", solo.waitForActivity("WebAppActivity"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 月结账单
solo.clickOnView(solo.getView("tv_yjzd"));
// Take screenshot
solo.takeScreenshot();
// Wait for activity: ‘com.coolwi.km.main.account.YJZDActivity‘
assertTrue("YJZDActivity is not found!", solo.waitForActivity("YJZDActivity"));

// Scroll to ImageView
android.widget.ListView listView1 = (android.widget.ListView) solo.getView(android.widget.ListView.class, 0);
solo.scrollListToLine(listView1, 0);
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 关于卡莫
solo.clickOnView(solo.getView("tv_about"));
// Wait for activity: ‘com.coolwi.km.main.account.AboutActivity‘
assertTrue("AboutActivity is not found!", solo.waitForActivity("AboutActivity"));
// Click on 新版本检测 当前版本号:2.5.16-T 已是最新版
solo.clickOnView(solo.getView("re_check"));
// Click on 联系我们
solo.clickOnView(solo.getView("re_lxwm"));
// Wait for activity: ‘com.coolwi.km.main.account.RZTSActivity‘
assertTrue("RZTSActivity is not found!", solo.waitForActivity("RZTSActivity"));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 分享二维码给好友 点击二维码分享给好友
solo.clickOnView(solo.getView("re_code"));
// Wait for activity: ‘com.mob.tools.MobUIShell‘
assertTrue("MobUIShell is not found!", solo.waitForActivity("MobUIShell"));
// Click on Empty Text View
solo.clickOnView(solo.getView(android.widget.TextView.class, 0));
// Click on ImageView
solo.clickOnView(solo.getView("iv_back"));
// Click on 系统设置
solo.clickOnView(solo.getView("tv_xtsz"));
// Wait for activity: ‘com.coolwi.km.main.account.XTSZActivity‘
assertTrue("XTSZActivity is not found!", solo.waitForActivity("XTSZActivity"));
// Click on 退出登录
solo.clickOnView(solo.getView("rela_tcdl"));
// Wait for dialog
solo.waitForDialogToOpen(5000);
// Click on 确定
solo.clickOnView(solo.getView("bt_confirm"));
// Wait for activity: ‘com.coolwi.km.activity.LoginActivity‘
assertTrue("LoginActivity is not found!", solo.waitForActivity("LoginActivity"));
}
}

原文地址:https://www.cnblogs.com/LinxiHuang/p/9246756.html

时间: 2024-10-04 22:57:26

安卓自动化测试:Robotium(一)测试demo的相关文章

安卓自动化业务层测试接口

安卓自动化业务层测试接口 阅读前需知的基本知识: 1. uiautomator 脚本的执行通过shell命令调起执行,向执行的方法传递参数也是通过shell命令 2  调起的执行方法所在类必须要继承UIATestCasel类 3.  调起方法内部,职能通过bundle获取外部传递的参数,而不是通过函数参数的方式传递 抛出问题: 在新的自动化测试框架中,业务层属于API层级,非继承于UIATestCasel类(com.android.uiautomator.testrunner.UiAutomat

零成本实现接口自动化测试 – Java+TestNG 测试Restful service

本文是转载Wade Xu的文章http://www.cnblogs.com/wade-xu/p/4229805.html 接口自动化测试 – Java+TestNG 测试 Restful Web Service 关键词:基于Rest的Web服务,接口自动化测试,数据驱动测试,测试Restful Web Service, 数据分离,Java+Maven+TestNG 本文主要介绍如何用Java针对Restful web service 做接口自动化测试(数据驱动),相比UI自动化,接口自动化稳定性

[安卓自动化测试] 001.UIAutomator初探

*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } a { color: #4183C4; text-decoration: none; } a.absent { color: #cc0000; } a.anchor { display: block; padding-left: 30px; margin-left: -30px; cursor: poin

Wookmark-jQuery-master 瀑布流插件使用介绍,含个人测试DEMO

要求 必备知识 本文要求基本了解 Html/CSS,  JavaScript/JQuery. 开发环境 Dreamweaver CS6 / Chrome浏览器 演示地址 演示地址 资料下载 测试预览截图(抬抬你的鼠标就可以看到演示地址哦): 程序核心代码看这里: $(function(){ function show(){ var colors=["#BA4A3A","#5BB5D6","#8EA83B","#EE5C92",

单独编译和使用webrtc音频降噪模块(附源码+测试demo)

webrtc的音频处理模块分为降噪ns,回音消除aec,回声控制acem,音频增益agc,静音检测部分.另外webrtc已经封装好了一套音频处理模块APM,如果不是有特殊必要,使用者如果要用到回声消除,音频增益等较为复杂的模块时,最好使用全部的音频处理模块二不要单独编译其中一部分以免浪费宝贵的时间. 但是音频降噪部分较为简单,用起来也就几个函数,除了需要传入的音频数据以外,需要调整的参数也就是音频采样率和降噪等级.另外这部分代码采用纯C语言语法编写,可以跨平台编译.整个算法也不算特别复杂,运行起

[Android-Appium]: 安卓自动化测试: 环境准备

安卓自动化测试:环境的准备: 在做安卓自动化测试之前,需要在你的电脑上做如下环境的准备, 我用的是 Appium做的自动化测试. 安装Appium server, 安装路径:http://appium.io/    https://bitbucket.org/appium/appium.app/downloads/ Appium 需要 Node JS,所以需要安装它,安装路径:https://nodejs.org/en/安装完后,      可以打开CMD,输入 node –v,如果能显示出 n

unity游戏开发之自定义事件测试demo

上文中写了unity游戏开发自定义消息事件点击打开链接 下面是测试demo 第一,打卡unity,新建一个场景,然后新建一个空的游戏对象,如图中的EventObj 第二步,测试代码EventTest.as,直接拖拽给上面的空游戏对象EentObj 测试代码如下: using UnityEngine; using System.Collections; public class EventTest : MonoBehaviour { // Use this for initialization v

Android 自动化测试—robotium(七) 使用Junit_report测试报告

使用Robotium进行测试的时候,要想可以导出明了的测试结果,可以使用junitreport来实现 junit-report下载地址:https://github.com/jsankey/android-junit-report 1.建立一个Android的测试工程,导入Robotium包,并建立好测试用例; 2.右键测试工程 → Properties → java build path → Libraries, 点击Add External JARs; 选择android-junit-rep

安卓自动化测试:Robotium(二)page object 模式完善测试框架

1.目前比较流行的设计模式就是page object,page object就是把页面作为对象,在使用中传递页面对象,来使用页面对象中相应的成员或者方法,能更好的提现java的面向对象和封装特性2.具体思想:测试框架分为四大类1)编写抽象父类:建立Utils包,存放工具类,用于自动完成唤醒手机.解锁.登录等前置操作,之后的用例只需继承抽象父类即可2)提取控件ID类:在工程中建立从com.pacagename.test.elements包用例存放控件ID类.命名一般以Element开头,后面接Ac