简体   繁体   中英

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

I would like to parameterize my webdriverio specs from the Gruntfile.js. 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.

Reading the Source Labs example from https://www.npmjs.com/package/grunt-webdriver#overview I set the host and the port in the options. 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:

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: v0.4.5

  • webdriver-manager: 3.0.0

  • 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). 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:

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:

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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