appium 二次切换webview后无法找到页面元素

本文解决Appium在Android自动化测试中,二次切换到WebView后无法定位元素的问题。原因为Chromedriver实例未重新生成。通过修改android-hybrid.js文件中的startChromedriverProxy函数,强制每次切换WebView时都创建新的Chromedriver实例。

问题:在使用Appium进行android自动化测试时,第一次切换webview可以正常找到元素,但切换到NATIVE_APP后,再次切换到webview时,appium就无法定位元素,且等待一段时间后,自动退出执行。

原因为:Appium第一次切换到Html页面时,会新生成一个Chromedriver;当第二次切换到Html时,会使用已经存在的Chromedriver。

 

解决方式:

  找到Appium\Appium\node_modules\appium\lib\devices\android目录下的android-hybrid.js文件,找到如下代码

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"));
  }

  // 修改时间:2018-07-23  目的:二次切换webview后,chromeDriver未重新生成,无法定位元素
  // 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);
  // 修改时间:2018-07-23  目的:二次切换webview后,chromeDriver未重新生成,无法定位元素
};

 

转载于:https://www.cnblogs.com/JcHome/p/9429717.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值