? 使用Robotium時(shí)笋妥,很多時(shí)候通過(guò)唯一的ID或text,即可找到對(duì)應(yīng)控件窄潭,并進(jìn)行操作春宣。不過(guò),編寫(xiě)用例較多時(shí)嫉你,總會(huì)遇到過(guò)以下類似操作問(wèn)題:
? ? 1月帝、在界面中,有多個(gè)相同的ID怎么辦幽污?
? ? 2嚷辅、在界面中,有多個(gè)相同的text怎么辦距误?
? ? 3簸搞、使用uiautomator,只有唯一的ID或text准潭,但一直操作不成功怎么辦趁俊?
? ? 4、在界面中刑然,沒(méi)有ID或text怎么辦寺擂?
? ? 5、在界面中泼掠,webview控件怎么辦怔软?
? ? 6、使用ID或text武鲁,無(wú)論如何也無(wú)法操作某個(gè)按鈕怎么辦爽雄?
? 對(duì)于這類問(wèn)題,只要解決了點(diǎn)擊控件存在的問(wèn)題沐鼠,基本也能解決其他操作的同類問(wèn)題挚瘟。所以,下面以點(diǎn)擊操作為例饲梭,說(shuō)明各種情況的處理乘盖。
在界面中,有多個(gè)相同的ID怎么辦憔涉?
? 點(diǎn)擊操作為:solo.clickOnView(solo.getView(sview,第n個(gè)id))订框。即先通過(guò)getView()得到對(duì)應(yīng)第n個(gè)id的view,然后再使用clickOnView點(diǎn)擊控件兜叨。
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
在界面中穿扳,有多個(gè)相同的text怎么辦衩侥?
? Robotium提供了方法,可以指定操作第幾個(gè)匹配的值矛物。
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雖然符合要求忆首,但通過(guò)uiautomator根本找不到爱榔。可以通過(guò)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
在界面中,沒(méi)有ID或text怎么辦丁鹉?
? 可以通過(guò)找到控件的父節(jié)點(diǎn)或祖父節(jié)點(diǎn)的ID的方式妒潭,來(lái)獲取對(duì)應(yīng)的控件的ID或進(jìn)行某些點(diǎn)擊操作。如
? ? solo.clickOnView(((ViewGroup)solo.getView(sview)).getChildAt(第n個(gè)子節(jié)點(diǎn)));
? 就是先通過(guò)父節(jié)點(diǎn)的id揣钦,找到子節(jié)點(diǎn)的ID對(duì)應(yīng)的view雳灾,然后進(jìn)行點(diǎn)擊操作。
? 或者冯凹,實(shí)在實(shí)現(xiàn)困難的話谎亩,可以和開(kāi)發(fā)人員交流,在代碼中增加對(duì)應(yīng)控件的ID宇姚,既能幫助測(cè)試提高代碼質(zhì)量匈庭,也能提高App的可測(cè)試性。
在界面中浑劳,有webview控件怎么辦阱持?
使用一下webview函數(shù)進(jìn)行操作。下次分享將詳細(xì)講解webview的相關(guān)界面自動(dòng)化開(kāi)發(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衷咽,無(wú)論如何也無(wú)法操作某個(gè)按鈕怎么辦?
? 如果操作ID或text實(shí)在無(wú)法解決蒜绽,可以通過(guò)先找到控件的坐標(biāo)范圍镶骗,然后點(diǎn)擊對(duì)應(yīng)控件的中心坐標(biāo)的方式解決。另外躲雅,有些封裝導(dǎo)致的無(wú)法獲得ID或text問(wèn)題鼎姊,有時(shí)可以通過(guò)反射,來(lái)達(dá)到目標(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