robotium 使用方法1

① 点击:
clickOnButton(int) //Clicks on a Button with a given index.
clickOnButton(String) //Clicks on a Button with a given text.
clickOnCheckBox(int) //Clicks on a CheckBox with a given index.
clickOnView(View) //Clicks on a given View.
clickOnText(String) //Clicks on a View displaying a given text.
clickLongOnText(String) //Long clicks on a given View.
clickOnRadioButton(int) //Clicks on a RadioButton with a given index.
clickOnScreen(float, float) //Clicks on a given coordinate(坐标) on the screen.点击屏幕上的某个坐标点
② 取得:
getCurrentActivity() //Returns the current Activity.获得当前的activity
getText(String) //Returns a TextView which shows a given text.
getView(int) //Returns a View with a given id.
getEditText(String) //Returns an EditText which shows a given text. 
getImage(int) //Returns an ImageView with a given index.
③ 拖拽:
drag(float fromX, float toX, float fromY, float toY, int stepCount) //Simulate touching a given location and dragging it to a new location.
from X : 屏幕坐标的初始X坐标
to X : 屏幕坐标拖拽目的地的X坐标
from Y : 屏幕坐标的初始Y坐标
to Y : 屏幕坐标拖拽目的地的Y坐标
stepCount: 完成拖拽需要的步骤数
④ 搜索:
searchText(String) //Searches for a text string and returns true if at least one item is found with the expected text.
searchEditText(String) //Searches for a text string in the EditText objects located in the current Activity.
searchButton(String, boolean) //Searches for a Button with the given text string and returns true if at least one Button is found.


clickOnMenuItem(String);
功能是点击Menu按钮,选择文本描述为String的菜单,如我们的例子是"Add note"


assertCurrentActivity(String message,String name);
这个是判断当前的activity是否和我预期的一致
message是描述性的文字
name是指activity的名字
关于如何知道activity 名字,目前的方法是得看源码中的 AndroidManifest.xml--Application label--Application Nodes,在那里我们可以看到所有的activity的name


enterText(int index,string text);
index用来标识写到哪个EditText中。如果当前只打开一个EditText,那index=0
text:就是我们要写入的内容


goBack();
相当于手机上的 返回键(back key)


goBackToActivity(String name);
返回到指定的activity


searchText(String text)
在当前的activity中搜索是否含有text的内容


clickInList(int index);
点击list表的第index行,进入该文本界面


solo.setActivityOrientation(Solo.LANDSCAPE);
setActivityOrientation,设置手机屏幕显示方式
LANDSCAPE:横向显示
Portrait:竖向显示


clickOnText(String text);
点击包含该文字的地方
其中text可以用正则表达式表示
(?i)----忽略大小写。默认情况是大小写敏感的。
正则表达式与java保持一致


clickLongOnText(String text);
长时间按住所选的文字


通过id获取、操作view
/* 通过ID点击打开VIEW
* s:Id(hierarchviewer)触摸按键的id,字符串格式 
*/ 
int ctrl; 
View v; 
ctrl = solo.getCurrentActivity().getResources().getIdentifier(s,"id","com.leju.platform"); 
v = solo.getView(ctrl); 
solo.clickOnView(v);


solo.getCurrentActivity().getResources().getIdentifier(s,"id","com.leju.platform")这个方法的意思主要是获取资源ID,getIdentifier(s,"id","com.leju.platform")中第一个参数是ID名,第二个参数是资源属性ID或者是Drawable,第三个参数是包名。


solo.typeText和solo.enterText方法都可以对EditeText进行测试,实现过程存在几点不同:
1,字面上:type是按键输入;enter就是回车,把那你输入后的东西键入进去
2、实现上:typeText方法是robotium框架调用系统Instrumentation类里面的sendStringSync方法来实现的;enterText是调用TextView里面setText方法来实现的。
3、显示上:typeText在测试过程中能看到输入的痕迹;enterText则没有输入痕迹。
(网友理解:typeText方法更接近用户真实操作,但在真机操作时容易弹出软键盘)

clickOnScreen 拥有两个参数

作用:点击屏幕上的某个坐标点

public void clickOnScreen(float x,float y)

x : 屏幕的x坐标

y : 屏幕的y坐标 


drag 拥有四个参数

作用:拖拽屏幕从A坐标,到B坐标

public void drag(float fromX, float toX, float fromY, float toY, int stepCount)

from X : 屏幕坐标的初始X坐标

to X   : 屏幕坐标拖拽目的地的X坐标

from Y : 屏幕坐标的初始Y坐标

to Y   : 屏幕坐标拖拽目的地的Y坐标

stepCount: 完成拖拽需要的步骤数

这个API中用到了一个叫坐标的东西,当然Android 模拟器的坐标是固定的,你可以凭借经验,来得到屏幕坐标位置,但这是不靠谱的...

这里介绍一个工具:monkeyrunner

monkeyrunner 的录制模式可以帮你获取到Android屏幕坐标

关于 monkeyrunner 可以参考:http://luwenjie.blog.51cto.com/925779/925659

setProgressBar

作用:设置得到的 ProgressBar 进度,例:Seekbar 和 RatinBar

public void setProgressBar(int index, int progress)

int index: ProgressBar 控件定位

int progress: ProgressBar 应该设置的进度位置

 
 
  1. public void Drag(){ 
  2.     boolean expected = true
  3.     solo.setProgressBar(065); 
  4.     //      solo.clickOnScreen(121, 80);     
  5.     //      solo.drag(11, 121, 80, 80, 3); 
  6.     boolean actual = solo.searchText("当前<拖动条>值为:65"); 
  7.     assertEquals("This is not found",expected,actual); 

注:使用setProgressBar后,依旧保持着按下状态。

clickLongOnScreen

作用:长时间按住屏幕的某坐标点

Public void clickLongScreen(float x, float y, int time)

float x : x 坐标

float y : y 坐标

int time: 长按时间



删除(清空)输入框内保存的上次输入字符:
solo.clearEditText(EditText editText)

solo.scrollToSide(KeyEvent.KEYCODE_DPAD_LEFT);//向左滑屏
 solo.scrollToSide(KeyEvent.KEYCODE_DPAD_RIGHT);//向右滑屏


注意:被测apk和测试apk必须使用相同的签名。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值