? 使用Robotium時,很多時候通過唯一的ID或text,即可找到對應(yīng)控件,并進行操作。不過,編寫用例較多時,總會遇到過以下類似操作問題:
? ? 1、在界面中,有多個相同的ID怎么辦?
? ? 2、在界面中,有多個相同的text怎么辦?
? ? 3、使用uiautomator,只有唯一的ID或text,但一直操作不成功怎么辦?
? ? 4、在界面中,沒有ID或text怎么辦?
? ? 5、在界面中,webview控件怎么辦?
? ? 6、使用ID或text,無論如何也無法操作某個按鈕怎么辦?
? 對于這類問題,只要解決了點擊控件存在的問題,基本也能解決其他操作的同類問題。所以,下面以點擊操作為例,說明各種情況的處理。
在界面中,有多個相同的ID怎么辦?
? 點擊操作為:solo.clickOnView(solo.getView(sview,第n個id))。即先通過getView()得到對應(yīng)第n個id的view,然后再使用clickOnView點擊控件。
public android.view.View?getView(Stringed,int?index)
? Returns a View matching the specified resource id andindex.
Parameters:
? id - the id of the View to return
? index - the index of the View. 0 if only one is available
Returns:
? ?a View matching the specified id and index
public void?clickOnView(android.view.View?view)
? Clicks the specified View.
Parameters:
? view - the View to click
在界面中,有多個相同的text怎么辦?
? Robotium提供了方法,可以指定操作第幾個匹配的值。
public void?clickOnText(String?text,int?match)
? Clicks a View or WebElement displaying the specified text. Will automatically scroll when needed.
Parameters:
? ?text - the text to click. The parameter will be interpreted as a regular expression
? ?match - if multiple objects match the text, this determines which one to click
使用uiautomator,只有唯一的ID或text,但操作不成功怎么辦?
? 這是由于使用uiautomator能看到ID或text的view,只是可以獲取的所有view的子集;有些控件的ID或text雖然符合要求,但通過uiautomator根本找不到??梢酝ㄟ^getCurrentViews()獲取控件結(jié)果,然后自己分析是否存在包含相同的ID或text的其他控件。
public?ArrayList?getCurrentViews()
? Returns an ArrayList of the Views currently displayed inthe focused Activity or Dialog.
Returns:
? an ArrayList of the View objects currently displayed in the focused window
在界面中,沒有ID或text怎么辦?
? 可以通過找到控件的父節(jié)點或祖父節(jié)點的ID的方式,來獲取對應(yīng)的控件的ID或進行某些點擊操作。如
? ? solo.clickOnView(((ViewGroup)solo.getView(sview)).getChildAt(第n個子節(jié)點));
? 就是先通過父節(jié)點的id,找到子節(jié)點的ID對應(yīng)的view,然后進行點擊操作。
? 或者,實在實現(xiàn)困難的話,可以和開發(fā)人員交流,在代碼中增加對應(yīng)控件的ID,既能幫助測試提高代碼質(zhì)量,也能提高App的可測試性。
在界面中,有webview控件怎么辦?
使用一下webview函數(shù)進行操作。下次分享將詳細講解webview的相關(guān)界面自動化開發(fā)。
clickOnWebElement(By?by)
Clicks? a WebElement matching the specified By object.
clickOnWebElement(By?by, int?match)
Clicks? a WebElement matching the specified By object.
clickOnWebElement(By?by, int?match,? boolean?scroll)
Clicks? a WebElement matching the specified By object.
clickOnWebElement(WebElement?webElement)
Clicks? the specified WebElement.
使用ID或text,無論如何也無法操作某個按鈕怎么辦?
? 如果操作ID或text實在無法解決,可以通過先找到控件的坐標(biāo)范圍,然后點擊對應(yīng)控件的中心坐標(biāo)的方式解決。另外,有些封裝導(dǎo)致的無法獲得ID或text問題,有時可以通過反射,來達到目標(biāo)。
public void?clickOnScreen(float?x,float?y,int?numberOfClicks)
? Clicks the specified coordinates rapidly a specified number of times. Requires API level >= 14.
Parameters:
? x - the x coordinate
? y - the y coordinate
? number Of Clicks - the number of clicks to perform