切换webview步骤

以下是在Appium测试中与​​webview​​对话所需的步骤:


  1. 导航到应用程序中web页面
  2. 获取上下文列表,它返回一个我们可以访问的上下文列表,例如:​​NATIVE_APP​​​或​​WEBVIEW_1​
  3. 切换到你要操作的webview,原来是在​​NATIVE_APP​​​,切换到​​WEBVIEW_1​​​,​​do something​
  4. 操作完后,返回到原来的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版本不对应的原因。

解决方案

  1. 根据错误提示显示的版本号 ​​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​​即可
  2. 运行脚本时指定chromedriver的运行路径(下载对应版本的driver,放在该路径下),即在desired_caps添加​​chromedriverExecutableDir​​​或​​chromedriverExecutable​​字段(如上边的Java和Python代码)

chromedriver对应Chrome的版本

Version

Minimum Chrome Version

Link to Chromedriver

2.44

69.0.3497.0

​v2.44 (link)​

2.43

69.0.3497.0

​v2.43 (link)​

2.42

68.0.3440.0

​v2.42 (link)​

2.41

67.0.3360.0

​v2.41 (link)​

2.40

66.0.3359.0

​v2.40 (link)​

2.39

66.0.3359.0

​v2.39 (link)​

2.38

65.0.3325.0

​v2.38 (link)​

2.37

64.0.3282.0

​v2.37 (link)​

2.36

63.0.3239.0

​v2.36 (link)​

2.35

62.0.3202.0

​v2.35 (link)​

2.34

61.0.3163.0

​v2.34 (link)​

2.33

60.0.3112.0

​v2.33 (link)​

2.32

59.0.3071.0

​v2.32 (link)​

2.31

58.0.3029.0

​v2.31 (link)​

2.30

58.0.3029.0

​v2.30 (link)​

2.29

57.0.2987.0

​v2.29 (link)​

2.28

55.0.2883.0

​v2.28 (link)​

2.27

54.0.2840.0

​v2.27 (link)​

2.26

53.0.2785.0

​v2.26 (link)​

2.25

53.0.2785.0

​v2.25 (link)​

2.24

52.0.2743.0

​v2.24 (link)​

2.23

51.0.2704.0

​v2.23 (link)​

2.22

49.0.2623.0

​v2.22 (link)​

2.21

46.0.2490.0

​v2.21 (link)​

2.20

43.0.2357.0

​v2.20 (link)​

2.19

43.0.2357.0

​v2.19 (link)​

2.18

43.0.2357.0

​v2.18 (link)​

2.17

42.0.2311.0

​v2.17 (link)​

2.16

42.0.2311.0

​v2.16 (link)​

2.15

40.0.2214.0

​v2.15 (link)​

2.14

39.0.2171.0

​v2.14 (link)​

2.13

38.0.2125.0

​v2.13 (link)​

2.12

36.0.1985.0

​v2.12 (link)​

2.11

36.0.1985.0

​v2.11 (link)​

2.10

33.0.1751.0

​v2.10 (link)​

2.9

31.0.1650.59

​v2.9 (link)​

2.8

30.0.1573.2

​v2.8 (link)​

2.7

30.0.1573.2

​v2.7 (link)​

2.6

29.0.1545.0

​v2.6 (link)​

2.5

29.0.1545.0

​v2.5 (link)​

2.4

29.0.1545.0

​v2.4 (link)​

2.3

28.0.1500.0

​v2.3 (link)​

2.2

27.0.1453.0

​v2.2 (link)​

2.1

27.0.1453.0

​v2.1 (link)​

2.0

27.0.1453.0

​v2.0 (link)​

所有可用的Chromedriver版本在​​这里​​。

扩展(自动化mobile web)

  1. 运行自带的浏览器则修改配置为​​'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​​(以下是打开自带浏览器):
Appium  混合应用切换webview_自动化混合应用

参考