solo.typeText和solo.enterText方法都可以对EditeText进行测试,实现过程存在几点不同:
1,字面上:type是按键输入;enter就是回车,那你输入后的东西键入进去
2、实现上:typeText方法是robotium框架调用系统Instrumentation类里面的sendStringSync方法来实现的;enterText是调用TextView里面setText方法来实现的。
3、显示上:typeText在测试过程中能看到输入的痕迹;enterText则没有输入痕迹。
接下来看代码:
- package com.tangbc.tedit.test;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import android.test.ActivityInstrumentationTestCase2;
- import android.view.View;
- import android.widget.EditText;
- import com.robotium.solo.Solo;
- import com.tangbc.tedit.MainActivity;
- import com.tangbc.tedit.R;
- public class EditTest extends ActivityInstrumentationTestCase2{
- private Solo solo;
- public EditTest() {
- super(MainActivity.class);
- }
- @Before
- public void setUp() throws Exception {
- solo = new Solo(getInstrumentation(), getActivity());
- }
- @After
- public void tearDown() throws Exception {
- solo.finishOpenedActivities();
- }
- @Test
- public void test() {
- solo.enterText(0, "this is enter text");
- //使用searchEditText方法确认EditText中的输入内容是正确的
- System.out.println(solo.searchEditText("this is enter text"));
- solo.sleep(2000);
- solo.typeText(1, "this is type text");
- //使用searchText方法也是可行的
- System.out.println(solo.searchText("this is type text"));
- solo.sleep(2000);
- }
- }
package com.tangbc.tedit.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import android.widget.EditText;
import com.robotium.solo.Solo;
import com.tangbc.tedit.MainActivity;
import com.tangbc.tedit.R;
public class EditTest extends ActivityInstrumentationTestCase2{
private Solo solo;
public EditTest() {
super(MainActivity.class);
}
@Before
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
@After
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
@Test
public void test() {
solo.enterText(0, "this is enter text");
//使用searchEditText方法确认EditText中的输入内容是正确的
System.out.println(solo.searchEditText("this is enter text"));
solo.sleep(2000);
solo.typeText(1, "this is type text");
//使用searchText方法也是可行的
System.out.println(solo.searchText("this is type text"));
solo.sleep(2000);
}
}
点我下载源码