1.安裝夜神安裝模擬器妻坝、UiAutomator、android sdk、javasdk環(huán)境萌踱;
https://pan.baidu.com/s/1Z70sPJagQG1EDRnVfzaOFA
2.配置環(huán)境變量谋国,啟動(dòng)模擬器槽地、運(yùn)行UiAutomator viewer.bat,在模擬器中安裝測(cè)試應(yīng)用芦瘾;
https://pan.baidu.com/s/1a2Bs1D8MsmDK8eakrUN5CA
也可以使用APPium desktop :https://github.com/appium/appium-desktop
包含inspector 可以來解析定位元素捌蚊;
進(jìn)行配置如下:
{
"deviceName": "Redmi 6",
"platformName": "Android",
"appPackage": "com.china.moa",
"appActivity": "com.ecology.view.WelcomeActivity",
"platformVersion": "8.1.0",
"automationName": "UiAutomator2",
"udid": "99001190304762"
}
手機(jī)開啟開發(fā)者模式,打開USB調(diào)試近弟;在線Xpath驗(yàn)證:https://freeformatter.com/xpath-tester.html
向云手機(jī)圖庫發(fā)送圖片:
1缅糟、上傳圖片,路徑不同手機(jī)有差異:
adb push {file path} /sdcard/DCIM/Camera/{file name}
2祷愉、廣播推至相冊(cè):
adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///sdcard/DCIM/Camera/{file name}修改云手機(jī)定位:
adb -s ip:port shell "echo 'longitude=114.055939:latitude=22.657501' > /data/gps/fifo" #其中ip:port是ADB方式(公網(wǎng))中記錄的ip和port溺拱。
以慈壽寺地鐵為例:
adb -s 127.0.0.1:5504 shell "echo 'longitude=116.2871874100:latitude=39.9328901600' > /data/gps/fifo" |
經(jīng)緯度獲取:http://www.gpsspg.com/maps.htm
- 下載安裝APPIUM推薦官網(wǎng)谣辞;
- appium -g /tmp/run.log #appium log輸出
- android-sdk中的avd manger.exe 可以創(chuàng)建模擬器或使用AndroidStudio創(chuàng)建模擬器迫摔;
adb基礎(chǔ)
app信息
adb shell dumpsys activity top #獲取當(dāng)前界面元素
adb shell dumpsys activity activities #獲取任務(wù)列表
#app入口
adb logcat |grep -i displayed
aapt dump badging mobike.apk | grep launchable-activity
apkanalyzer 最新版本的sdk中才有
#啟動(dòng)應(yīng)用
adb shell am start -W -n com.xueqiu.android/.view.WelcomeActivityAlias -S
$adb devices #設(shè)備可用列表
$adb -s 127.0.0.1:5510 install apk絕對(duì)路徑 #安裝apk
$adb shell dumpsys activity |find "mFocusedActivity" #查看前臺(tái)應(yīng)用包名;
$adb kill-server #終止adb服務(wù)
$adb start-server #啟動(dòng)adb服務(wù)泥从;
$adb pull [手機(jī)路徑] [本地路徑] #將手機(jī)文件拉取到PC句占;
$adb push [本地路徑] [手機(jī)路徑] # 將PC文件放到手機(jī);
$adb shell am start -n 包名/入口 #啟動(dòng)app
$adb shell pm clean 包名 #清除應(yīng)用的數(shù)據(jù)和緩存躯嫉;
$adb shell input tap x y #坐標(biāo)點(diǎn)擊纱烘;
$adb shell pm list packages #列出所有包名 -s 列出系統(tǒng)apk路徑及包名, -3 列出用戶apk路徑以及包名祈餐;
$adb logcat > logcat.log #打印APP日志
$aapt dump badging apk包路徑 ##查看指定apk的相關(guān)信息-找app入口 launchable-activity;
deviceName值的獲壤奚丁:
deviceName=192.168.137.150:5555 ip:手機(jī)ip地址,端口帆阳,通過如下命令開啟
$ adb devices //查看當(dāng)前連接設(shè)備
$ adb tcpip 5555 //開啟5555端口
$ adb connect 192.168.137.150 //連接手機(jī)看是否能連接
$ adb devices //再查看當(dāng)前連接設(shè)備
adb usb #拔取數(shù)據(jù)線后執(zhí)行哺壶,回復(fù)usb調(diào)試模式,重新插線蜒谤;
jar依賴包
<!--Java client for Appium Mobile Webdriver -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
添加配置[Appium Desired Capabilities]
capabilities設(shè)置
? app apk地址
? appPackage 包名
? appActivity Activity名字
? automationName 默認(rèn)使?uiautomator
? noReset fullReset 是否在測(cè)試前后重置相關(guān)環(huán)境
? unicodeKeyBoard resetKeyBoard 是否需要輸??英?
之外的語?并在測(cè)試完成后重置輸?法
#(https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md)
desiredCapabilities.setCapability("deviceName", "06f8794b7d29");
desiredCapabilities.setCapability("platformName", Platform.ANDROID);
desiredCapabilities.setCapability("appPackage", "com.xx.xx");
desiredCapabilities.setCapability("appActivity","com.xx.xx.xx" );
//uiautomator2自動(dòng)化引擎山宾,解決輸入框輸入不了數(shù)據(jù)的問題,
desiredCapabilities.setCapability("automationName","uiautomator2" );
//noReset 不清除應(yīng)用啟動(dòng)數(shù)據(jù)鳍徽;默認(rèn)清理false资锰;
desiredCapabilities.setCapability("noReset",true);
#創(chuàng)建驅(qū)動(dòng)
AndroidDriver<WebElement> androidDriver;
#找到頁面元素并操作頁面元素來模擬用戶操作
androidDriver.findElementByXPath("");
androidDriver.findElementById("");
#通過斷言和日志查看測(cè)試結(jié)果
Assert
元素定位
ID定位 resource-id
將相同ID值的元素放在集合匯總,再去通過集合的索引訪問阶祭;
androidDriver.findElementById(“”);
text定位
androidDriver.fiindElementByAndroidUIAutomator(“new UiSelector().text(\"長(zhǎng)沙\")”);
MobileBy.AndroidUIAutomator("new UiSelector().className(\"android.widget.Button\").textMatches(\".*允許.*\")");
這里使用了原生AndroidUIAutomator绷杜;
XPath定位
xpath定位符
? 絕對(duì)定位: 不推薦
? 相對(duì)定位:
? //*
? //[contains(@resource-id, ‘login’)]
? //[@text=‘登錄’]
? //[contains(@resource-id, ‘login’) and contains(@text, ‘登錄’)]
? //[contains(@text, ‘登錄’) or contains(@label, ‘登錄’)]
? //[contains(@text, '看點(diǎn)')]/ancestor:://[contains(name(), ‘EditText’)]
? //[@clickable="true"]//android.widget.TextView[string-length(@text)>0 and string-length(@text)<20]
?//android.view.View[@content-desc="您的職務(wù)"]/preceding-sibling::android.widget.EditText[1] # preceding-sibling 是找當(dāng)前的前面的兄弟節(jié)點(diǎn);
androidDriver.findeElementByXpath("相對(duì)路徑");
androidDriver.findeElementByXpath("http://android.widget.TextView[@text='我發(fā)起的']");
通過class屬性元素定位的話class在頁面有較多直秆,通常不適用;可以通過結(jié)合Xpath定位鞭盟,80%元素可以使用圾结,APPIUM對(duì)XPath定位有了一定的優(yōu)化性能不用擔(dān)心;
accessibility id定位
在UIAutomator中么有這個(gè)屬性, 對(duì)應(yīng)是content-desc屬性懊缺;
self.driver.find_element(MobileBy.ACCESSIBILITY_ID,'輸入11位手機(jī)號(hào)').click()
坐標(biāo)定位
受屏幕尺寸/分辨率/DPPI影響疫稿,萬不得已不要用培他;
元素等待
元素加載時(shí)間不一致鹃两,會(huì)導(dǎo)致元素?zé)o法定位超時(shí)報(bào)錯(cuò);靈活定制定位元素等待時(shí)間舀凛;
強(qiáng)制等待
固定的等待時(shí)間
Thread.sleep();
隱式等待
針對(duì)全局元素設(shè)置等待時(shí)間
androidDriver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
顯示等待
針對(duì)特定的某個(gè)元素俊扳,不可以對(duì)非元素;
WebDriverWait 與隱式等待不同的是不會(huì)一直等到元素出現(xiàn)猛遍,顯示等待會(huì)在超過設(shè)定時(shí)間后拋出異常馋记。
WebDriverWait webDriverWait = new WebDriverWait(androidDriver, 10);
WebElement webElement = webDriverWait.until(new ExpectedCondition<WebElement>() {
@NullableDecl
public WebElement apply(@NullableDecl WebDriver input) {
return androidDriver.findElementById("com.chinat.moa:id/sdl__negative_button");
}
});
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element =
wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
手勢(shì)操作
? press : TouchAction().press(el0).moveTo(el1).release()
? release
? moveTo
? tap wait
? longPress
? cancel
? perform
手勢(shì)操作-滑動(dòng)
java-client5.0之前提供了滑動(dòng)API,單次滑動(dòng)(下拉刷新)
public void refresh() {
//java-client 4.1.2
//void swipe(int startx, int starty, int endx, int endy, int duration) //duration滑動(dòng)的時(shí)間;
androidDriver.swipe(356,594,356,794,800)
//java-client 6.1.0
TouchAction touchAction = new TouchAction(androidDriver);
//把原始的坐標(biāo)轉(zhuǎn)換成PointOption類型的懊烤;
PointOption startPointOption = PointOption.point(356,594);
PointOption endPointOption = PointOption.point(356,794);
//把原始的時(shí)間轉(zhuǎn)換成Duration類型的梯醒;
Duration duration = Duration.ofMillis(800);
WaitOptions waitOptions = new WaitOptions().withDuration(duration);
touchAction.press(startPointOption).waitAction(waitOptions).press(endPointOption).release();
touchAction.perform();
}
手勢(shì)操作-九宮格解鎖
連續(xù)多次滑動(dòng)(九宮格解鎖)
@Test
public void MultiSwipe() throws InterruptedException{
Thread.sleep(100);
//實(shí)例化TouchAction對(duì)象
TouchAction touchAction = new TouchAction(androidDriver);
//把原始的坐標(biāo)轉(zhuǎn)換成PointOption類型的;
PointOption pointOption1 = PointOption.point(150,427);
PointOption pointOption2 = PointOption.point(362,427);
PointOption pointOption3 = PointOption.point(569,427);
PointOption pointOption4 = PointOption.point(356,625);
PointOption pointOption5 = PointOption.point(356,850);
PointOption pointOption6 = PointOption.point(356,850);
PointOption pointOption7 = PointOption.point(356,850);
//把原始的時(shí)間轉(zhuǎn)換成Duration類型的腌紧;
Duration duration = Duration.ofMillis(800);
WaitOptions waitOptions = new WaitOptions().withDuration(duration);
touchAction.press(pointOption1).moveTo(pointOption2).moveTo(pointOption3).moveTo(pointOption4).moveTo(pointOption5).moveTo(pointOption6).moveTo(pointOption7).release();
touchAction.perform();
}
手勢(shì)操作-多點(diǎn)觸摸
MultiTouchAction類可以模擬用戶多點(diǎn)觸摸操作茸习;
主要包含add()/perform()兩個(gè)方法;
可以結(jié)合TouchAction類模擬多根手指的滑動(dòng)效果壁肋;
原理介紹:
B->A同時(shí)C->D是放大效果号胚,反之是縮小浸遗;
@Test
public void testMultiTouch() throws InterruptedException {
Thread.sleep(6000);
//1.實(shí)例化MultiTouchAction對(duì)象
MultiTouchAction multiTouchAction = new MultiTouchAction(androidDriver);
//2.實(shí)例化兩個(gè)TouchAction
TouchAction touchAction1 = new TouchAction<>(androidDriver);
TouchAction touchAction2 = new TouchAction<>(androidDriver);
//獲得當(dāng)前屏幕的高寬猫胁;
int x = androidDriver.manage().window().getSize().getWidth();
int y = androidDriver.manage().window().getSize().getHeight();
//第一根手指的動(dòng)作從B點(diǎn)滑動(dòng)到A點(diǎn);
touchAction1.press(PointOption.point(x * 4 / 10, y * 4 / 10)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(100)))
.moveTo(PointOption.point(x * 2 / 10, y * 2 / 10)).release();
//第一根手指的動(dòng)作從B點(diǎn)滑動(dòng)到A點(diǎn)跛锌;
touchAction2.press(PointOption.point(x * 6 / 10, y * 6 / 10)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(100)))
.moveTo(PointOption.point(x * 8 / 10, y * 8 / 10)).release();
//添加觸摸動(dòng)作到MultiTouchAction
multiTouchAction.add(touchAction1).add(touchAction2);
multiTouchAction.perform();
}
APPIUM常用API
1.startActivity 實(shí)現(xiàn)頁面跳轉(zhuǎn)(包括APP內(nèi)部頁面及APP相互跳轉(zhuǎn))
//開啟某一個(gè)activity實(shí)現(xiàn)跳轉(zhuǎn)弃秆;
//首先我們創(chuàng)建activitiy對(duì)象,用Activity構(gòu)建方法初始化髓帽,參數(shù)為對(duì)應(yīng)的包名和類名驾茴;
//1.app內(nèi)部跳轉(zhuǎn);
Activity activity = new Activity("com.chinatower.moa", "com.ecology.view.MainActivity");
androidDriver.startActivity(activity);
//2.app相互跳轉(zhuǎn),必須要是跳轉(zhuǎn)app的啟動(dòng)入口氢卡;
Activity activityApp = new Activity("com.android.browser", "com.android.view.browser.BrowserView");
androidDriver.startActivity(activity);
2.getPageSource 得到當(dāng)前頁面的dom結(jié)構(gòu)锈至;
可以用于斷言當(dāng)前頁面是否有某個(gè)元素,或者判斷當(dāng)前頁面有沒有產(chǎn)生變化:如上下滾動(dòng)判斷是否已經(jīng)到了底端/頂端译秦;
String pageSource = androidDriver.getPageSource();
System.out.println(pageSource);
3.currentActivity() 獲得當(dāng)前頁的類名峡捡;
String actual = androidDriver.currentActivity();
4.resetApp重置應(yīng)用的數(shù)據(jù)击碗;
有些場(chǎng)景需要清除應(yīng)用的數(shù)據(jù),相當(dāng)于第一次安裝時(shí)候的狀態(tài)们拙,比如第一次啟動(dòng)APP的引導(dǎo)頁稍途、登錄等;
//重置應(yīng)用數(shù)據(jù)砚婆;
androidDriver.resetApp();
5.isAppInstalled判斷App是否安裝械拍;
//獲取到應(yīng)用是否安裝
androidDriver.isAppInstalled("com.android.browser");
6.pressKey 安卓平臺(tái)獨(dú)有,向系統(tǒng)發(fā)送鍵值事件装盯,不同的鍵值對(duì)應(yīng)不同的功能坷虑,如keyevent(4)標(biāo)識(shí)手機(jī)的HOME按鍵;
//pressKey
KeyEvent keyEvent = new KeyEvent();
keyEvent.withKey(AndroidKey.HOME);
androidDriver.pressKey(keyEvent);
7.getScreenshotAs截圖功能埂奈,當(dāng)測(cè)試用例執(zhí)行失敗之后進(jìn)行屏幕截圖迄损,保存到本地為了更好的查找問題;
//getScreenshotAs截圖功能
File file = androidDriver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file,new File("D:\\test.png" ));
8.getDeviceTime獲取設(shè)備當(dāng)前時(shí)間
//獲取設(shè)備當(dāng)前時(shí)間
androidDriver.getDeviceTime();
- getDisplayDensity獲取設(shè)備DPI,不是分辨率
//獲取設(shè)備DPI
androidDriver.getDisplayDensity();
10.getAutomationName獲取當(dāng)前自動(dòng)化引擎
//獲取當(dāng)前自動(dòng)化引擎
androidDriver.getAutomationName();
11.getOrientation獲取設(shè)備的橫豎屏狀態(tài)账磺;
//獲取設(shè)備的橫豎屏狀態(tài)芹敌;
androidDriver.getOrientation();
Toast元素獲取
- 獲取要求:Java-client 5.0+;使用UIAntomator2自動(dòng)化引起垮抗;Android系統(tǒng)版本5.0+氏捞;
必須使?xpath查找
//*[@class='android.widget.Toast']
//*[contains(@text, "xxxxx")]
#獲取方式
By.xpath(“//*[contains(@text,'toast部分信息‘)]”)
常?功能
https://github.com/appium/appium/blob/master/docs/en/
writing-running-appium/appium-bindings.md
? 系統(tǒng)操作
? lock background hideKeyBoard openNotifications shake
? startActivity currentActivity getCurrentPackage
? app操作
? installApp removeApp isInstalled closeApp launchApp reset
getAppStrings
? getContextHandles getContext context
Hybrid自動(dòng)化準(zhǔn)備 俗稱H5
如何區(qū)分H5和原生頁面,定位中類為webview的是H5冒版,打開開發(fā)者調(diào)試-UI布局原生頁面會(huì)有框框液茎;
Hybrid自動(dòng)化準(zhǔn)備
Appium提供的解決方案基于UIAutomator + ChromeDriver;
準(zhǔn)備:
- 準(zhǔn)備android4.4+版本以上的手機(jī)壤玫、模擬器豁护;
- 在app源碼中將webview調(diào)試模式打開;
- webview.setWebContentsDebuggingEnabled(true);
- 安裝UC開發(fā)者工具欲间;uc-devtools工具
設(shè)置為本地資源楚里,鏈接上手機(jī)活模擬器后,打開一個(gè)包含H5頁面的APP猎贴,HOME中即可有inspect檢測(cè)到班缎;
image.png
image.png
5.開啟手機(jī)開發(fā)者模式-打開布局,若是原生頁面會(huì)有很多框她渴;
原生頁面:image.png
H5頁面:image.png
線上App開啟WebView調(diào)試(root)
如果是第三方線上APP达址,一般webview debug開關(guān)都是關(guān)閉的,這個(gè)就需要借助第三方工具才能打開趁耗;
解決方案:
Xposed+WebviewDebugHook
Xposed是一個(gè)框架沉唠,能夠集成很多功能模塊,這些模塊能夠在不修改APK的情況下苛败,修改APP的運(yùn)行方式满葛,將Xposed安裝到手機(jī)径簿,下載對(duì)應(yīng)的x86活arm框架;
然后安裝WebviewDebugHook嘀韧,勾選Xposed模塊中WebviewDebugHook激活篇亭,通過模塊來開啟APP的WebView debug模式;
上下文
- 獲取所有的contexts
driver.getContextHandles(); - 切換到webview視圖
driver.context(webview視圖) - 定位webview中的元素锄贷,并執(zhí)行操作
web網(wǎng)頁元素定位和操作 - 切換回默認(rèn)的視圖
driver.context(nativer視圖) -
示例
image.png
- 常見錯(cuò)誤chromedriver和Chrome 版本不符译蒂,下載對(duì)應(yīng)的版本chromedriver 替換appium的目錄下的即可;
-
報(bào)錯(cuò)截圖:
image.png -
可在git查詢chromedriver和Chrome 版本谊却,淘寶查詢支持版本柔昼;
image.png
-
替換appium目錄下的chromedriver:
image.png
非root設(shè)備開啟線上APP的Webview調(diào)試
安卓VitualXposed+WebviewDebugHook后,選擇對(duì)應(yīng)應(yīng)用因惭;
adb install C:\Users\Tower\Documents\VirtualXposed_AOSP_0.17.3.apk
adb install C:\Users\Tower\Documents\WebViewDebugHook.apk
APPIUM自動(dòng)化原理解析
Appium Android?動(dòng)化流程分析
appium -g <log file path>
- Appium Log
? 清晰記錄了所有的請(qǐng)求和結(jié)果 - getPageSource
? 界?的完整dom結(jié)構(gòu). xml?件 - 腳本內(nèi)調(diào)試
? 利?xpath獲取所有匹配的元素
? driver.findElementsByXPath(“//*")