测试类的编写:
package com.m1905.dd.mobile; import com.robotium.solo.By; import com.robotium.solo.Solo; import android.test.ActivityInstrumentationTestCase2;
public class testdd extends ActivityInstrumentationTestCase2 { private Solo solo; private static String mainActivity = "com.m1905.dd.mobile.act.StartAct"; private static String PackName = "com.m1905.dd.mobile"; private static Class<?> launchActivityClass; static{ try{ launchActivityClass = Class.forName(mainActivity); }catch(ClassNotFoundException e){ throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public testdd() { super(launchActivityClass); // TODO Auto-generated constructor stub } @Override public void setUp() throws Exception { super.setUp(); solo = new Solo(getInstrumentation(),getActivity()); } @Override public void tearDown() throws Exception { solo.finishOpenedActivities(); } public void testGetHaoYou() throws Exception{ //登陆页面后首先完成登陆的操作 solo.clickOnView(solo.getView("rbnMine")); solo.clickOnImage(0); Thread.sleep(2000); solo.clearEditText(0); solo.enterText((android.widget.EditText) solo.getView("edtUname"), "sylovezp"); solo.enterText((android.widget.EditText) solo.getView("edtUpass"),"qqqqqq"); Thread.sleep(1000); solo.clickOnButton("登录"); //进入活动页面 solo.clickOnView(solo.getView("rbnActivities")); //切换我的活动/热门活动/正在点评/已结束活动 solo.clickOnButton(0); solo.clickOnText("我的活动"); solo.scrollDown(); solo.clickOnButton(0); solo.clickOnText("正在点评"); solo.scrollDown(); solo.clickOnButton(0); solo.clickOnText("已结束"); solo.scrollDown(); solo.clickOnButton(0); solo.clickOnText("热门活动"); Thread.sleep(2000); //进入热门活动第一条中的活动详情页 solo.clickOnImage(0); //切换活动规则/关于影片/他们报名啦页面 solo.clickOnButton("关于影片"); //观看视频 solo.clickOnView(solo.getView("btnActPlay")); Thread.sleep(3000); solo.clickOnView(solo.getView("btnBack")); //报名参加,看看是否正确记录手机号 solo.clickOnText("我要报名"); solo.clickOnView(solo.getView("btnNext")); solo.clickOnView(solo.getView("btnBack")); //退出活动详情页 solo.clickOnView(solo.getView("btnBack")); //以下执行一个简单的登陆/退出的操作 //findElementById("rbnMine").doClick();//athrun solo.clickOnView(solo.getView("rbnMine"));//当一个页面文本名称出现多个时,就不能用text了,此时可以用这个方法 Thread.sleep(1000); solo.clickOnText("系统设置"); Thread.sleep(1000); solo.clickOnButton("退出登录"); Thread.sleep(1000); } }
配置文件的配置:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.m1905.dd.mobile.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.m1905.dd.mobile" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <uses-library android:name="android.test.runner" /> </application> </manifest>