HuJiaoHJ/blog

使用 Appium 进行微信小程序自动化测试

Opened this issue · 4 comments

使用 Appium 进行微信小程序自动化测试

使用 node(wd)编写 Appium 测试用例 介绍了使用 wd 编写简单的 Appium 测试用例

本文主要介绍使用 Appium 进行微信小程序自动化测试,涉及使用 wd 编写复杂 Appium 测试用例以及微信 webview 自动化测试。

测试项目搭建及简单前期准备参考使用 node(wd)编写 Appium 测试用例

其实微信小程序就是webview,所以可以使用webview测试方式进行小程序的测试

打开微信调试功能

用微信打开http://debugx5.qq.com页面,或者直接打开二维码:

debugx5

勾选【打开TBS内核Inspector调试功能】,如下:

debugx5_page

设置好之后,就可以在chrome浏览器中打开chrome://inspect/页面查看当前微信中打开的H5页面了

在电脑中安装chromedriver

安装Appium时,会自动安装chromedriver,但是在使用默认安装的chromedriver时,对于一些老设备会存在一些问题,报类似下面这样的错误:

An unknown server-side error occurred while processing the command.
Original error: unknown error: Chrome version must be >= 55.0.2883.0

解决办法可以参考官方文档

我使用的测试机chrome版本是59.0.3071.0,所以直接下载 v2.32 版本的 chromedriver 到 /usr/local/ 目录下,在启动Appium时,通过 --chromedriver-executable 指定使用此chromedriver,如下:

$ appium --chromedriver-executable /usr/local/chromedriver

编写测试用例

以测试【美团酒店+】小程序为例,测试代码如下:( setup.js、logging.js 参考使用 node(wd)编写 Appium 测试用例

weapp.js

require("../helpers/setup");

const wd = require("wd");

const serverConfig = {
    host: 'localhost',
    port: 4723
};

describe("sample test", function () {
    this.timeout(300000);

    let driver;
    let allPassed = true;

    before(function () {
        
        driver = wd.promiseChainRemote(serverConfig);
        require("../helpers/logging").configure(driver);

        let desired = {
            platformName: 'Android',
            deviceName: 'U2TDU15904014013',
            appPackage: 'com.tencent.mm',
            appActivity: '.ui.LauncherUI',
            fullReset: false,
            fastReset: false,
            noReset: true,
            chromeOptions: {
                androidProcess: 'com.tencent.mm:appbrand0',
            }
        };
        return driver
            .init(desired)
            .setImplicitWaitTimeout(8000);
    });

    after(function () {
        return driver
            .quit();
    });

    afterEach(function () {
        allPassed = allPassed && this.currentTest.state === 'passed';
    });

    it("enter 小程序", function () {
        return driver
            .elementByXPath("//*[@text='发现']")
            .click()
            .elementByXPath("//*[contains(@text, '朋友圈')]")
            .then(function () {
                let action = new wd.TouchAction(driver);
                action.press({x: 20, y: 0}).moveTo({x: 20, y: 20}).wait(200).release().perform();
                return driver.performTouchAction(action);
            })
            .elementByXPath("//*[@text='小程序']")
            .click()
            .elementByXPath("//*[contains(@text, '美团酒店+')]")
            .click()
            .elementByXPath("//*[contains(@text, '美团酒店')]")
            .should.eventually.exist
            .context('WEBVIEW_com.tencent.mm:appbrand0')
            .sleep(5000)
            .elementsByCssSelector('.cell', function (err, els) {
                els[0].click();
            })
            .sleep(5000);
    });
});

执行测试用例

package.json中添加以下脚本:

{
    ...
    "scripts": {
        "weapp": "mocha ./test/weapp.js"
    }
    ...
}

执行测试用例:

$ appium --chromedriver-executable /usr/local/chromedriver # 启动Appium服务且指定chromedriver
$ npm run weapp # 运行测试用例

执行结果如下:

appium-weapp

以上就是使用 Appium 进行微信小程序自动化测试~

完整代码:https://github.com/HuJiaoHJ/appium-wd-example

经过一系列的实践之后,发现使用 Appium 进行微信小程序自动化测试依旧还存在以下几个问题:

1、微信在6.5.23版本之后在使用 driver.context(WEBVIEW_com.tencent.mm:appbrand0) 时,获取的 https://servicewechat.com/{appid}/{version}/page-frame.html 中body为空,而页面内容都包含在 https://servicewechat.com/preload/page-frame.html ,而在切换时,随机获取两个html中一个,所以会存在获取到空内容的情况,导致测试流程走不通(微信社区问题:https://developers.weixin.qq.com/blogdetail?action=get_post_info&lang=zh_CN&token=1707682624&docid=a85a5892193727a1954ccf55198c77d2&comment_lvl=1

2、在小程序内部进行页面跳转之后,webview之间的相互切换暂时是存在问题的,原因还是上面提到的两个html的原因,暂时没有找到解决办法。(社区问题:https://testerhome.com/topics/7769

所以,大概在17年12月更新的版本之后,还是不要再考虑使用 Appium 进行微信小程序的自动化测试了,网上很多教程都是在17年12月之前的,所以能走通整个流程,但是现在是存在问题的

欢迎找到解决办法的小伙伴分享一下~~~

写在最后:从调研到得出最终结论,花了差不多一周的时间,中间还经历了一个春节。虽然最后此方案没能用在实际开发中,有点遗憾,不过整个过程还是收获不少~~~

.elementByXPath("//*[contains(@text, '美团酒店+')]")
.click()

我测试,到这步就无响应了

最后一步卡住了

加上这一行代码.context('WEBVIEW_com.tencent.mm:appbrand0')
电脑端和手机端的Chrome 都比65.0.3325.0 要高 ,还是运行报错

Error: [context("WEBVIEW_com.tencent.mm:appbrand0")] Error response status: 13, UnknownError - An unknown server-side error occurred while processing the command. Selenium error: 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 >= 65.0.3325.0
(Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.13.3 x86_64))

不加 .context('WEBVIEW_com.tencent.mm:appbrand0')
提示css 不支持

Error: [elementsByCssSelector(".cell")] Error response status: 32, , InvalidSelector - Argument was an invalid selector (e.g. XPath/CSS). Selenium error: Locator Strategy 'css selector' is not supported for this session

@ImHype 出现这个问题有几种可能:
1、你的微信没有打开过“美团酒店+”小程序,即在你的小程序列表里找不到“美团酒店+”
2、小程序列表里有“美团酒店+”小程序,但是没有在列表的第一屏内,这种情况可能也会打不开
大概能想到这几个可能吧,希望能对你有帮助~

@vera3224 我在调研实践过程中,也经常遇到错误码为13的报错,试过很多解决方案,到最后发现,只有卸载微信,重新安装微信,再执行正确的测试用例代码,希望这种方法能解决你的问题