① 点击:
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.
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, float, float, float, int)—Simulate touching a given location and dragging it to a new location.
④ 搜索:
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:就是我们要写入的内容
④其他
solo.setActivityOrientation(0);设置为横屏,参数为“1”是竖屏
5、点击自动化
clickOnMenuItem("菜单名")
clickInList(列表行数) 注:从1开始
clickOnText("(?i).*?test.*") 点击文本
clickLongOnText("Note 2") 长时间点击文本
clickOnButton("按钮名") 点击按钮
6、输入自动化
enterText(号,"输入的内容")
7、屏幕控制
setActivityOrientation(Solo.LANDSCAPE或Solo.PORTRAIT) 控制屏幕横向或纵向显示
solo.scrollViewToSide(view, solo.LEFT);左滑动
8、跳转
goBack() 模仿硬返回键
goBackToActivity("Activity名") 跳到指定的Activity
9、判断
判断当前是否是指定的Activity
assertCurrentActivity("测试提示", "Activity名");
搜索指定文本是否存在
searchText("搜索文本")或searchText("(?i).*?note 1 test") 后面这个是正则表达式
assertTrue("导航页没有找到Innjoo", solo.searchText("Innjoo"));
10、获取
(EditText) solo.getView(R.id.EditText01);
(TextView) solo.getView(R.id.TextView01);
ArrayList currentTextViews = solo.getCurrentTextViews(outputField);
11、点击按钮等测试中需要注意2点:
(1)真机测试时发现,屏保后点击按钮测试会报找不到该按钮,也就是点不中的意思,看来测试机器人还真仿真啊。
(2)点击按钮后有个延迟的过程,以后的测试需要循环等待一段时间,否则直接进入下面的测试后误报错错误,此处处理示例如下:
// 点击按钮开启服务
solo.clickOnButton(butStartService);
// 判断指定服务是否存在
long start = System.currentTimeMillis();
while (!isServiceStarted(SERVICE_PACKAGE_NAME)) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
if ((System.currentTimeMillis() - start) > TIMEOUT) {
break;
}
}
assertTrue("没有开启服务", isServiceStarted(SERVICE_PACKAGE_NAME));
12.
private Map jdField_b_of_type_JavaUtilMap = new HashMap();
public View findViewById(String paramString) {
try {
int i;
if (this.jdField_b_of_type_JavaUtilMap.containsKey(paramString))
i = ((Integer) this.jdField_b_of_type_JavaUtilMap
.get(paramString)).intValue();
else
i = solo.getCurrentActivity()
.getResources()
.getIdentifier(paramString.replace(".R.id.", ":id/"),
null, null);
if (i > 0) {
this.jdField_b_of_type_JavaUtilMap.put(paramString,
Integer.valueOf(i));
View localView1 = solo.getView(i);
if (localView1 != null)
return localView1;
ArrayList localArrayList = solo.getViews();
Iterator localIterator = localArrayList.iterator();
while (localIterator.hasNext()) {
View localView2 = (View) localIterator.next();
if (localView2.getId() == i)
return localView2;
}
} else {
return null;
}
} catch (Exception e) {
}
return null;
}