如何将Gruntfile.js中的参数传递到webdriverio规范

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

本文关键字:参数传递 webdriverio 规范 Gruntfile js      更新时间:2023-09-26

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

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

/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

  • 咕哝:v0.4.5

  • 网络驱动程序管理器:3.0.0

  • grunt网络驱动程序:0.4.8

好的,我解决了你的问题:)

这些"主机"answers"端口"参数是预定义的参数,用于另一个目的(将执行测试的主机和端口,您正在重新定义端口-这就是它们失败的原因,例如这里-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;

之后,如果需要TEST_HOST作为env变量,您只需提供它:

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

如果不设置变量,则"http://localhost"将是默认值。