混合應用中包含 Web 視圖的應用,在 Appium 介紹及環境安裝 中介紹了appium支持WebView測試,本文將分享Android 混合頁面的測試方法。
WebView測試環境准備
手機端
-
被測瀏覽器:
iOS:Safari
Android:Chrome,Chromium,Browser (自帶瀏覽器)
PC端
- 安裝 Chrome瀏覽器(或chromium),可以訪問https://www.google.com
- 下載手機瀏覽器webview版本對應的 driver
- webview和driver版本對應關系:https://raw.githubusercontent.com/appium/appium-chromedriver/master/config/mapping.json
- driver下載:https://sites.google.com/a/chromium.org/chromedriver/downloads
- 國內鏡像地址 : https://npm.taobao.org/mirrors/chromedriver/
- appium github: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md
查看手機瀏覽器版本
adb shell pm list package | grep webview
adb shell pm dump com.android.browser | grep version
adb shell pm dump com.android.chrome | grep version
adb shell pm dump com.android.webview | grep version
查看手機browser和chrome版本
C:\Users\10287>adb shell pm list packages|findstr browser
package:com.android.browser
C:\Users\10287>adb shell pm dump com.android.browser | findstr version
versionCode=22 targetSdk=22
versionName=5.1.1-500200323
C:\Users\10287>adb shell pm dump com.android.chrome | findstr version
versionCode=398713200 targetSdk=29
versionName=80.0.3987.132
enabled=true targetSdkVersion=29 versionCode=398713200
查看手機webview版本
在手機上設置中查看 Android System WebView應用版本
客戶端代碼
-
desirecapability
-
- “chromedriverExecutable" = "指定driver地址”
- "browser" = "Browser" 或者“browser" = ”Chrome“
WebView元素定位
1、連接手機/模擬器
手機打開google瀏覽器,並進入百度網頁:https://m.baidu.com
2、Chrome瀏覽器輸入地址: chrome://inspect
可以看到電腦連接的設備名以及打開的手機瀏覽器
點擊inspect
這樣就可以定位到瀏覽器元素。
Android混合頁面測試
文檔:https://developers.google.com/web/tools/chrome-devtools/remote-debugging/webviews?hl=zh-cn
測試步驟:
- 打開ApiDemos
- 進入WebView頁面
- 點擊"i am a link"
- 退出應用
下載ApiDemos-debug.apk
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
class TestBrowser():
def setup(self):
desired_caps = {
'platformName': 'android',
'platformVersion': '10',
'appPackage': 'io.appium.android.apis',
'appActivity': 'io.appium.android.apis.ApiDemos',
'deviceName': 'CUYDU19626004019',
'noReset': 'true',
'chromedriverExecutable': 'D:/testing_tools/chromedriver85/chromedriver.exe'
}
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
self.driver.implicitly_wait(5)
def teardown(self):
self.driver.quit()
def test_webview(self):
self.driver.find_element_by_accessibility_id("Views").click()
webview ="WebView"
print(self.driver.contexts)
self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().'
'scrollable(true).instance(0)).'
f'scrollIntoView(new UiSelector().text("{webview}")'
'.instance(0));').click()
print(self.driver.contexts)
self.driver.switch_to.context(self.driver.contexts[1])
print(self.driver.current_context)
self.driver.find_element(MobileBy.ID, 'i am a link').click()
文章標題:Android WebView測試
本文作者:hiyo
本文鏈接:https://www.cnblogs.com/hiyong/p/14163722.html
歡迎關注公眾號:「測試開發小記」及時接收最新技術文章!