使用Grunt启动Nightwatch时,网站服务器未启动

时间:2015-06-17 07:51:29

标签: javascript node.js selenium gruntjs nightwatch.js

我正在使用Nightwatch.js为网站运行系统测试。我想通过grunt运行它们来自动化测试。我的Gruntfile包含以下行:

...
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');

当我使用grunt nightwatch在终端中运行nightwatch时,它会启动selenium服务器并尝试运行测试,但网站服务器未启动。为什么?浏览器正在打开,但它只是说,连接是不可能的。我用Google搜索了但我找不到任何东西。我需要添加什么才能让grunt运行服务器?

这是我的nightwatch.json文件:

{
"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 个答案:

答案 0 :(得分:1)

您可以尝试grunt-express-server并在运行测试之前启动服务器,例如:

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

并修改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' ]);