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

qiqiqinqin · 2018年02月23日 · 最后由 TesterWa 回复于 2023年01月29日 · 11252 次阅读
本帖已被设为精华帖!

感谢

文章的主要思路来自于 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 替换为相应版本后,即可成功切换。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 43 条回复 时间 点赞
思寒_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

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

匿名 · #35 · 2018年07月26日
仅楼主可见

@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
simple [精彩盘点] TesterHome 社区 2018 年 度精华帖 中提及了此贴 01月07日 12:08
47楼 已删除

推荐这一套方案:https://github.com/richshaw2015/wxapp-appium 经过工程实践检验过

为什么我的是这个样子,求大神解答:

ftzx007 回复

我的也是,看不到小程序的页面

有人遇到过这种情况吗?微信调试模式已经打开。

@ 巴拉巴拉_修 请问这个问题你解决了吗

回复内容未通过审核,暂不显示
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册