https://blog.csdn.net/sinat_41688684/article/details/83025740 参考
https://blog.csdn.net/xqtesting/article/details/79376688 参考
https://blog.csdn.net/qq_19636353/article/details/53731254 参考
谷歌驱动对应参考
https://npm.taobao.org/mirrors/chromedriver/2.37/notes.txt
驱动下载
https://npm.taobao.org/mirrors/chromedriver/
谷歌翻墙策略
https://github.com/haotian-wang/google-access-helper
由于新版本Chrome已禁止安装第三方应用,且本破解版无法上传至Chrome网上应用店,因此只能通过以下方法在开发者模式下运行:
- 克隆本仓库到本地或下载master.zip后解压
- 打开Chrome扩展程序管理器
chrome://extensions
- 勾选
开发者模式
- 点击
加载已解压的扩展程序
,选择本扩展所在目录
操作步骤
1、手机与电脑连接,开启USB调试模式,通过adb devices可查看到此设备。
2、电脑端、移动端必须安装chrome浏览器(尽量保证移动端chrome版本与PC端一致),根据对应的Chrome浏览器版本安装对应的Chrome driver
3、App Webview开启debug模式
4、在电脑端Chrome浏览器地址栏输入chrome://inspect/#devices,进入调试模式
此时页面显示了手机型号、驱动名称、APP要调试的WebView名称
点击inspect,若成功加载与APP端相同界面的调试页面,则配置成功
5、执行测试脚本
上面这些搞定了,在Appium里写代码就简单了,先说下关键的几个点:
1、#必须加上此句
desired_caps['chromeOptions']={'androidProcess': 'com.tencent.mm:tools'}
小强提示:
微信的package name=com.tencent.mm,activity=com.tencent.mm.ui.LauncherUI,不要问我怎么知道的。。。
2、#可以通过下面的语句输出webview的名称
contexts=driver.contexts
print('contexts=',contexts)
3、#使用下面的语句切换到指定的webview里
driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')
desired_caps = {
'platformName': 'Android',
'platformVersion': '23',
'deviceName': 'Android Emulator',
'unicodeKeyboard': 'True',
'resetKeyboard': 'True',
'appPackage': 'com.tencent.mm',
'appActivity': 'com.tencent.mm.ui.LauncherUI',
'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'} # 一定要加这句
}
记录
1.PC端 chromedriver 版本大于2.20,
http://chromedriver.storage.googleapis.com/index.html
C:\Program Files (x86)\Appium\node_modules\appium\build\chromedriver\windows
2. 手机端webview 版本(可以用chrome://inspect查看,第6条)和PC端chromedriver匹配:
3. APP添加测试声明配置
WebView.setWebContentsDebuggingEnabled(true);
4. 'recreateChromeDriverSessions':'True',
5. 切换H5
self.driver.switch_to.context('WEBVIEW_co')
self.driver.switch_to.context('NATIVE_APP')
H5元素定位环境搭建
资源下载
Chrome PC浏览器: 官网下载地址
手机版 Chrome
Chrome driver: 下载地址 (注:chrome driver要与Chrome的版本对应:ChromeDriver版本与Chrome版本对应表)
夜神模拟器
dr.fone app 3.2.0 (被测试app)
操作步骤
1、手机与电脑连接,开启USB调试模式,通过adb devices可查看到此设备。
2、电脑端、移动端必须安装chrome浏览器(尽量保证移动端chrome版本与PC端一致),根据对应的Chrome浏览器版本安装对应的Chrome driver
3、App Webview开启debug模式
4、在电脑端Chrome浏览器地址栏输入chrome://inspect/#devices,进入调试模式
此时页面显示了手机型号、驱动名称、APP要调试的WebView名称
点击inspect,若成功加载与APP端相同界面的调试页面,则配置成功
5、执行测试脚本
Webview 调试模式检查与开启方式
1、检查方式:
打开app对应的h5页面,在 chrome://inspect/#devices 地址中,检查是否显示对应的webview,如没有,则当前未开启调试模式。
2、开启方式:
在app中配置如下代码(在WebView类中调用静态方法setWebContentsDebuggingEnabled):
if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
注:此步骤,一般需要App开发人员开启。
H5定位实践案例
1、测试场景
启动dr.fone app 进入backup H5页面中的输入邮箱并点击提交,然后返回
2、代码实现
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
desired_caps={}
desired_caps['platformName']='Android'
desired_caps['deviceName']='127.0.0.1:62025'
desired_caps['platformVersion']='4.4.2'
desired_caps['app']='/Users/frank/Desktop/MonkeyRunner/dr.fone3.2.0.apk'
desired_caps['appPackage']='com.wondershare.drfone'
desired_caps['appActivity']='com.wondershare.drfone.ui.activity.WelcomeActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(5)
print('click BackupBtn')
driver.find_element_by_id('com.wondershare.drfone:id/btnBackup').click()
WebDriverWait(driver,8).until(lambda x:x.find_element_by_id('com.wondershare.drfone:id/btnRecoverData'))
print('click NextBtn')
driver.find_element_by_id('com.wondershare.drfone:id/btnRecoverData').click()
WebDriverWait(driver,8).until(lambda x:x.find_element_by_class_name('android.webkit.WebView'))
contexts=driver.contexts
print(contexts)
#需android4.4及以上版本的系统中才会输出更多的webview
print('switch conetext')
driver.switch_to.context('WEBVIEW_com.wondershare.drfone')
print('edit email')
driver.find_element_by_id('email').send_keys('shuqing@wondershare.cn')
print('click sendBtn')
driver.find_element_by_class_name('btn_send').click()
#切换context 点击返回
driver.switch_to.context('NATIVE_APP')
driver.find_element_by_class_name('android.widget.ImageButton').click()
3、报错&解决方案
报错1
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException:Message: An unknown server-side error occurred while processing the command.
Original error: Failed to start Chromedriver session: A new session could not be created.
(Original error: session not created exception: Chrome version must be >= 60.0.3112.0
报错2
error: Chromedriver: Chromedriver exited unexpectedly with code null, signal SIGTERM
【解决方案】
下载对应版本的chromedriver驱动,放置在: {Appium path}\node_modules\appium\node_modules\appium-chromedriver\chromedriver\mac 替换即可