混合應(yīng)用是原生APP+webview組成的,可以簡單的理解為一個原生app的外殼,內(nèi)部全是html頁面。在處理這樣的app的定位的時候 需要先定位原生APP上的按鈕或者鏈接,然后點擊按鈕或者鏈接,然后經(jīng)過appium提供的方法,進入webview頁面,通過定位工具和方法進行元素定位(同web自動化元素定位)。
移動端
1.將手機與PC通過USB連接,開啟USB調(diào)試模式;
- 使用360手機助手或在dos窗口輸入adb devices查看手機驅(qū)動連接是否成功;
PC端
1.搭建Appium環(huán)境,并下載SDK
- 在Chrome的應(yīng)用商店下載 ADB Plugin插件并安裝,點擊進入下載 ADB Plugin插件,進入調(diào)試模式;
- 此時頁面顯示了手機型號、驅(qū)動名稱、APP要調(diào)試的WebView名稱;
-
點擊inspect,若成功加載與APP端相同界面的調(diào)試頁面,則配置成功;
image.png

因為Appium是通過 chromedriver-port 9515進行通信,驅(qū)動安卓手機上的WebView;
所以需要查看手機系統(tǒng)的Android System WebView顯示的Chrome版本,下載對應(yīng)的chromedriver并添加到Appium的chromedriver目錄,保證驅(qū)動程序版本對應(yīng)。然后Appium后臺啟動時會自動重啟chromedriver;
例如遇到這類問題:
An unknown server-side error occurred while processing the command. (Original error: unknown error: Chrome version must be >= 43.0.2357.0
解決方案:
1、查看手機的Android System WebView ,打開手機的設(shè)置>應(yīng)用程序管理>全部,注意,一定要在全部里面找。
2、查找到Android System WebView應(yīng)用,查看詳情,顯示版本號:55.0.2883.91,需要下載55~56版本的chromdriver
3、然后找到對應(yīng)的Chromedriver版本進行下載。下載地址:chromedriver各版本下載。
注意:最新版本是:2.37,可通過對應(yīng)版本的目錄下的notes.txt查看。
4、在PC端找到Appium中chromdriver目錄下,替換chromdriver.exe.我的路徑是:D:\software\app tools\nodeV8.7.0Winx64\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win
Appium切換context、切換webview
@Test
public void test1() {
logger.info("開始了------------------------");
Set<String> contexts = driver.getContextHandles();
for(String context:contexts) {
logger.info(context);
//會打印出 NATIVE_APP(原生app handle)和WEBVIEW_com.example.testapp(webview的 handle)
}
//進入webview中
driver.context((String) contexts.toArray()[1]);
WebElement vin = driver.findElement(By.xpath("http://input[@ng-model=\"vehicleInfo.vin\"]"));
vin.clear();
vin.sendKeys("LFV2B21K1D0904712");
}
或者是
public static void getContextHandle(AndroidDriver<WebElement> driver) {
Set<String> context = null ;
for(int i=1;i<=20;i++){
context = driver.getContextHandles();
for(String contextName : context) {
System.out.println(contextName);//打印當(dāng)前上下文
if(contextName!=null && contextName.contains("WEBVIEW_com.quantum.Tmsp7")||contextName.contains("WEBVIEW_com.tencent.mm:tools")){
switchTo_WEBVIEW(driver);
driver.getPageSource();
return;
}
if(i==20) assert false;
}
Log.goSleep(1);
}
}
public static void switchTo_WEBVIEW(AndroidDriver<WebElement> driver) {
String str = driver.currentActivity();//檢查當(dāng)前APP
for(int k=0;k<30;k++){
try {
if(str.equals(".MainActivity")){
driver.context("WEBVIEW_com.quantum.Tmsp7");
return;
}else if(str.equals(".plugin.webview.ui.tools.WebViewUI")){
driver.context("WEBVIEW_com.tencent.mm:tools");
return;
}
} catch (Exception e) {
if(k<10){
logger.info("switch...");
}if(k==30){
logger.fatal(driver, "switch fail!", e);
}
} finally{
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
}
}
為了預(yù)防端口被占用,請關(guān)閉91助手、殺毒軟件等。
(1)啟動appium
(2)運行cmd 輸入 adb devices -l 查看UDID 如圖:
(3)再在cmd中輸入 appium -a 127.0.0.1 -p4723 -Uf4a4d8bb (-a表示ip,-p表示端口,-U表示設(shè)備的udid )
執(zhí)行上述測試類即可。

