切换webview步骤
以下是在Appium测试中与webview
对话所需的步骤:
- 导航到应用程序中web页面
- 获取上下文列表,它返回一个我们可以访问的上下文列表,例如:
NATIVE_APP
或WEBVIEW_1
- 切换到你要操作的webview,原来是在
NATIVE_APP
,切换到WEBVIEW_1
,do something
- 操作完后,返回到原来的app view(通常为
NATIVE_APP
)
Python
from appium import webdriver
mobile_desired_caps = {
'platformName': 'Android',
'platformVersion': '7.0',
'deviceName': 'your device name',
"app": r"your.apk",
# 指定Chromedriver存放的地址,或者下边的路径,二者其一即可
"chromedriverExecutableDir": r"C:\path",
# "chromedriverExecutable": r"C:\path\chromedriver.exe",
# 声明中文
"unicodeKeyboard": 'True',
# 声明中文,否则不支持中文
"resetKeyboard": 'True',
# 执行时不重新安装包
'noReset': 'True',
'automationName': 'uiautomator2',
'appPackage': 'your package',
'appActivity': 'your App activity'
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', mobile_desired_caps)
# 点击进入到webview页面
driver.find_elements_by_id("id").click()
# 切换到 webview
webview = driver.contexts[-1]
driver.switch_to.context(webview)
print "###########################################"
print driver.contexts
# [u'NATIVE_APP', u'WEBVIEW_xxxxxxxxx']
# 判断webview上的元素是否存在
print driver.find_element_by_id("btnLogoutConfirmation").is_displayed()
# 返回到原生view
driver.switch_to.context(driver.contexts[0])
Java
DesiredCapabilities cap = new DesiredCapabilities();
//运行mobile browser时指定
//cap.setCapability(CapabilityType.BROWSER_NAME, "chrome");
cap.setCapability("platformName", "Android"); //指定测试平台
//指定测试机的ID,通过adb命令adb devices获取
cap.setCapability("deviceName", "your device name");
cap.setCapability("platformVersion", "7.0");
cap.setCapability("app", "your.apk");
cap.setCapability("appPackage", "your package");
cap.setCapability("appActivity", "your app activity");
// 指定Chromedriver存放的地址,或者下边的路径,二者其一即可
cap.setCapability("chromedriverExecutableDir", "C:\path");
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
// 点击进入到webview页面
driver.findElement(By.cssSelector(".login_button")).click();
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
//prints out something like NATIVE_APP \n WEBVIEW_1
System.out.println(contextName);
}
// 切换到 webview
driver.context((String) contextNames.toArray()[1]);
//do some web testing
String myText = driver.findElement(By.cssSelector(".green_button")).click();
// 切换到原生view
driver.context("NATIVE_APP");
// do more native testing if we want
driver.quit();
运行时报错
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: No Chromedriver found that can automate Chrome ‘64.0.3282’. See https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md">https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md for more details.
报类似这种错误是由于Chrome和driver版本不对应的原因。
解决方案
- 根据错误提示显示的版本号
64.0.3282
下载相应的Chromedriver版本(参考下边),将下载的driver解压,替换掉路径(Windows)C:\Users\xxxx\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win
下的chromedriver.exe
即可 - 运行脚本时指定chromedriver的运行路径(下载对应版本的driver,放在该路径下),即在desired_caps添加
chromedriverExecutableDir
或chromedriverExecutable
字段(如上边的Java和Python代码)
chromedriver对应Chrome的版本
Version | Minimum Chrome Version | Link to Chromedriver |
2.44 | 69.0.3497.0 | |
2.43 | 69.0.3497.0 | |
2.42 | 68.0.3440.0 | |
2.41 | 67.0.3360.0 | |
2.40 | 66.0.3359.0 | |
2.39 | 66.0.3359.0 | |
2.38 | 65.0.3325.0 | |
2.37 | 64.0.3282.0 | |
2.36 | 63.0.3239.0 | |
2.35 | 62.0.3202.0 | |
2.34 | 61.0.3163.0 | |
2.33 | 60.0.3112.0 | |
2.32 | 59.0.3071.0 | |
2.31 | 58.0.3029.0 | |
2.30 | 58.0.3029.0 | |
2.29 | 57.0.2987.0 | |
2.28 | 55.0.2883.0 | |
2.27 | 54.0.2840.0 | |
2.26 | 53.0.2785.0 | |
2.25 | 53.0.2785.0 | |
2.24 | 52.0.2743.0 | |
2.23 | 51.0.2704.0 | |
2.22 | 49.0.2623.0 | |
2.21 | 46.0.2490.0 | |
2.20 | 43.0.2357.0 | |
2.19 | 43.0.2357.0 | |
2.18 | 43.0.2357.0 | |
2.17 | 42.0.2311.0 | |
2.16 | 42.0.2311.0 | |
2.15 | 40.0.2214.0 | |
2.14 | 39.0.2171.0 | |
2.13 | 38.0.2125.0 | |
2.12 | 36.0.1985.0 | |
2.11 | 36.0.1985.0 | |
2.10 | 33.0.1751.0 | |
2.9 | 31.0.1650.59 | |
2.8 | 30.0.1573.2 | |
2.7 | 30.0.1573.2 | |
2.6 | 29.0.1545.0 | |
2.5 | 29.0.1545.0 | |
2.4 | 29.0.1545.0 | |
2.3 | 28.0.1500.0 | |
2.2 | 27.0.1453.0 | |
2.1 | 27.0.1453.0 | |
2.0 | 27.0.1453.0 |
所有可用的Chromedriver版本在这里。
扩展(自动化mobile web)
- 运行自带的浏览器则修改配置为
'browserName': 'Browser'
(我用的android pad,目前测试不成功。。)
通过安装Chrome APP,然后执行以下脚本是可以的:
from appium import webdriver
web_desired_caps = {
'platformName': 'Android',
'platformVersion': '7.0',
'deviceName': '520381b347dd148b',
'browserName': 'chrome',
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', web_desired_caps)
driver.get("https://www.baidu.com/")
获取APP的
appPackage
和appActivity
打开APP,然后命令行运行:adb shell dumpsys window windows | findstr "Current"
则看到appPackage
和appActivity
(以下是打开自带浏览器):参考
- https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md
- http://appium.io/docs/en/writing-running-appium/web/hybrid/
- https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md
- https://github.com/appium/python-client
- https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/other/appium-bindings.md
- http://appium.io/docs/en/writing-running-appium/web/ios-webkit-debug-proxy/
- https://github.com/appium-boneyard/sample-code/tree/master/sample-code