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

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

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

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
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的子类):

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()


下面是代码:

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、付费专栏及课程。

余额充值