在自动化测试中,UI上经常有一些控件是没有名称的,那么此时,就可以通过id来找到这些控件。
案例:对两个EditText进行测试
- 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() {
- EditText enter = (EditText)solo.getView(R.id.enterText);
- solo.enterText(enter, "this is enter text");
- solo.sleep(2000);
- int typeId = solo.getCurrentActivity().getResources().getIdentifier("typeText", "id", "com.tangbc.tedit");
- View typeView = solo.getView(typeId);
- solo.typeText((EditText)typeView, "this is type text");
- solo.sleep(2000);
- }
- }
【转载:http://blog.csdn.net/tt75281920/article/details/26929549】