如何正确调用Nightwatch.runTests?遇连接拒绝错误求助
Let's break down why you're hitting this connection refused error and walk through actionable fixes:
1. Fix the start_process Misconfiguration in Your First Setup
Looking at your initial settings object, you had selenium.start_process: false—this tells Nightwatch to connect to an already-running Selenium Server on port 4444, but you weren't manually starting one. That's the immediate cause of the ECONNREFUSED error.
Even though you later set start_process: true, there were other configuration issues blocking it from working.
2. Correct Configuration Hierarchy in Your Second Setup
Your revised settings has a critical nesting error in the selenium test environment:
// ❌ Wrong: nested "selenium" object inside the environment selenium: { selenium: { start_process: true, port: 4444, // ... rest of config } }
Nightwatch expects Selenium/webdriver settings to be directly under the test environment, not nested. Fix it like this:
// ✅ Correct: flat structure selenium: { start_process: true, port: 4444, server_path: seleniumServer.path, cli_args: { 'webdriver.chrome.driver': chromedriver.path }, desiredCapabilities: { browserName: 'chrome', chromeOptions: { w3c: true } } }
3. Explicitly Specify the Test Environment When Running Tests
By default, Nightwatch uses the default test environment. If your default setup is configured to use chromedriver directly (no Selenium Server), make sure you're either:
- Using that
defaultenvironment correctly, or - Explicitly passing the environment you want to use when calling
runTests:
Nightwatch.runTests('tests', { ...settings, test_env: 'chrome' // Use the chrome environment you defined }).then(function() { // Tests finished }).catch(function(err) { // Handle error });
4. Simplify Configuration to Debug
If you're still stuck, strip down your config to the absolute minimum to rule out conflicts. This setup skips Selenium Server entirely and uses chromedriver directly:
const Nightwatch = require('nightwatch'); const chromedriver = require('chromedriver'); const minimalSettings = { src_folders: ['tests'], test_settings: { default: { desiredCapabilities: { browserName: 'chrome', chromeOptions: { w3c: false } }, webdriver: { start_process: true, server_path: chromedriver.path, port: 9515 } } } }; Nightwatch.runTests('tests', minimalSettings) .then(() => console.log('Tests completed successfully!')) .catch(err => console.error('Test error:', err));
If this works, gradually add back your original config options (output folders, screenshots, etc.) to find what's causing the conflict.
5. Verify Dependency Compatibility
- Ensure your
chromedriverversion matches your installed Chrome browser version (runchromedriver --versionto check, and update if needed). - Nightwatch 1.3.6 is compatible with Selenium Server 3.141.59, so that combination should work once your config is fixed.
内容的提问来源于stack exchange,提问作者xserjeo

