When starting Nightwatch with Grunt, the website server is not started

1.2k views Asked by At

I am using Nightwatch.js to run system tests for a website. I want to automate the tests by running them via grunt. My Gruntfile contains these lines:

...
var nightwatch = require('nightwatch');
nightwatch.initGrunt(grunt);
...
nightwatch: {
    options: {
        standalone: true,
        test_settings: {
            "default": {
                "launch_url": "http://localhost",
                "selenium_port": 4444,
                "selenium_host": "localhost",
                "silent": true,
                "screenshots": {
                    "enabled": false,
                    "path": ""
                },
                "desiredCapabilities": {
                    "browserName": "firefox",
                    "javascriptEnabled": true,
                    "acceptSslCerts": true
                }
            }
        },
        "chrome" : {
            "desiredCapabilities": {
                "browserName": "chrome",
                "javascriptEnabled": true,
                "acceptSslCerts": true
            }
        }
    }
},
...
grunt.loadNpmTasks('grunt-nightwatch');

When I run nightwatch in the terminal with grunt nightwatch it starts the selenium server and tries to run the tests, but the website server is not being started. Why? The browser is being opened, but it just says, that a connection is not possible. I googled about it but I could not find anything. What do I have to add to make grunt run the server?

This is my nightwatch.json file:

{
"src_folders" : ["nightwatch/tests"],
"output_folder" : "nightwatch/reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",

"selenium" : {
    "start_process" : false,
    "server_path" : "",
    "log_path" : "",
    "host" : "127.0.0.1",
    "port" : 4444,
    "cli_args" : {
        "webdriver.chrome.driver" : "",
        "webdriver.ie.driver" : ""
    }
},

"test_settings" : {
    "default" : {
        "launch_url" : "http://localhost",
        "selenium_port"  : 4444,
        "selenium_host"  : "localhost",
        "silent": true,
        "screenshots" : {
            "enabled" : false,
            "path" : ""
      },
      "desiredCapabilities": {
          "browserName": "firefox",
          "javascriptEnabled": true,
          "acceptSslCerts": true
      }
},

"chrome" : {
    "desiredCapabilities": {
        "browserName": "chrome",
        "javascriptEnabled": true,
        "acceptSslCerts": true
    }
}
}
1

There are 1 answers

0
mido On BEST ANSWER

you can give grunt-express-server a try and start the server before running the test, for example:

npm install grunt-express-server --save-dev

and modify the gruntfile:

grunt.loadNpmTasks('grunt-express-server');
grunt.initConfig({
  express: {
    options: {
      // Override defaults here
    },
    dev: {
      options: {
        script: 'app.js'
      }
    }...


// and finally, in the test task, add this in the beginning.     
grunt.registerTask('test', [ 'express:dev', 'nightwatch' ]);