如何用夜视仪自动启动chromedrivre?

时间:2017-04-20 14:14:36

标签: selenium-chromedriver nightwatch.js

配置

{
    "src_folders": [
        "test/e2e"
    ],
    "selenium": {
        "start_process": false,
        "cli_args": {
            "webdriver.chrome.driver": "./node_modules/.bin/chromedriver"
        }
    },
    "test_settings": {
        "default": {
            "selenium_port": 9515,
            "selenium_host": "localhost",
            "default_path_prefix": "",
            "desiredCapabilities": {
                "browserName": "chrome",
                "javascriptEnabled": true,
                "acceptSslCerts": true,
                "chromeOptions": {
                    "args": [
                        "--no-sandbox"
                    ]
                }
            }
        }
    }
}

测试

module.exports = {
    'index page': function (client) {
        client
            .url('http://localhost:8080')
            .waitForElementVisible('body', 1000)
            .assert.title('Apidae')
            .end();
    }
};

运行

为了使其工作,我在不同的标签中运行两个命令: 1.开始chromedriver

./node_modules/.bin/chromedriver

2。开始我的测试:

./node_modules/.bin/nightwatch

问题

如何修改我的nightwatch配置以自动启动chromedriver

2 个答案:

答案 0 :(得分:1)

'selenium': { // downloaded by selenium-download module (see readme)
    'start_process': true, // tells nightwatch to start/stop the selenium process
    'server_path': './node_modules/selenium/lib/runner/selenium-server-standalone-2.20.0.jar',
    'host': '127.0.0.1',
    'port': 4444, // standard selenium port
    'cli_args': { // chromedriver is downloaded by selenium-download (see readme)
      'webdriver.chrome.driver': './node_modules/nightwatch/bin/chromedriver'
    }

并将其添加到test_settings

  'chrome': {
      'desiredCapabilities': {
        'browserName': 'chrome',
        'javascriptEnabled': true // turn off to test progressive enhancement
      }

这对我有用。

答案 1 :(得分:1)

根据@Hikaryu的回答,请 upvote他的回答,这是我的完整配置:

的package.json

我在启动测试之前检查了selenium(可能是一个单独的脚本)。

"scripts": {
  "test:e2e": "node ./test/selenium-download.js; ./node_modules/.bin/nightwatch"
},
"devDependencies": {
  "nightwatch": "0.9.x",
  "selenium-download": "^2.0.10",
}

检查硒存在脚本

基于selenium-download示例:

var selenium = require('selenium-download');

selenium.ensure(__dirname + '/bin', function (error) {
  if (error) console.error(error.stack);
  process.exit(0);
});

nightwatch.json

我更新了一些键/值,删除了default_path_prefix字段。

{
    "src_folders": [
        "test/e2e"
    ],
    "selenium": {
        "start_process": true,
        "server_path": "./test/bin/selenium.jar",
        "host": "127.0.0.1",
        "port": 4444,
        "cli_args": {
            "webdriver.chrome.driver": "test/bin/chromedriver"
        }
    },
    "test_settings": {
        "default": {
            "desiredCapabilities": {
                "browserName": "chrome",
                "javascriptEnabled": true,
                "acceptSslCerts": true,
                "chromeOptions": { "args": [ "--no-sandbox" ] }
            }
        }
    }
}

测试目录

./test/
├── bin
│   ├── chromedriver
│   └── selenium.jar
├── *.tests.js
└── e2e
    └── index.js