WebView切换
1、目前app基本都是混合型的,有原生的native_app,也有webview,使用appium测试的时候,遇到webview的时候需要切换上下文,切换后就可以使用css等方式定位
2、如何获取app的上下文
- self.driver.context 获取当前窗口的context,
- self.driver.contexts,获取当前窗口的context,返回的是list
3、切换webview:
- self.driver.switch_to.context("xxx")切换到对应的webview
4、如何判断是否是webview:
- 使用uiaumator的时候class:会包含webview/view,如android.webkit.WebView
- 使用self.driver.context/self.driver.contexts获取到的context包含如:WEBVIEW_com.xxx等就是
5、如何从webview切换到原生app:
-
self.driver.switch_to.context("NATIVE_APP")
6、代码:
1 def get_webview(self,webname): 2 webviews=self.driver.contexts 3 for web in webviews: 4 if webname in web: 5 return webname 6 def swich_webview(self): 7 self.driver.switch_to.context(self.get_webview("xxxx")) 8 #切换后可以使用css方式定位 9 self.driver.find_element_by_css_selector()