雾山的Robotium学习笔记---CheckBox,RadioGroup&RadioButton的测试方法及结果判定 .

在Android中,CheckBox和RadioButton是很常见的控件,那怎样用Robotium对该空间进行测试呢;

我们在Robotium的API文档的solo类中可以看到以下两种方法,通过CheckBox和RadioButton的index值来找到该控件

  1. public void clickOnCheckBox(int index)  
  2. Clicks a CheckBox matching the specified index.  
  3. Parameters:  
  4. index - the index of the CheckBox to click. 0 if only one is available  
public void clickOnCheckBox(int index)
Clicks a CheckBox matching the specified index.
Parameters:
index - the index of the CheckBox to click. 0 if only one is available
  1. public void clickOnRadioButton(int index)  
  2. Clicks a RadioButton matching the specified index.  
  3. Parameters:  
  4. index - the index of the RadioButton to click. 0 if only one is available  
public void clickOnRadioButton(int index)
Clicks a RadioButton matching the specified index.
Parameters:
index - the index of the RadioButton to click. 0 if only one is available

当然也可以 通过button的text值来直接调用。( CheckBox 和RadioButton都是Button的子类):

  1. public void clickOnButton(String text)  
  2. Clicks a Button displaying the specified text. Will automatically scroll when needed.  
  3. Parameters:  
  4. text - the text displayed by the Button. The parameter will be interpreted as a regular expression  
public void clickOnButton(String text)
Clicks a Button displaying the specified text. Will automatically scroll when needed.
Parameters:
text - the text displayed by the Button. The parameter will be interpreted as a regular expression

而判定按钮有没有选择可以调用以下的方法:

isCheckBoxChecked()和isRadioButtonChecked()


下面是代码:

  1. package com.tangbc.choosedemo.test;  
  2.   
  3. import org.junit.Test;  
  4.   
  5. import android.test.ActivityInstrumentationTestCase2;  
  6.   
  7. import com.robotium.solo.Solo;  
  8. import com.tangbc.choosedemo.MainActivity;  
  9.   
  10. public class ChooseTest extends ActivityInstrumentationTestCase2{  
  11.     private Solo solo;  
  12.   
  13.     public ChooseTest() {  
  14.         super(MainActivity.class);  
  15.     }  
  16.   
  17.     @Override  
  18.     protected void setUp() throws Exception {  
  19.         solo = new Solo(getInstrumentation(), getActivity());  
  20.     }  
  21.   
  22.     @Override  
  23.     protected void tearDown() throws Exception {  
  24.         solo.finishOpenedActivities();  
  25.     }  
  26.   
  27.     @Test  
  28.     public void test() {  
  29.         //直接调用名称或CheckBox的index都可以找到该控件   
  30.         solo.clickOnButton("WOW");  
  31.         //调用isCheckBoxChecked(int index)方法,判断WOW按钮有没有被选中   
  32.         boolean expected = true;  
  33.         boolean actual = solo.isCheckBoxChecked(0);  
  34.         assertEquals("WOW没有被选中", expected, actual);  
  35.   
  36.           
  37.         for(int i = 0; i < 10; i++){  
  38.             solo.clickOnCheckBox(1);  
  39.             solo.clickOnCheckBox(1);  
  40.             solo.takeScreenshot();  
  41.             solo.sleep(2000);  
  42.         }  
  43.     }  
  44.       
  45.     public void test1(){  
  46.         //直接调用clickOnButton的名称或clickOnRadioButton的index都可以找到该控件   
  47.         solo.clickOnRadioButton(0);  
  48.         //调用isRadioButtonChecked(int index)方法,判断boy按钮有没有被选中   
  49.         boolean BoyExpected = true;  
  50.         boolean BoyActual = solo.isRadioButtonChecked(0);  
  51.         assertEquals("boy没被选中", BoyExpected, BoyActual);  
  52.           
  53.         solo.clickOnButton("girl");  
  54.         boolean GirlExpected = true;  
  55.         boolean GirlActual = solo.isRadioButtonChecked(1);  
  56.         System.out.println(GirlActual);  
  57.         assertEquals("girl没被选中", GirlExpected, GirlActual);  
  58.           
  59.         solo.takeScreenshot();  
  60.         solo.sleep(2000);  
  61.     }  
  62.   
  63. }  
package com.tangbc.choosedemo.test;

import org.junit.Test;

import android.test.ActivityInstrumentationTestCase2;

import com.robotium.solo.Solo;
import com.tangbc.choosedemo.MainActivity;

public class ChooseTest extends ActivityInstrumentationTestCase2{
	private Solo solo;

	public ChooseTest() {
		super(MainActivity.class);
	}

	@Override
	protected void setUp() throws Exception {
		solo = new Solo(getInstrumentation(), getActivity());
	}

	@Override
	protected void tearDown() throws Exception {
		solo.finishOpenedActivities();
	}

	@Test
	public void test() {
		//直接调用名称或CheckBox的index都可以找到该控件
		solo.clickOnButton("WOW");
		//调用isCheckBoxChecked(int index)方法,判断WOW按钮有没有被选中
		boolean expected = true;
		boolean actual = solo.isCheckBoxChecked(0);
		assertEquals("WOW没有被选中", expected, actual);

		
		for(int i = 0; i < 10; i++){
			solo.clickOnCheckBox(1);
			solo.clickOnCheckBox(1);
			solo.takeScreenshot();
			solo.sleep(2000);
		}
	}
	
	public void test1(){
		//直接调用clickOnButton的名称或clickOnRadioButton的index都可以找到该控件
		solo.clickOnRadioButton(0);
		//调用isRadioButtonChecked(int index)方法,判断boy按钮有没有被选中
		boolean BoyExpected = true;
		boolean BoyActual = solo.isRadioButtonChecked(0);
		assertEquals("boy没被选中", BoyExpected, BoyActual);
		
		solo.clickOnButton("girl");
		boolean GirlExpected = true;
		boolean GirlActual = solo.isRadioButtonChecked(1);
		System.out.println(GirlActual);
		assertEquals("girl没被选中", GirlExpected, GirlActual);
		
		solo.takeScreenshot();
		solo.sleep(2000);
	}

}

点我下载源码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值