问题:

 hybrid app进行appium自动化时,都会进行contexts切换到webview来进行事件点击。但如果打开了第二个webview的情况下并且webview是新开页面,可能会识别成之前的webview内容。导致无法进行事件点击。

例:

 

解决方法:

 切换到新的webview页面时候,先进行切换回native的context,此时杀掉chromedriver进行,再次切换webview context让chromedriver重新获取新的webview。

注意:不要在webview context情况下杀掉chromedriver,会导致页面关闭。

 

更新:appium1.5版本以上可以设置

recreateChromeDriverSessions=true  不需要手动kill。

 

 

杀掉chromedriver:

pkill -9 chromedriver

 

 

附带切换context代码

def switch_context(self, context="WEBVIEW_xxxx"):
    context = context.upper()
    contexts = self.driver.contexts
    for con in contexts:
        if context in con and context.find('WEBVIEW') != -1:
            print 'switch webview'
            self.driver.switch_context(con)
            res = self.driver.current_context
            return True if context in res else False
        elif context in con and context.find('NATIVE') != -1:
            print 'switch native'
            self.driver.switch_context(con)
            os.system('pkill -9 chromedriver')
            res = self.driver.current_context
            print res
            return True if context in res else False
    return SwitchContextError


def switch_content_default(self):
    time.sleep(3)
    self.driver.switch_context('NATIVE_APP')
    os.system('pkill -9 chromedriver')
    for con in self.driver.contexts:
        if con.find('WEBVIEW') != -1:
            self.driver.switch_context(con)
            return True
    return SwitchContextError

 

posted on 2017-11-07 15:00  Dapped_zhang  阅读(1550)  评论(0编辑  收藏  举报