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

时间:2015-03-31 13:39:17

标签: javascript gruntjs webdriver-io

我想从Gruntfile.js参数化我的webdriverio规范。 目标是在Grunt中指定主机,端口,用户名和密码以及其他参数,并从spec文件中读取它们。

https://www.npmjs.com/package/grunt-webdriver#overview读取Source Labs示例我在选项中设置了主机和端口。但是在配置端口时出现以下错误:

/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

这就是为什么我认为必须有另一种方法来做到这一点。 我的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']);
};

提前感谢任何提示!

更新: 我使用以下版本:

  • grunt-cli:v0.1.13

  • grunt:v0.4.5

  • webdriver-manager:3.0.0

  • grunt-webdriver:0.4.8

1 个答案:

答案 0 :(得分:1)

好的,我遇到了你的问题:)

这些“主机”和“端口”参数是预定义的参数并且用于其他目的(它是执行测试的主机和端口,并且您正在重新定义端口 - 这就是它们失败的原因,因为这里的示例 - https://github.com/webdriverio/webdriverio/blob/master/examples/webdriverio.saucelabs.js您可以看到它们用于连接酱油)。为此目的,最简单的解决方案是定义ENV变量并为它们设置一些默认值(但实际上你不应该在gruntfile中这样做,它不是必需的) 您可以在文件中定义它,这是您第一次放置这些变量,例如:

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

之后,如果需要将其作为env变量提供TEST_HOST:

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

如果您不设置变量,那么“http://localhost”将是默认变量。