雾山的Robotium学习笔记---通过Id寻找控件 .

在自动化测试中,UI上经常有一些控件是没有名称的,那么此时,就可以通过id来找到这些控件

直接上案例:

这是对两个EditText进行测试

  1. package com.tangbc.tedit.test;  
  2.   
  3. import org.junit.After;  
  4. import org.junit.Before;  
  5. import org.junit.Test;  
  6.   
  7. import android.test.ActivityInstrumentationTestCase2;  
  8. import android.view.View;  
  9. import android.widget.EditText;  
  10.   
  11. import com.robotium.solo.Solo;  
  12. import com.tangbc.tedit.MainActivity;  
  13. import com.tangbc.tedit.R;  
  14.   
  15. public class EditTest extends ActivityInstrumentationTestCase2{  
  16.     private Solo solo;  
  17.   
  18.     public EditTest() {  
  19.         super(MainActivity.class);  
  20.     }  
  21.   
  22.     @Before  
  23.     public void setUp() throws Exception {  
  24.         solo = new Solo(getInstrumentation(), getActivity());  
  25.     }  
  26.   
  27.     @After  
  28.     public void tearDown() throws Exception {  
  29.         solo.finishOpenedActivities();  
  30.     }  
  31.   
  32.     @Test  
  33.     public void test() {  
  34.         EditText enter = (EditText)solo.getView(R.id.enterText);  
  35.         solo.enterText(enter, "this is enter text");  
  36.         solo.sleep(2000);  
  37.           
  38.         int typeId = solo.getCurrentActivity().getResources().getIdentifier("typeText""id""com.tangbc.tedit");  
  39.         View typeView = solo.getView(typeId);  
  40.         solo.typeText((EditText)typeView, "this is type text");  
  41.         solo.sleep(2000);  
  42.     }  
  43.   
  44. }  
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);
	}

}

点我下载源码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值