实现功能:
用户登录与注销
package com.aaa.bbb;
import android.os.SystemClock;
import android.test.ActivityInstrumentationTestCase2;
import com.jayway.android.robotium.solo.*;
import android.app.Activity;
import android.app.Instrumentation;
import android.view.inputmethod.InputMethodManager;
import android.view.KeyEvent;
import android.widget.*;
import android.view.ViewGroup;
@SuppressWarnings( "rawtypes" )
public class Test_Case_01 extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_CLASSNAME = "com.aaa.bbb.ccc.SplashActivity";
private static Class< ? > launchActivityClass;
static {
try {
launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
private Solo solo;
protected Integer screenWidth = null;
protected Integer screenHeight = null;
private Instrumentation inst;
public Test_Case_01() {
super("com.aaa.bbb", launchActivityClass);
}
@Override
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
inst = this.getInstrumentation();
}
@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
public void testRecorded() {
//等待 Activity "GuideActivity" 启动
assertTrue("Activity \"GuideActivity\" is not started.", solo.waitForActivity("GuideActivity", 30000));
solo.sleep(2000);
//拖拽:从 (57.71%, 57.75%) 到 (18.13%, 56.25%)
solo.drag(toX((float)0.57708335), toX((float)0.18125), toY((float)0.5775), toY((float)0.5625), 6);
solo.sleep(2000);
//拖拽:从 (74.58%, 48.25%) 到 (32.08%, 49.00%)
solo.drag(toX((float)0.74583334), toX((float)0.32083333), toY((float)0.4825), toY((float)0.49), 5);
solo.sleep(2000);
//拖拽:从 (76.25%, 46.13%) 到 (40.21%, 45.00%)
solo.drag(toX((float)0.7625), toX((float)0.40208334), toY((float)0.46125), toY((float)0.45), 4);
solo.sleep(2000);
//按下 图片 路径为 com.aaa.bbb.ccc:id/my_slider[0].
solo.clickOnView(solo.getViewByPath("com.aaa.bbb:id/my_slider[0]"));
solo.sleep(2000);
//等待 Activity "SplashActivity" 启动
assertTrue("Activity \"SplashActivity\" is not started.", solo.waitForActivity("SplashActivity", 30000));
solo.sleep(2000);
//等待 Activity "MainTabActivity" 启动
assertTrue("Activity \"MainTabActivity\" is not started.", solo.waitForActivity("MainTabActivity", 30000));
solo.sleep(2000);
//按下 RadioButton "个人中心".
solo.clickOnText("个人中心");
solo.sleep(2000);
//等待 Activity "UserCenterActivity" 启动
assertTrue("Activity \"UserCenterActivity\" is not started.", solo.waitForActivity("UserCenterActivity", 30000));
solo.sleep(2000);
for(int i=0;i<5;i++){
//按下 TextView "登录".
solo.clickOnText("^登录");
solo.sleep(2000);
//等待 Activity "UserLoginActivity" 启动
assertTrue("Activity \"UserLoginActivity\" is not started.", solo.waitForActivity("UserLoginActivity", 30000));
solo.sleep(2000);
//按下 EditText (ID: edit_usename)
solo.clickOnView("edit_usename");
solo.sleep(5000);
//输入文字:"131112222111"
enterText((EditText)solo.getView("edit_usename"), "1123131");
solo.sleep(2000);
//按下 EditText (ID: edit_passwrod)
solo.clickOnView("edit_passwrod");
solo.sleep(2000);
//输入文字:"12092111"
enterText((EditText)solo.getView("edit_passwrod"), "123456aaa");
solo.sleep(2000);
//按下 按钮 "登 录".
solo.clickOnButton("^登 录");
solo.sleep(2000);
//等待 Activity "MainTabActivity" 启动
assertTrue("Activity \"MainTabActivity\" is not started.", solo.waitForActivity("MainTabActivity", 30000));
solo.sleep(2000);
//按下 TextView "注销".
solo.clickOnText("^注销");
solo.sleep(2000);
}
}
protected float toX( float x ) {
return x * getScreenWidth();
}
protected float toY( float y ) {
return y * getScreenHeight();
}
protected int getScreenWidth() {
if ( screenWidth == null ) {
screenWidth = getActivity().getWindowManager().getDefaultDisplay()
.getWidth();
}
return screenWidth;
}
protected int getScreenHeight() {
if ( screenHeight == null ) {
screenHeight = getActivity().getWindowManager().getDefaultDisplay()
.getHeight();
}
return screenHeight;
}
public void enterText(int index, String text) {
solo.enterText(index, text);
hideKeyboard(solo.getEditText(index));
}
public void enterText(EditText editText, String text) {
solo.clearEditText(editText);
solo.enterText(editText, text);
hideKeyboard(editText);
}
public void hideKeyboard(){
solo.getCurrentActivity().runOnUiThread(new Runnable() {
public void run()
{
solo.getCurrentActivity().getWindow().setSoftInputMode(3);
}
});
}
public void hideKeyboard(final EditText editText) {
solo.getCurrentActivity().runOnUiThread(new Runnable() {
public void run()
{
InputMethodManager imm = (InputMethodManager)inst.getTargetContext().getSystemService("input_method");
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
});
}
}