本文主要是介绍雾山的Robotium学习笔记---CheckBox,RadioGroupRadioButton的测试方法及结果判定,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在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);}@Overrideprotected void setUp() throws Exception {solo = new Solo(getInstrumentation(), getActivity());}@Overrideprotected void tearDown() throws Exception {solo.finishOpenedActivities();}@Testpublic 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);}}
点我下载源码
这篇关于雾山的Robotium学习笔记---CheckBox,RadioGroupRadioButton的测试方法及结果判定的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!