Appium 使用 Appium 测试微信小程序 Webview

qiqiqinqin · 2018年02月23日 · 最后由 gaogao621621 回复于 2018年11月16日 · 15584 次阅读
本帖已被设为精华帖!

感谢

文章的主要思路来自于seveniruby的Appium 微信 webview 的自动化技术,非常感谢~~

打开调试功能

通过微信打开debugx5.qq.com,或者直接扫下面二维码

勾选【打开TBS内核Inspector调试功能】

Chrome查看页面元素

手机连接电脑,查看是否连接成功。如下展示设备号则为连接成功

进入任意小程序,以【X东】为例,在Chrome浏览器访问chrome://inspect/#devices

点击inspact,即可查看页面元素

小程序的进程

微信有很多的进程,每一个小程序都运行在不同的进程中。
进入【X东】后,看下当前运行在哪个进程中

我们可以看到,当前的小程序运行在com.tencent.mm:appbrand0中,记住这个进程,记住啦

Appium编写测试用例

最关键的一点,需要添加androidProcess这一项,也就是我们上面的com.tencent.mm:appbrand0
我用python写一个简单的demo

desired_caps = {
'platformName': 'Android',
'fastReset': 'false',
'deviceName': 'XXXXXX',
'appPackage': 'com.tencent.mm',
'appActivity': '.ui.LauncherUI',
'fullReset': 'false',
'unicodeKeyboard': 'True',
'resetKeyboard': 'True',
'chromeOptions': {
'androidProcess': 'com.tencent.mm:appbrand0'
}
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.find_element_by_name("发现").click()
driver.find_element_by_name("小程序").click()
driver.find_element_by_name("X东购物").click()
driver.switch_to.context('WEBVIEW_com.tencent.mm:appbrand0')
time.sleep(5)
print(driver.page_source)

切换Webview失败

我遇到过一种切换Webview失败的情况,下面贴上appium的报错日志。

> info: Chromedriver: Changed state to 'stopped'
> error: Chromedriver: Chromedriver exited unexpectedly with code null, signal SIGTERM
> warn: Chromedriver for context WEBVIEW_com.tencent.mm:appbrand1 stopped unexpectedly
> warn: Chromedriver quit unexpectedly, but it wasn't the active context, ignoring
> error: Chromedriver: Error: A new session could not be created. (Original error: session not created exception
> from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"32496.2","isDefault":true},"id":2,"name":"","origin":"https://servicewechat.com"}
> (Session info: chrome=57.0.2987.132)
> (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64))
> at JWProxy.command$ (lib/proxy.js:133:15)
> at tryCatch (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:67:40)
> at GeneratorFunctionPrototype.invoke [as _invoke] (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:315:22)
> at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:100:21)
> at GeneratorFunctionPrototype.invoke (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:136:37)
> at bound (domain.js:284:14)
> at GeneratorFunctionPrototype.runBound (domain.js:297:12)
> at run (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\es6.promise.js:89:39)
> at D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\es6.promise.js:100:28
> at flush (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\$.microtask.js:17:13)
> at process._tickDomainCallback (node.js:381:11)
> { [Error: A new session could not be created. (Original error: session not created exception
> from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"32496.2","isDefault":true},"id":2,"name":"","origin":"https://servicewechat.com"}
> (Session info: chrome=57.0.2987.132)
> (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64))]
> status: 33,
> value: { message: 'session not created exception\nfrom unknown error: Runtime.executionContextCreated has invalid \'context\': {"auxData":{"frameId":"32496.2","isDefault":true},"id":2,"name":"","origin":"https://servicewechat.com"}\n (Session info: chrome=57.0.2987.132)\n (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64)' },

注意最后那一段的chrome和chromedriver的版本

查看对应表可知,两者版本不对应,需要替换chromedriver

将Appium的chromedriver替换为相应版本后,即可成功切换。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 37 条回复 时间 点赞
思寒_seveniruby 将本帖设为了精华贴 02月23日 17:29

能获取到截图吗

xdefine 回复

我尝试过在webview里截图是不成功的,原因还不清楚
如果要截webview,我现在的做法是:切换到native -> 截图 -> 再切回webview

😀 😀 , 温馨提示:Chrome浏览器访问chrome://inspect/#devices需要翻墙才能查看页面元素

忘红尘 回复

大坑

小程序跳一跳可以用这种思路操作吗?

fanwo 回复

游戏是没法直接拿页面元素的。我自己考虑的话,只能截图,然后做图像识别咯。

请问一下,根据您写的Demo用例,启动微信app时每次需要重新登录账号(连的真机,微信是登录状态)。有什么办法吗?因为每次调试启动的时候重新登录挺麻烦的

Scofell 回复

desired_caps['noReset'] = 'true' 加上这个试试 吧

huan 回复

感谢,后来我加上'fastReset': 'false', 'fullReset': 'false', 就ok了。

请问为什么我的appium路径下面没有chromedriver这个路径啊,改不了版本

teng 回复

在Appium的目录下,搜索chromedriver就可以了呀

@qiqiqinqin 我现在的情况是可以进入*东小程序了,而且可以点击页面跳转到其他页面,但是driver.page_source打印的仍是首页的内容,请问遇到过这种情况吗

bernie 回复

需要切换window_handles

大哥 我切换微信公众号上下文卡主不动,切换不成功。也还了好多个谷歌驱动 还是不成功

小马 Appium1.7.2 android toast 消息测试 中提及了此贴 03月21日 17:14
宣梦达 回复

appium的日志贴一下吧

@qqiqinqin

这是appium日记[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running 'C:\Android\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","35795c08","shell","ps"]
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running 'C:\Android\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","35795c08","shell","ps"]
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running 'C:\Android\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","35795c08","shell","ps"]
[debug] [AndroidDriver] Parsed pid: '18676' pkg: 'com.tencent.mm:support' from
[debug] [AndroidDriver] USER PID ADJ PPID VSIZE RSS WCHAN PC NAME
[debug] [AndroidDriver] u0_a86 18676 900 821 2311224 131608 0 0000000000 S com.tencent.mm:support
[debug] [AndroidDriver] Returning process name: 'com.tencent.mm:support'
[debug] [AndroidDriver] Parsed pid: '18847' pkg: 'com.tencent.mm:tools' from
[debug] [AndroidDriver] USER PID ADJ PPID VSIZE RSS WCHAN PC NAME
[debug] [AndroidDriver] u0_a86 18847 900 821 2320624 133316 0 0000000000 S com.tencent.mm:tools
[debug] [AndroidDriver] Returning process name: 'com.tencent.mm:tools'
[debug] [AndroidDriver] Parsed pid: '18504' pkg: 'com.tencent.mm:appbrand0' from
[debug] [AndroidDriver] USER PID ADJ PPID VSIZE RSS WCHAN PC NAME
[debug] [AndroidDriver] u0_a86 18504 900 821 2539160 182908 0 0000000000 S com.tencent.mm:appbrand0
[debug] [AndroidDriver] Returning process name: 'com.tencent.mm:appbrand0'
[debug] [AndroidDriver] Found webviews: ["WEBVIEW_com.tencent.mm:support","WEBVIEW_chrome","WEBVIEW_com.tencent.mm:tools","WEBVIEW_com.tencent.mm:appbrand0"]
[debug] [AndroidDriver] Available contexts: ["NATIVE_APP","WEBVIEW_com.tencent.mm:support","WEBVIEW_chrome","WEBVIEW_com.tencent.mm:tools","WEBVIEW_com.tencent.mm:appbrand0"]
[debug] [AndroidDriver] Connecting to chrome-backed webview context 'WEBVIEW_com.tencent.mm:appbrand0'
[debug] [AndroidDriver] A port was not given, using random port: 8000
[debug] [Chromedriver] Changed state to 'starting'
[Chromedriver] Set chromedriver binary as: C:\Users\woxia\AppData\Roaming\npm\node_modules\appium\node_modules_appium-chromedriver@3.4.0@appium-chromedriver\chromedriver\win\chromedriver.exe
[debug] [Chromedriver] Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (netstat -nao ^| findstr /R /C:"8000 ") do (FOR /F "usebackq" %b in (TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe) do (IF NOT %b=="" TASKKILL /F /PID %a))
[Chromedriver] No old chromedrivers seemed to exist
[debug] [Chromedriver] Cleaning any old adb forwarded port socket connections
[debug] [ADB] List forwarding ports
[debug] [ADB] Running 'C:\Android\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","35795c08","forward","--list"]
[Chromedriver] Spawning chromedriver with: C:\Users\woxia\AppData\Roaming\npm\node_modules\appium\node_modules_appium-chromedriver@3.4.0@appium-chromedriver\chromedriver\win\chromedriver.exe --url-base=wd/hub --port=8000 --adb-port=5037 --verbose
[debug] [Chromedriver] Chromedriver version: '2.28.455520'
[debug] [JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:8000/wd/hub/status] with no body
[debug] [JSONWP Proxy] Got response with status 200: "{\"sessionId\":\"\",\"status\":0,\"value\":{\"build\":{\"version\":\"alpha\"},\"os\":{\"arch\":\"x86_64\",\"name\":\"Windows NT\",\"version\":\"10.0.16299\"}}}"
[debug] [JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"androidProcess":"com.tencent.mm:tools","androidDeviceSerial":"35795c08"}}}
[debug] [AndroidBootstrap] Emitting alert message...
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Emitting system alert message
[debug] [JSONWP Proxy] Got response with status 200: {"sessionId":"0941fe59a2ab9575d2e476fcd3845667","status":100,"value":{"message":"chrome not reachable\n (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 10.0.16299 x86_64)"}}
[Chromedriver] Error: Failed to start Chromedriver session: An error occurred (Original error: chrome not reachable
Chromedriver)
[Chromedriver] at Object.wrappedLogger.errorAndThrow (../../lib/logging.js:78:13)
[Chromedriver] at Chromedriver.callee$2$0$ (../../lib/chromedriver.js:217:13)
[Chromedriver] at tryCatch (C:\Users\woxia\AppData\Roaming\npm\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:67:40)
[Chromedriver] at GeneratorFunctionPrototype.invoke as _invoke
[Chromedriver] at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
[Chromedriver] at GeneratorFunctionPrototype.invoke (C:\Users\woxia\AppData\Roaming\npm\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:136:37)
[Chromedriver] at
[debug] [JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8000/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"androidProcess":"com.tencent.mm:tools","androidDeviceSerial":"35795c08"}}}
[debug] [JSONWP Proxy] Got response with status 200: {"sessionId":"854195b08a0dfba4bc9685dcb95c23c4","status":100,"value":{"message":"chrome not reachable\n (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 10.0.16299 x86_64)"}}
[Chromedriver] Error: Failed to start Chromedriver session: An error occurred (Original error: chrome not reachable
Chromedriver)
[Chromedriver] at Object.wrappedLogger.errorAndThrow (../../lib/logging.js:78:13)
[Chromedriver] at Chromedriver.callee$2$0$ (../../lib/chromedriver.js:217:13)
[Chromedriver] at tryCatch (C:\Users\woxia\AppData\Roaming\npm\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:67:40)
[Chromedriver] at GeneratorFunctionPrototype.invoke as _invoke
[Chromedriver] at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
[Chromedriver] at GeneratorFunctionPrototype.invoke (C:\Users\woxia\AppData\Roaming\npm\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:136:37)
[Chromedriver] at

忘红尘 回复

你好,请问有什么免费的翻墙工具吗?初学者求学中,嘻嘻~~~

小白求问~为什么一直打开不了微信,出来的一直是代码初始的helloworldApp,之前用这个代码可以正确安装别的app,代码如下:
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/Mall/src/main/java/apps/");
File app = new File(appDir, "weixin_1280.apk");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("app",app.getAbsolutePath());

capabilities.setCapability("platformName", "Android");

capabilities.setCapability("deviceName", "DU2SSE15AH117303");

capabilities.setCapability("platformVersion", "4.4.2");
capabilities.setCapability("appPackage", "com.tencent.mm");
capabilities.setCapability("appActivity", ".ui.LauncherUI");

你好,我在测试微信小程序,微信webview:WebView in com.tencent.mm (57.0.2987.132),报错信息是:unable to discover open pages,是什么原因
信息如下:
Appium server all ready
2018-04-25 14:06:38,783 - [ - INFO - platformName: Android
2018-04-25 14:06:38,784 - [ - INFO - platformVersion: 5.1
2018-04-25 14:06:38,785 - [ - INFO - deviceName: [
2018-04-25 14:06:38,785 - [ - INFO - app:
2018-04-25 14:06:38,785 - [ - INFO - appPackage: com.tencent.mm
2018-04-25 14:06:38,785 - [ - INFO - appActivity: .ui.LauncherUI
2018-04-25 14:06:38,786 - [ - INFO - noReset: True
2018-04-25 14:06:38,786 - [ - INFO - unicodeKeyboard: True
2018-04-25 14:06:38,786 - [ - INFO - resetKeyboard: True
2018-04-25 14:06:38,786 - [ - INFO - chromeOptions: {'androidProcess': 'com.tencent.mm:tools'}
2018-04-25 14:06:38,787 - [ - INFO - appium server port: 4723

start cases
2018-04-25 14:07:04,582 - [ - INFO - --> LoginPage.init
2018-04-25 14:07:04,583 - [ - INFO - <-- LoginPage.init, Success
2018-04-25 14:07:04,584 - [ - INFO - --> Weixin.test_application
2018-04-25 14:07:04,584 - [ - INFO - --> HomePage.wait_check_page
2018-04-25 14:07:09,745 - [ - INFO - <-- HomePage.wait_check_page, Success
2018-04-25 14:07:09,745 - [ - INFO - --> HomePage.find_button
2018-04-25 14:07:10,593 - [ - INFO - --> HomePage.little_application
2018-04-25 14:07:16,473 - [ - INFO - --> HomePage.application_name
2018-04-25 14:07:20,509 - [ - INFO - --> HomePage.webview
[Chromedriver] Error: Failed to start Chromedriver session: An unknown server-side error occurred while processing the command. (Original error: unknown error: unable to discover open pages
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 10.0.16299 x86_64))
at Object.wrappedLogger.errorAndThrow (../../lib/logging.js:69:13)
at Chromedriver.callee$2$0$ (../../lib/chromedriver.js:217:13)
at tryCatch (C:\Program Files\nodejs\X64\node_global\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke as _invoke
at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
at GeneratorFunctionPrototype.invoke (C:\Program Files\nodejs\X64\node_global\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:136:37)
at
Error: Failed to start Chromedriver session: An unknown server-side error occurred while processing the command. (Original error: unknown error: unable to discover open pages
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 10.0.16299 x86_64))
at Object.wrappedLogger.errorAndThrow (../../lib/logging.js:69:13)
at Chromedriver.callee$2$0$ (../../lib/chromedriver.js:217:13)
at tryCatch (C:\Program Files\nodejs\X64\node_global\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke as _invoke
at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
at GeneratorFunctionPrototype.invoke (C:\Program Files\nodejs\X64\node_global\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:136:37)
at
[Chromedriver] Error: Failed to start Chromedriver session: An unknown server-side error occurred while processing the command. (Original error: unknown error: unable to discover open pages
(Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 10.0.16299 x86_64))
at Object.wrappedLogger.errorAndThrow (../../lib/logging.js:69:13)
at Chromedriver.callee$2$0$ (../../lib/chromedriver.js:217:13)
at tryCatch (C:\Program Files\nodejs\X64\node_global\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke as _invoke
at GeneratorFunctionPrototype.prototype.(anonymous function) as throw
at GeneratorFunctionPrototype.invoke (C:\Program Files\nodejs\X64\node_global\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:136:37)
at
Error: Failed to start Chromedriver session: An unknown server-side e

SUNFEIFEI 回复

这个是你启动的小程序的线程名不对:'androidProcess': 'com.tencent.mm:appbrand0'

kukaka 回复

感谢! 又刚出现一个问题,用chrome://inspect/#devices查看不到小程序页面元素,反而用uiautomatorviewer反而能查看元素属性了,这是什么鬼?? 试了一下公众号没有问题

SUNFEIFEI 回复

你是用的同一个测试机吗?我之前好像也遇见过,因为不是同一个测试机,所以也看不出是什么原因。

kukaka 回复

是同一个

楼主,我切到小程序后拿到的页面没有Body,打印此时窗口句柄,只有1个,请问你遇到过没?

仅楼主可见

mark一下

落雨 使用 chrome://inspect/#devices 查看元素报 404 中提及了此贴 06月19日 17:57
落雨 关于 chrome://inspect/#devices 中提及了此贴 06月22日 18:08
SUNFEIFEI 回复

你的问题解决了么 我也遇到了同样的问题,使用的是appium+java。报错信息:Error: Failed to start Chromedriver session: An unknown server-side error occurred while processing the command. (Original error: unknown error: unable to discover open pages

我也是这个情况,你的解决了吗

仅楼主可见

@qqiqinqin
你好,请问用chrome的inspect看微信小程序xpath,需要特殊配置吗,我直接连接上以后能看到手机,但是看不到页面

仅楼主可见
仅楼主可见

楼主您好,请教一下。我这边按照您帖子的方法测试X东的小程序,切换WEBVIEW都是成功的,但是打印PageSource却只能获取到一些样式文件的内容。反而是切换回NATIVE_APP,能打印出小程序的页面内容,但仍然是原生APP的元素,无法使用chromedriver获取到的H5元素进行定位。

测试萌新 回复

同样问题,楼上解决了没?

现在通过chrome去inspect只能看到搜一搜,看不到小程序呀。。

Jodie 回复

我和你的问题一样,我的连接之后,打开chrome,地址栏输入chrome://inspect/#devices,我打开的是微信小程序,看不到设备或者访问的资源,打开手机谷歌的话,才有一个Chrome显示,也没有显示小程序的Webview页面,请问你解决了吗

hello 小程序测试 Appium 支持 Java-Client6+ 解决方案 中提及了此贴 11月12日 19:36
泡面火锅 回复

你好,你的问题解决了吗,方便加下好友吗2426330053qq

simple 专栏文章:[精华帖] 社区历年精华帖分类归总 中提及了此贴 12月13日 14:44
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册