配置 Nightwatch 以在 Chrome 的独立实例上进行 运行 测试

Configuring Nightwatch to run tests on a standalone instance of Chrome

我正在尝试设置 Nightwatch,这样我就不必使用 Selenium 而是直接指向 Linux 上的 Chrome 实例,但我似乎无法连接到chromedriver 实例。

nightwatch.json

{
  "src_folders" : ["tests"],
  "output_folder" : "reports",
  "custom_commands_path" : "",
  "custom_assertions_path" : "",
  "page_objects_path" : "",
  "globals_path" : "globals.js",

  "selenium" : {
    "start_process" : false,
    "cli_args" : {
      "webdriver.chrome.driver" : "./chromedriver"
    }
  },

  "test_settings" : {
    "default" : {
      "selenium_port"  : 9515,
      "selenium_host"  : "127.0.0.1",
      "silent": true,
      "screenshots" : {
        "enabled" : false,
        "path" : ""
      },
      "desiredCapabilities": {
        "browserName": "chrome",
    "acceptSslCerts": true
      }
    }
  }
}

尝试 运行 一个简单的测试套件后,出现以下错误

 ~/scripts/TestNightwatch$ nightwatch 

 [Test] Test Suite
 =====================

 Running:  Demo test Google Error processing the server response:  
 unknown command: wd/hub/session

 Error retrieving a new session from the selenium server

 Connection refused! Is selenium server started? { value: -1, error:
 'Unexpected token u in JSON at position 0' }

我没有正确配置 nightwatch 以连接到 Chrome?我是否遗漏了任何拼图?

提前致谢!

globals.js

var chromedriver = require('chromedriver');

module.exports = {
  before : function(done) {
    chromedriver.start();

    done();
  },

  after : function(done) {
    chromedriver.stop();

    done();
  }
}

问题可能是 Chromedriver 需要 / 而不是 /wd/hub 的命令。要修复,请使用 --url-base=/wd/hub

启动 Chromedriver

来源:Selenium WebDriverJS, cannot build webdriver for Chrome and https://github.com/webdriverio/webdriverio/issues/113

您需要在外部全局文件中启动 chromedriver。看起来你的配置是正确的。只需在外部全局模块中设置 beforeafter 函数即可启动 chromedriver。

before : function(done) {
  chromedriver.start();

  done();
},

after : function(done) {
  chromedriver.stop();

  done();
}

查看 Getting Started 指南了解更多详情。