Robotium 测试方法

时间:2023-03-09 16:48:05
Robotium 测试方法

1.检查CheckBox 是否选上,用solo.isCheckBoxChecked( “text” )。

  有时候checkBox 没有相关的text,这时要用solo.isCheckBoxChecked(index) 。 
2. 如果页面上相同的string有多个,可以用index来区分。

  如solo.clickOnText(text,index),第一次匹配index=1,第N次匹配index=n。
3.发送sendkey命令时,用sendKeys (String keysSequence)可以指定发送动作重复的次数,唯一需要注意的就是这里面KeysSequence不用想普通命令时写上 KeyEvent.KEYCODE_这些,直接写sendKeys(i*KEYEVENT),比如sendKeys(256*DEL)就是重复256次删除动作

4. InstrumentationTestCase可以用来模拟触摸屏和按键的处理,为了避免误操作,在测需要触摸和按键的case前最好关闭模拟器或设备的触摸功能:setActivityInitialTouchMode(false);

5. 触摸和按键可以通过TouchUtils.clickView(this, button/view);来实现

6、断言  assert:

assertEquals(“string”,expect,actual):判断实际值与期待值是否相等,相等判定为真,否则为false,并报错string。

8.EditText 处理

用solo.enterText(0,"text"), 有时会发生无法输入string的现象。

 EditText fNameInputField = solo.getEditTextWithHint("string")

 assertNotNull (“string of error hint”,fNameInputField);

 solo.enterText (fNameInputField, "your string")

 //getEditTextWithHint的函数定义:

  for (EdutText view :getCurrentEditText())

 {
CharSequence target=view.getHint(); Pattern pattern=Pattern.compile(hintRegex); Matcher matcher=pattern.matcher(target); if (matcher.find()) {
return view;
}
}

10.View 处理:

ArrayList<View> viewList =getCurrentViews();

index=viewList.indexOf(view);//当前view对应的index

如果要得到其他View  viewList.get(index+othernumber)

11.instrumentation 对key的处理  private instrumentation instru;instru.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK)

solo实例的创建方法 helloinstrumentation=getInstrumentation   solo =new Solo(helloinstrumentation)

12.屏幕上拉至顶

 ArrayList<Listview> listviews =solo.getCurrentListViews();
if (listviews.size()>0) { TouchUtils.scrollToTop(this,getActivity,listviews.get(0));
}
assertFalse (solo.searchText(subject));

13.点击下拉框

 ArrayList<Spinner> spinner=solo.getCurrentSpinners();
solo.clickOnView(spinners.get(index));