基於Appium+java進行Android自動化測試:列表定位、用例失敗截圖、長按、滑動、依賴、切換~
一、appium中對元素的定位方法的使用
場景:元素的定位,常利用resource-id、index、name(text)等屬性進行定位。如下圖所示,列表專案的index、resource-id、class、package等屬性均相同,無法利用常用屬性進行區分。直接根據resource-id、index等屬性,由於列表中全部相同,預設自動選擇的是第一個元素。故對元素定位進行了調研,可以通過此類方法獲取當前頁面的任意一個元素;
方法:用通用屬性獲取所有該型別的當前頁面的某個目標元素,示例:
(1)獲取text為“備註”的所有元素
List bookMarkList = findList(“備註”);
(2)獲取當前列表頁面中所有專案數:
int itemNum = bookMarkList.size();
(3)獲取當前列表中第一個元素
WebEelement targetEle = bookMarkList.get(0);
示例
/*resource-id、text、index無法區分時獲取列表,使用的時候,呼叫此方法即可*/
publicstaticList<WebElement> findList(String name){
List<WebElement> BookMarkList = elements(By.name(name));
returnBookMarkList;
}
publicstaticList<WebElement> findListByid(String id){
List<WebElement> BookMarkList = elements(By.id(id));
returnBookMarkList;
}
獲取列表方法呼叫(From Appium demo source code,所有TestCase的基類)
/**
* Return a list of elements by locator *
*/
publicstaticList<WebElement> elements(By locator){
returndriver.findElements(locator);
}
二、用例失敗截圖
場景:我們進行自動化測試過程中,需要對一些用例結果進行截圖或者對錯誤的case進行記錄,可以通過截圖來分析失敗或者異常原因,儲存場景。
//呼叫方法
getScreenshotAs ();
//截圖並儲存至本地
publicstaticvoidtakeScreenShots(AndroidDriver driver)
{
File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(screenShotFile, newFile( "D:AutoScreenCapture' + System.currentTimeMillis() +'.jpg"));
}
catch(IOException e) {e.printStackTrace();}
}
注:靜態方法System.currentTimeMillis()返回1970-01-01 00:00:00.000到現在的毫秒數,返回值是一個long型;
三、依賴測試
場景:如果測試用例間有依賴性,也就是如果想執行B,就必須要先執行A。可以利用TestNG中依賴測試方法解決。如自動化測試有些測試用例都依賴於同類型的操作,作為基礎條件。地圖自動化測試中,收藏模組很多新增收藏的,調研後利用方法的依賴。
依賴規則:
method2()依賴於method1()
If method1() is passed, method2() will be executed.
If method1() is failed, method2() will be skipped.
示例:
@Test
publicvoidmethod1()
{
BookMarkEditPage bookMarkPageEdit = newBookMarkEditPage();
bookMarkPageEdit.addPoiToBookMark( "清華東路西口");
Assert.assertTrue(bookMarkPageEdit.verifyAddPoiToBookMark( "清華東路西口"), "新增poi點失敗");
System.out.println( "This is method 1");
}
@Test(dependsOnMethods = { "method1"})
publicvoidmethod2(){
BookMarkCommonPage bookMarkCommonPage = newBookMarkCommonPage();
bookMarkCommonPage.deleteAllItems(); Assert.assertTrue(bookMarkCommonPage.isDisplayed( "你還沒有新增收藏"), "刪除失敗!");
System.out.println( "This is method 2");
四、Android–模擬長按操作
場景:自動化測試過程中需要模擬使用者進行點選、長按、滑動等不同操作。
方法:對元素進行定位,TouchAction,然後設定一個等待時間,進行模擬。
publicstaticAndroidDriver driver;
/*長按操作:waitAction的引數單位是ms*/
publicstaticvoidlongClick(String id){
WebElement longClick = driver.findElement(By.id(id));
newTouchAction(driver).longPress(longClick).waitAction( 1000).perform();
}
/*TouchAction.class中定義*/
publicTouchAction waitAction(intms){
TouchAction.ActionParameter action = newTouchAction.ActionParameter( "wait");
action.addParameter( "ms", Integer.valueOf(ms));
this.parameterBuilder.add(action);
returnthis;
}
五、模擬上下、左右滑動操作
場景:專案自動化測試中,有滑動底圖、滑動列表的場景需求。
方法:獲取當前螢幕的size,設定滑動的起終點座標,呼叫swip()方法。
/**
* 滑動操作,可以用來模擬左右,上下滑動操作
* @paramstartX 起點的x座標
* @paramstartY 起點的y座標
* @paramendX 終點的x座標
* @paramendY 終點的y座標
* @paramduration 單位是秒
*/
publicstaticvoidswipe(intstartX, intstartY, intendX, intendY, intduration){
driver.swipe(startX, startY, endX, endY, duration* 1000);
}
/**
* 獲取手機螢幕的size
* @return
*/
publicstaticDimension getScreenSize(){
returndriver.manage().window().getSize();
}
//向上滑動
publicstaticvoidswipeToUp(intduration){
intwidth = driver.manage().window().getSize().width;
intheight = driver.manage().window().getSize().height;
driver.swipe(width/ 2, height/ 2, width/ 2, height/ 4, duration* 1000);
}
//向下滑動
publicstaticvoidswipeToDown(intduration){
intwidth = driver.manage().window().getSize().width;
intheight = driver.manage().window().getSize().height;
driver.swipe(width/ 2, height/ 4, width/ 2, height/ 2, duration* 1000);
}
//向左滑動
publicstaticvoidswipeToLeft(intduration){
intwidth = driver.manage().window().getSize().width;
intheight = driver.manage().window().getSize().height;
driver.swipe(width* 3/ 4, height/ 2, width/ 4, height/ 2, duration* 1000);
}
//向右滑動
publicstaticvoidswipeToRight(intduration){
intwidth = driver.manage().window().getSize().width;
intheight = driver.manage().window().getSize().height;
driver.swipe(width/ 4, height/ 2, width* 3/ 4, height/ 2, duration* 1000);
}
六、native app、webview切換
場景:專案測試中,有app、web頁切換的場景需求,比如搜尋結果餐飲團購詳情頁、第三方微博登入頁等。
方法:可以通過切換context實現Native app 和Webview切換。
/**
* 從native app切換到webview
* 注意:Appium官網有提到,4.3以上才支援在android模式下切換到webview,如果是之前的版本, 需要使用selendroid模式
* @return返回是否支援切換到webview
*/
publicstaticbooleanswitchToWebView(){
Set<String> contextNames = driver.getContextHandles();
System.out.println(contextNames.toArray());
for(String contextName : contextNames) {
if(contextName.contains( "WEBVIEW_com.sogou.map.android.maps")) {
driver.context( "WEBVIEW_com.sogou.map.android.maps");
returntrue;
}
}
returnfalse;
}
/*從webview切換到native app*/
publicstaticvoidswitchToNativeApp(){
Set<String> contextNames = driver.getContextHandles();
driver.context( "NATIVE_APP");
}
/*通過xpath來查詢元素,用於WebView內的測試*/
publicstaticWebElement findByXpathInWebView(String attrName){
//return driver.findElement(By.xpath("//a[@log='" + attrName + "'] | //div[@log='" + attrName + "']"));
returndriver.findElement(By.xpath( "//*[@log='"+ attrName + "']"));
}