0%

Python-04:AppWeb自动化测试【打开App切换WebView登录】

概要内容

  • 环境准备
  • 测试步骤
  • 案例代码

环境准备

  1. NodeJS v12.13.1
  2. JDK 1.8.0_191
  3. Android SDK
  4. Appium-Python-Client

具体教程请看: 自动化测试-02:实践

步骤如下

  1. AppWebView开启调试模式:
    1
    setWebContentsDebuggingEnabled(true);
  2. 获取Android Chrome 版本:
    1
    adb shell am start -a android.intent.action.VIEW -d  https://liulanmi.com/labs/core.html
  3. 根据Android Chrome版本,下载对应ChromeDriver驱动 下载地址
  4. 将下载的ChromeDriver Copy 至 Appium 目录,比如我的Appium目录:
    1
    C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win
  5. 启动APP后并跳转App内嵌的WebView页面,使用Chrome获取WebView中的元素
    1
    2
    打开Chrome 
    地址栏输入:chrome://inspect
    如下图所示:

案例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 案例:自动化测试-登录

from appium import webdriver
from selenium.webdriver.common.by import By

import time

desired_caps = {}
desired_caps["platformName"] = "android"
desired_caps["platformVersion"] = "7.1.2"
desired_caps["deviceName"] = "127.0.0.1:62001"
desired_caps["appPackage"] = "com.guide.custom"
desired_caps["appActivity"] = "com.anyapps.Activity.AALaunch"
desired_caps["noReset"] = "true"
desired_caps["autoGrantPermissions"] = "true"
desired_caps['chromeOptions'] = {"w3c":False}
desired_caps['showChromedriverLog'] = True

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

view_context = "WEBVIEW_com.guide.custom"
driver.switch_to.context(view_context)
time.sleep(5)
print(driver.current_context)


# 获取账号和密码元素,输入账号密码
inputAccount = driver.find_element_by_id("account")
inputAccount.send_keys('186xxxxxxx')
inputPassword = driver.find_element_by_id("password")
inputPassword.send_keys('123456')
# 获取登录元素,点击登录
loginBtn = driver.find_element_by_class_name('confirmDisable')
loginBtn.click()

Appium 调试App 原理图

参考文献:


以上: 如发现有问题,欢迎留言指出,我及时更正

如何文章对你有益,请给我买杯豆浆喝