简体   繁体   English

如何将参数从Gruntfile.js传递到webdriverio规范?

[英]How to pass parameters from the Gruntfile.js to the webdriverio spec?

I would like to parameterize my webdriverio specs from the Gruntfile.js. 我想从Gruntfile.js参数化我的webdriverio规格。 The goal is to specify the host, the port, a username a password and maybe other parameters in Grunt and read them from the spec file. 目的是在Grunt中指定主机,端口,用户名,密码以及其他参数,然后从规格文件中读取它们。

Reading the Source Labs example from https://www.npmjs.com/package/grunt-webdriver#overview I set the host and the port in the options. https://www.npmjs.com/package/grunt-webdriver#overview阅读Source Labs示例我在选项中设置了主机和端口。 But when configuring the port I got the following error: 但是在配置端口时出现以下错误:

/Users/sandro/Developing/Projekte/sling/svn/contrib/explorers/resourceeditor/frontend/node_modules/grunt-webdriver/node_modules/webdriverio/lib/utils/PromiseHandler.js:154
             throw error;
                   RuntimeError: RuntimeError

This is why I think there must be an other way to do that. 这就是为什么我认为必须有其他方法来做到这一点的原因。 My Gruntfile.js looks like that: 我的Gruntfile.js看起来像这样:

module.exports = function(grunt) {

var e2eTestSpecFolder = '../src/test/javascript/e2e/spec/**/*spec.js';

grunt.initConfig({
...
    webdriver: {
        options: {
            host: 'localhost',
            port: 8080
        },
        chrome: {
            tests: [e2eTestSpecFolder],
            options: {
                // overwrite default settings 
                desiredCapabilities: {
                    browserName: 'chrome'
                }
            }
        },
        firefox: {
            tests: [e2eTestSpecFolder],
            options: {
                // overwrite default settings 
                desiredCapabilities: {
                    browserName: 'firefox'
                }
            }
        }
    }
})

...
grunt.registerTask('desktop_build', ['webdriver:chrome', 'webdriver:firefox']);
};

Thanks in advance for any hints! 预先感谢您的任何提示!

Update: I use the following versions: 更新:我使用以下版本:

  • grunt-cli: v0.1.13 grunt-cli:v0.1.13

  • grunt: v0.4.5 咕unt声:v0.4.5

  • webdriver-manager: 3.0.0 webdriver-manager:3.0.0

  • grunt-webdriver: 0.4.8 grunt-webdriver:0.4.8

Ok, I got your problem :) 好的,我有你的问题:)

These "host" and "port" parameters are the the pre-defined one and are using for another purpose (it's host and port where the tests will be executed, and you are redefining port - that's why they are failing, for example here - https://github.com/webdriverio/webdriverio/blob/master/examples/webdriverio.saucelabs.js you can see they are using for connecting to saucelabs). 这些“主机”和“端口”参数是预定义的参数,它们用于另一目的(将在其中执行测试的主机和端口,而您正在重新定义端口-这就是它们失败的原因,例如,在这里- https://github.com/webdriverio/webdriverio/blob/master/examples/webdriverio.saucelabs.js,您可以看到它们正在用于连接到ucelabs。 For that purposes the simpliest solution is defining ENV variables and make some default values for them (but you shouldn't do it inside gruntfile in fact, it's not neccessary) You can define it in the file, where you are placing these variables for the first time, like: 为此,最简单的解决方案是定义ENV变量并为其设置一些默认值(但实际上,您不必在gruntfile中执行此操作,这不是必需的)。您可以在文件中定义它,将这些变量放置在文件中。第一次像:

testHost: (typeof(process.env.TEST_HOST) === 'undefined') ? 'http://localhost' : process.env.TEST_HOST;

And after that you are just providing TEST_HOST if it's needed as an env variable: 之后,如果需要将它用作环境变量,则只需提供TEST_HOST:

    Linux: sh~ TEST_HOST=http://google.com
grunt task
    Win: export TEST_HOST=http://google.com
grunt task

If you will not set the variable, then " http://localhost " will be the default one. 如果不设置该变量,则默认为“ http:// localhost ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM