appium---如何判斷原生頁面和H5頁面

  目前app中存在愈來愈多的H5頁面了,對於一些作app自動化的測試來講,要求也愈來愈高,自動化不單單要支持原生頁面,也要能夠H5中進行操做自動化,這一篇介紹如何查看頁面上是否存在H5頁面,這裏首先要了解一個知識點webview是什麼html

webview是什麼

webview是屬於android中的一個控件,也至關於一個容器,須要把H5的一些前端內容,經過這個容器去調用,顯示和渲染網頁前端

目前不少app中都實現app原生頁面(native)和webview頁面(H5),咱們如何查看頁面上哪些存在webview和native呢?android

查看webview和native

一、打開uiautomatorviewer定位工具,進行查看頁面上是否存在webview。下圖能夠看到android.webkit.webview。web

 

二、斷網狀況下,進行訪問app,若是能夠正常顯示頁面,說明爲原生頁面,若是不能訪問,則爲webview(H5)頁面app

三、經過fiddler進行抓包查看,內容存在html則爲webview(H5)頁面工具

那麼咱們在appium中如何查看呢?測試

四、經過contexts方法ui

def contexts(self): """ 返回當前會話中的上下文。 """
        return self.execute(Command.CONTEXTS)['value']

執行腳本:spa

# coding:utf-8
from appium import webdriver import time desired_caps = { 'platformName': 'Android',  # 測試版本
                        'deviceName': 'emulator-5554',   # 設備名
                        'platformVersion': '5.1.1', # 系統版本
                        'appPackage': 'com.yipiao', #apk的包名
                       'appActivity': '.activity.LaunchActivity', # apk的launcherActivity
                        "noReset": True,  # 不清空數據
 } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(6) driver.find_element_by_xpath('//*[@text="下次再說"]').click() time.sleep(3) driver.find_element_by_xpath('//*[@text="個人"]').click() # 點擊產品意見
time.sleep(3) driver.find_element_by_xpath('//*[@text="產品意見"]').click() # 獲取所有上下文
cons = driver.contexts print(cons) # ['NATIVE_APP', 'WEBVIEW_com.yipiao']

上面返回的結果中能夠看到頁面中存在NATIVE_APP,和webview_com.yipiao。其中native_app表示app原生頁面,而後webview_com.XXXX表示webview(H5)的頁面code

切換webview

前面已經瞭解到如何查看頁面中是否存在webview,當咱們想要操做webview上的元素時,必需要進入到webview中,那麼如何進入webview呢?

能夠經過 _switch_to.context() 方法進行切換

上面已經查詢到頁面上的內容,而後咱們經過_switch_to.context()方法進行跳轉

# 跳轉webview
driver._switch_to.context("WEBVIEW_com.yipiao") print(driver.context)

源碼:

# coding:utf-8
from appium import webdriver import time desired_caps = { 'platformName': 'Android',  # 測試版本
                        'deviceName': 'emulator-5554',   # 設備名
                        'platformVersion': '5.1.1', # 系統版本
                        'appPackage': 'com.yipiao', #apk的包名
                       'appActivity': '.activity.LaunchActivity', # apk的launcherActivity
                        "noReset": True,  # 不清空數據
 } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(6) driver.find_element_by_xpath('//*[@text="下次再說"]').click() # 點擊個人
time.sleep(3) driver.find_element_by_xpath('//*[@text="個人"]').click() # 點擊產品意見
time.sleep(3) driver.find_element_by_xpath('//*[@text="產品意見"]').click() # 獲取所有上下文
cons = driver.contexts print(cons) # 跳轉到webview中
driver._switch_to.context("WEBVIEW_com.yipiao") print(driver.context)


#  ['NATIVE_APP', 'WEBVIEW_com.yipiao']

#  WEBVIEW_com.yipiao

 

若是感受安靜寫的對您有所幫助,點個關注,持續更新~~哪裏有寫錯的地方,或者不懂的地方,能夠下方留言,看到後第一時間回覆~~

相關文章
相關標籤/搜索