您的位置:首页 > 其它

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

2014-07-04 14:54 561 查看
在Android中,CheckBox和RadioButton是很常见的控件,那怎样用Robotium对该空间进行测试呢;

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

[plain]
view plaincopyprint?





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


[plain]
view plaincopyprint?





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

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

[plain]
view plaincopyprint?





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

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

下面是代码:

[java]
view plaincopyprint?





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);
}

}

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);
}

}


点我下载源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐