使用Gulp+nightwatch.js运行web服务器和selenium服务器



我正在尝试使用Gulp运行一些nightwatch.js e2e测试。

目前我必须做以下事情:

  1. 手动运行硒服务器

  2. 设置selenium-server-standalone-2.47.1.jarphantomjs.exe 的路径

  3. 手动运行web服务器

我的吞咽任务如下:

gulp.task("run-e2e-tests", function () {
  return gulp.src('')
    .pipe(nightwatch({
      configFile: "nightwatch.json",
      cliArgs: {
        env: "phantomjs"
      }
    }));
});

我的nightwatch.js配置如下:

{
  "src_folders" : [
    "bundle/e2e_test/"
  ],
  "output_folder": false,
  "selenium" : {
    "start_process" : false,
    "server_path" : "./selenium-binaries/selenium-server-standalone-2.47.1.jar"
  },
  "test_settings" : {
    "default" : {
      "silent": true,
      "screenshots" : {
        "enabled" : false
      },
      "desiredCapabilities": {
        "browserName": "phantomjs",
        "javascriptEnabled" : true,
        "acceptSslCerts" : false
      }
    },
    "phantomjs" : {
      "desiredCapabilities": {
        "browserName": "phantomjs",
        "javascriptEnabled" : true,
        "acceptSslCerts" : false,
        "phantomjs.binary.path" : "phantomjs.exe"
      }
    },
    "chrome" : {
      "desiredCapabilities": {
        "browserName": "chrome",
        "javascriptEnabled": true,
        "acceptSslCerts": false
      }
    }
  }
}

如果我将"start_process"更改为true,我会得到以下错误:

[17:10:04] Using gulpfile ~DesktopCPIC.UI.Webgulpfile.js
[17:10:04] Starting 'run-e2e-tests'...
[17:10:04] log file
[17:10:04] Starting nightwatch...
[CPIC E2e Test] Test Suite
==========================
Running:  CPIC integration
Error retrieving a new session from the selenium server
Error: connect ECONNREFUSED 127.0.0.1:4444
    at Object.exports._errnoException (util.js:874:11)
    at exports._exceptionWithHostPort (util.js:897:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)

Connection refused! Is selenium server started?
[17:10:06] 'run-e2e-tests' errored after 1.5 s
[17:10:06] Error in plugin 'gulp-nightwatch'
Message:
    nightwatch exited with code 1

e2e测试按预期运行,但作为CI过程的一部分,我想从Gulp自动运行web服务器和selenium服务器

我开始工作了!基本上,如果你通过npm安装selenium和phatomjs,就会出现问题,所以你必须手动下载它们,并在nightwatch.js配置文件中设置路径:

{
  "src_folders" : [
    "bundle/e2e_test/"
  ],
  "output_folder": false,
  "selenium" : {
    "start_process" : true,
    "server_path" : "./selenium-binaries/selenium-server-standalone-2.47.1.jar"
  },
  "test_settings" : {
    "default" : {
      "silent": true,
      "screenshots" : {
        "enabled" : false
      },
      "desiredCapabilities": {
        "browserName": "phantomjs",
        "javascriptEnabled" : true,
        "acceptSslCerts" : false
      }
    },
    "phantomjs" : {
      "desiredCapabilities": {
        "browserName": "phantomjs",
        "javascriptEnabled" : true,
        "acceptSslCerts" : false,
        "phantomjs.binary.path" : "./selenium-binaries/phantomjs.exe"
      }
    }
  }
}

为了运行web服务器并杀死它,我使用了3个任务和运行顺序:

gulp.task("run-e2e-tests", function (cb) {
  runSequence(
    "run-http-server",
    "run-nightwatch",
    "kill-http-server",
    cb);
});
gulp.task("run-nightwatch", function () {
  return gulp.src('')
  .pipe(nightwatch({
    configFile: "nightwatch.json",
    cliArgs: {
      env: "phantomjs"
    }
  }));
});
gulp.task("run-http-server", function () {
  return connect.server({
    port: 8888
  });
});
gulp.task("kill-http-server", function () {
  return connect.serverClose();
});

最新更新