博客
关于我
Appium+python自动化13-native和webview切换
阅读量:470 次
发布时间:2019-03-06

本文共 1674 字,大约阅读时间需要 5 分钟。

为什么理解WebView元素是必要的

在现代移动应用开发中,混合式应用(Native + WebView)已经成为主流。对于测试人员来说,最大的挑战在于如何准确定位和操作WebView渲染的屏幕区域。Native组件的元素可以轻松定位和操作,但WebView渲染的区域则完全不同。通过学习如何识别和处理WebView元素,我们可以更高效地完成测试任务。

如何正确获取WebView的上下文

第一步:观察页面元素

使用定位工具(如Android Studio的LayoutInspector)查看页面元素。通常,页面会分为两部分:Native区域和WebView区域。通过观察,可以发现WebView区域内的元素无法通过普通的定位方式识别。

第二步:检查元素属性

在WebView区域内,通常会有一些固定的标识符。例如,class属性可能显示为"WebView",这表明该区域是由WebView渲染的。通过查看元素属性,我们可以确认该区域是否属于WebView。

上下文管理:理解contexts的作用

在Appium中,contexts用于区分不同的应用上下文(environment)。每个上下文代表一个不同的应用或页面。以下是操作步骤:

第一步:获取所有上下文

通过调用driver.contexts获取当前应用的所有上下文列表。列表中通常包含两个项:

  • NATIVE_APP:表示原生应用
  • WEBVIEW_com.xxxx:表示WebView渲染的区域

第二步:验证WebView上下文

如果列表中包含WEBVIEW_com.xxxx项,说明已经成功识别到了WebView上下文。可以通过打印上下文列表来确认这一点。

第三步:切换到WebView上下文

在完成Native操作后,为了操作WebView元素,需要切换到相应的上下文:

driver.switch_to.context(contexts[1])

这里的contexts[1]表示WebView上下文。

如何切回Native上下文

完成WebView操作后,需要切回Native上下文。可以通过以下两种方式实现:

方法一:使用固定的上下文名称

driver.switch_to.context("NATIVE_APP")

方法二:使用上下文列表

driver.switch_to.context(contexts[0])

完整示例代码

以下是完整的代码示例,展示了上下文切换的实际应用:

from appium import webdriverimport timedesired_caps = {    'platformName': 'Android',    'deviceName': '30d4e606',    'platformVersion': '6.0',    'appPackage': 'com.baidu.yuedu',    'appActivity': 'com.baidu.yuedu.splash.SplashActivity'}driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)time.sleep(30)# 切换到图书界面后获取所有的环境contexts = driver.contextsprint(contexts)# 切换到webviewdriver.switch_to.context(contexts[1])# 获取当前的环境,看是否切换成功now = driver.current_contextprint(now)# 切回nativedriver.switch_to.context(contexts[0])

总结

通过上述方法,我们可以轻松地在混合式应用中切换Between Native和WebView上下文。掌握这一技能,对于自动化测试和应用调试都非常有帮助。在实际开发中,确保正确获取WebView上下文是确保测试成功的关键步骤。

转载地址:http://nfmbz.baihongyu.com/

你可能感兴趣的文章
nmap指纹识别要点以及又快又准之方法
查看>>
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>