① 點擊:
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;
}