小程序自动化测试--测试3

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

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

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

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

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

打开微信调试功能

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

debugx5

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

debugx5_page

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

在电脑中安装chromedriver

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

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

解决办法能够参考官方文档json

我使用的测试机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 进行微信小程序自动化测试~

完整代码:github.com/HuJiaoHJ/ap…

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

一、微信在6.5.23版本以后在使用 driver.context(WEBVIEW_com.tencent.mm:appbrand0) 时,获取的 servicewechat.com/{appid}/{ve… 中body为空,而页面内容都包含在 servicewechat.com/preload/pag… ,而在切换时,随机获取两个html中一个,因此会存在获取到空内容的状况,致使测试流程走不通(微信社区问题:developers.weixin.qq.com/blogdetail?…

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

因此,大概在17年12月更新的版本以后,仍是不要再考虑使用 Appium 进行微信小程序的自动化测试了,网上不少教程都是在17年12月以前的,因此能走通整个流程,可是如今是存在问题的

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

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



原文地址:https://github.com/HuJiaoHJ/blog/issues/5

相关文章
相关标签/搜索