【问题标题】:WebdriverIO - How to send arguments to Jasmine tests?WebdriverIO - 如何向 Jasmine 测试发送参数?
【发布时间】:2015-10-15 16:09:53
【问题描述】:

我有一堆Webdriver.io/Selenium 测试,我正在使用 jasmine 测试框架来编写我的测试,所以我有一个 wdio.conf.js 文件,我已经配置为使用 jasmine,按照these instructions

问题在于我正在测试一个基于广告/营销的 Web 应用程序,它是高度动态/可配置的,并且需要我传递大量配置信息,以便测试知道要测试什么。例如在命令行我想运行类似:

> wdio --campaignId=123 --productId=456

我可以修改 wdio.conf.js 文件中的“onPrepare”函数/事件来获取这些参数,如下所示:

exports.config = {
    ....other options (snip)...

    framework: 'jasmine',

    jasmineNodeOpts: {
        defaultTimeoutInterval: 9999999,
        expectationResultHandler: function (passed, assertion) { }
    },

    onPrepare: function (config) {
        var campaignId = parseInt(process.argv[2]
            .replace('--campaignId=', ''));
        var productId = parseInt(process.argv[3]
            .replace('--productId=', ''));

        config.params = {
            campaignId: campaignId,
            productId: productId
        };
    }
};

...但我不知道如何将这些传递给我的茉莉花测试。我尝试从 process.argv 读取,但缺少 campaignId 和 productId 参数,例如

describe('Campaign Tests', function () {

    beforeEach(function(done) {
        browser
            .session(function(err, client) {
                var campaignId = parseInt((process.argv[2] || '')
                    .replace('--campaignId=', ''));
                var productId = parseInt((process.argv[3] || '')
                    .replace('--productId=', ''));

                // campaignId and productId are both NaN/undefined
            });
    });

    it('should test something...snip...

如何将自定义参数/参数传递给我的 jasmine 测试?我可以将它们写入 onPrepare 中的文件,然后在 jasmine 测试中读取该文件,这似乎有点 hacky。

【问题讨论】:

    标签: selenium-webdriver jasmine webdriver-io


    【解决方案1】:

    我建议使用环境变量来处理这种行为。

    例如,您可以像 VARIABLE=1 wdio wdio.config.js 这样运行测试,然后在测试中您可以使用 process.env.VARIABLE 轻松访问它

    如果您使用 gulp、grunt 等构建工具,则可以通过标准方式 (gulp e2e --variable=1) 传递命令行,然后在构建工具任务中设置 process.env.VARIABLE

    【讨论】:

    • 我可以添加像 process.env.xyz 这样的自定义变量并从 CLI 传递 xyz 值吗?
    猜你喜欢
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 2021-05-05
    • 2021-04-08
    • 2017-12-14
    • 2016-09-14
    • 1970-01-01
    相关资源
    最近更新 更多