WebdriverIO - 如何向Jasmine测试发送参数? [英] WebdriverIO - How to send arguments to Jasmine tests?

查看:154
本文介绍了WebdriverIO - 如何向Jasmine测试发送参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆 Webdriver.io / Selenium测试,我正在使用jasmine测试框架来编写我的测试,所以我有一个wdio.conf.js文件,我已配置为使用jasmine,根据这些说明

I have a bunch of Webdriver.io/Selenium tests, and I'm using the jasmine test framework to write my tests in, so I have a wdio.conf.js file that I have configured to use jasmine, as per these instructions

麻烦的是,我正在测试一个基于广告/营销的网络应用程序,它是高度动态/可配置的,并要求我传入一堆配置信息,以便测试知道要测试的内容。例如在命令行我想运行如下:

The trouble is that I'm testing an ad/marketing based web app that is highly dynamic/configurable, and requires me to pass in a load of configuration information so that the test knows what to test. e.g. at the command line I would like to run something like:

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

我可以修改'onPrepare'功能/事件在wdio.conf.js文件中选择这样的参数:

I can modify the 'onPrepare' function/event in the wdio.conf.js file to pick up these paramters like so:

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 args,例如

...but I have no idea how to pass these along to my jasmine tests. I've tried reading from process.argv but the campaignId and productId args are missing, e.g.

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...

如何将自定义参数/参数传递给我的茉莉花测试?我可以将它们写入onPrepare中的文件,然后在茉莉花测试中读取该文件,这看起来有点笨拙。

How do I pass custom params/arguments along to my jasmine tests? I could write them to a file in onPrepare and then read that file in jasmine tests, that seems a bit hacky.

推荐答案

我向我们推荐用于处理此行为的环境变量。

I recommend to use environment variables to approach this behavior.

例如,您可以运行测试,如 VARIABLE = 1 wdio wdio.config.js 然后在测试中你可以使用 process.env.VARIABLE

So for example you can run your tests like VARIABLE=1 wdio wdio.config.js and then in your test you can access it easily with process.env.VARIABLE

轻松访问它如果你正在使用一些构建工具,如gulp,grunt等,而不是以标准方式传递命令行( gulp e2e --variable = 1 ),然后设置 process.env.VARIABLE 在构建工具任务中。

If you are using some build tool like gulp, grunt, etc. than you can pass command lines the standard way (gulp e2e --variable=1) and then set process.env.VARIABLE inside the build tool task.

这篇关于WebdriverIO - 如何向Jasmine测试发送参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆