针对测试对象方式:(米聊)
Robotium 在只有APK文件时进行JUnit testing
测试工具准备:
1. 米聊.apk (Package name: com.xiaomi.channel)
2. Eclipse (eclipse IDE for java EE Developers).
3. 插件:ADT (Android Development Tools)
4. SDK (Android SDK)
5. JDK
6. Robotium
7.插件:re-sign.jar;robotium-solo-2.5.jar;robotium-solo-2.5-javadoc.jar;
测试要点:
自动化测试步骤在百度 google一搜一堆 ,本人只根据自身遇到的“坑”进行说明:
要点一:“Package name”获取:
这里用到一个工具“re-sign.jar”。启用这个工具其实只要JDK安装正常双击就可以。但如果默认启用被更改就麻烦了。本人的第一个小坑出现。解决方法是呼出cmd,在工具所在的当前目录输入命令 java -jar re-sign.jar回车即可。然后把米聊程序拖拽到re-sign.jar程序内,就获取到package name 和MainActivity (这2个在后续操作时会用到须记住)
要点二:"AndroidMainfest.xml"文件修改:
<?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xiaomi.channel.Test"
android:versionCode="1"
android:versionName="1.0">
//增加下面红色标注2行
<supports-screensandroid:anyDensity="true"/>
<uses-sdkandroid:targetSdkVersion="8"/>
//8代表的是你启用设置的SDK版本号
<uses-sdk android:minSdkVersion="8" />
<instrumentation android:targetPackage="com.xiaomi.channel"//我们刚才生成并需要记录的包名android:name="android.test.InstrumentationTestRunner"/>
<application android:icon="@drawable/icon"android:label="@string/app_name">
<uses-library android:name="android.test.runner"/>
</application>
</manifest>
OK,以上设置完成,保存就可以了
要点三:测试框架修改:
测试框架在网上已经存在,需要针对自身做下简单修改就可以使用:
图示:
代码:
package com.xiaomi.channel.Test;
import android.test.ActivityInstrumentationTestCase2;
import com.jayway.android.robotium.solo.Solo;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ActivityTest extends ActivityInstrumentationTestCase2{
public ActivityTest(Class activityClass) {
super(activityClass);
// TODO Auto-generated constructor stub
}
//“TARGET_PACKAGE_ID”和“LAUNCHER_ACTIVITY_FULL_CLASSNAME”是我们用工具生成的2个(注意大小写保持一致,不然运行时程序报:“Test run failed: Process crashed”)
private static final String TARGET_PACKAGE_ID = "com.xiaomi.channel";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.xiaomi.channel.ui.ChannelLauncherActivity";
private staticClass<?> launcherActivityClass;
static {
try {
launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}
catch (ClassNotFoundExceptione) {
throw newRuntimeException(e);
}
}
public ActivityTest() throwsClassNotFoundException{
super(TARGET_PACKAGE_ID,launcherActivityClass);
}
private static Solo solo;
@Override
protected void setUp() throws Exception {
solo = newSolo(getInstrumentation());
getActivity();
}
public static void test(){
//这里我们写的测试脚本
TestScript1.test_login(solo);
// TestScript2.test_talk(solo);
}
public void tearDown() throws Exception {
try{
}
catch(Throwable e) {
e.printStackTrace();
}
//getActivity().finish();
//super.tearDown();
solo.sleep(10000000);//测试调试时用于查看,真正执行时把上面2处执行,此处注掉。
}
}
要点四:TestScript1.test_login(solo)脚本范例;
package com.xiaomi.channel.Test;
import com.jayway.android.robotium.solo.Solo;
public class TestScript1 {
public static void test_login(Solo solo){
// TODO Auto-generated methodstub
solo.waitForActivity("ChannelLauncherActivity",5000);//ChannelLauncherActivity说明如下
solo.waitForActivity("LoginActivity",500);
solo.clearEditText(0);
solo.enterText(0, "米聊号");
solo.enterText(1, "密码");
solo.clickOnCheckBox(0);
solo.clickOnButton("确定");
solo.sleep(5000);
solo.waitForActivity("XMTabActivity",5000);
if(solo.searchText("是否要创建桌面快捷方式")){
solo.clickOnButton("取消");
solo.sleep(2000);
if(solo.searchText("欢迎使用米聊")){
solo.clickOnButton("确认");
}
}
solo.sleep(2000);
if(solo.searchText("通过加密上传通讯录")){
solo.clickOnButton("是");
}
solo.sleep(5000);
}
}
ChannelLauncherActivity说明:使用工具“hierarchyviewer.bat”位置在\android-sdk-windows\tools目录下。
启用米聊后,每执行的操作,都会在hierarchyviewer.bat上看到对应的Activity。
尾牙:目前只初步研究到这个程度了,希望能有所帮助吧。