轉(zhuǎn)~
要解決的問(wèn)題:Appium測(cè)試Android混合應(yīng)用時(shí)苦锨,第二次切換到WebView時(shí)失敗
原因分析:在用Appium測(cè)試Android混合應(yīng)用時(shí)廉嚼,當(dāng)程序第一次切換到WebView時(shí)矗烛,可以正常進(jìn)行自動(dòng)化測(cè)試辅柴。可是當(dāng)程序第二次切換到WebView時(shí)瞭吃,Appium會(huì)自動(dòng)找到到第一次打開(kāi)的Html頁(yè)面碌嘀,那么這時(shí)Appium就無(wú)法定位我們第二次打開(kāi)的Html頁(yè)面中的元素。
Appium第一次切換到Html頁(yè)面時(shí)歪架,會(huì)新生成一個(gè)Chromedriver股冗;當(dāng)?shù)诙吻袚Q到Html時(shí),會(huì)使用已經(jīng)存在的Chromedriver牡拇。但其實(shí)在我們的應(yīng)用里面每次打開(kāi)一個(gè)Activity時(shí)一般都是會(huì)重新創(chuàng)建一個(gè)WebChromeClient魁瞪,所以這里就把它改成無(wú)論如何都生成一個(gè)新的Chromedriver穆律。
解決步驟:修改Appium源碼
Appium安裝目錄下的文件
Appium\node_modules\appium\lib\devices\android\android-hybrid.js,文件中有這樣一個(gè)函數(shù):
androidHybrid.startChromedriverProxy = function (context, cb) {
cb = _.once(cb);
logger.debug("Connecting to chrome-backed webview");
if (this.chromedriver !== null) {
return cb(new Error("We already have a chromedriver instance running"));
}
if (this.sessionChromedrivers[context]) {
// in the case where we've already set up a chromedriver for a context,
// we want to reconnect to it, not create a whole new one
this.setupExistingChromedriver(context, cb);
} else {
this.setupNewChromedriver(context, cb);
}
};
改為:
androidHybrid.startChromedriverProxy = function (context, cb) {
cb = _.once(cb);
logger.debug("Connecting to chrome-backed webview");
if (this.chromedriver !== null) {
return cb(new Error("We already have a chromedriver instance running"));
}
// if (this.sessionChromedrivers[context]) {
// // in the case where we've already set up a chromedriver for a context,
// // we want to reconnect to it, not create a whole new one
// this.setupExistingChromedriver(context, cb);
// } else {
// this.setupNewChromedriver(context, cb);
// }
this.setupNewChromedriver(context, cb);
};