Problem to solve: Appium When testing Android hybrid app, the second time switching to WebView failed
Cause Analysis: When testing Android hybrid applications with Appium, automated tests can be performed normally when the program switches to WebView for the first time. However, when the program switches to WebView for the second time, Appium will automatically find the HTML page that was opened for the first time, so appium cannot locate the element in the HTML page we opened the second time.
Appium The first time you switch to an HTML page, it will be reborn as a chromedriver; when you switch to HTML for the second time, the existing chromedriver is used. But in fact, in our application every time we open an activity, we usually recreate a webchromeclient, so we'll change it to create a new chromedriver anyway.
Resolution steps: Modify Appium source code
Appium files under the installation directory
Appium\node_modules\appium\lib\devices\android\android-hybrid.js, there is a function in the file:
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 h Ave A Chromedriver instance running ")); } if (thiscontext]) {//in the case where we ' ve a Lready set up a chromedriver for a context, //we want to reconnect to it, not create a W Hole New One this.setupexistingchromedriver (context, CB); } else {thiscontext, CB); }};
Switch
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 recon Nect to it, not create a whole new one //this.setupexistingchromedriver (context, CB); //} else {//this.setupnewchromedriver (context, CB); //} this.setupnewchromedriver (context, CB); };
"pro-Test" Appium test Android Hybrid app, the second switch to WebView failed