WebdriverIO Mocha框架下chromedriver报错ECONNREFUSED及会话请求失败求助
Hey there, let’s break down the two issues you’re facing with WebdriverIO and Mocha, step by step.
1. Fixing ERROR: connect ECONNREFUSED 127.0.0.1:4444 (Chromedriver Issues)
This error almost always means WebdriverIO can’t reach a running Chromedriver instance on port 4444. Try these targeted fixes:
Verify Chromedriver compatibility & manual startup
The #1 cause here is a mismatch between your Chrome browser version and Chromedriver version. Double-check they’re aligned, then download the exact Chromedriver version for your Chrome release if needed. Test manually starting it with:chromedriver --port=4444If you get version-related errors here, that’s your root problem.
Check WebdriverIO config for auto-start settings
In yourwdio.conf.js, make sure the Chromedriver service is set to auto-launch the driver (so you don’t have to run it manually):exports.config = { // ... other config services: ['chromedriver'], chromedriver: { start_process: true, // WebdriverIO handles launching Chromedriver port: 4444 // Match this to the port in your error } }If
start_processwas set tofalse, WebdriverIO expects you to run Chromedriver manually first.Free up port 4444
Another process might be hogging the port. On macOS, find the culprit with:lsof -i :4444Kill it using
kill -9 <PID>(replace<PID>with the process ID from the output), then restart your tests.
2. Resolving Session Request Failures (Firefox Session)
Looking at your logs, you’re trying to spin up a Firefox session but it’s failing. Here’s how to debug this:
Ensure GeckoDriver is properly configured
Firefox relies on GeckoDriver, not Chromedriver. Add the GeckoDriver service to your WebdriverIO config and update your capabilities to use modern syntax:exports.config = { // ... other config services: ['geckodriver'], // Add this if missing capabilities: [ { browserName: 'firefox', platformName: 'macOS', // Use `platformName` instead of the outdated `platform` browserVersion: 'your-firefox-version' // Replace with your actual Firefox version (avoid `latest` on older macOS like 10.12) } ] }Note:
desiredCapabilitiesis deprecated in newer WebdriverIO versions—switch tocapabilitiesinstead.Match GeckoDriver to your Firefox version
Older Firefox builds (especially those compatible with macOS 10.12) require specific GeckoDriver versions. Check the GeckoDriver release notes to find the right match for your installed Firefox.Simplify your capabilities
Your log shows extra fields likebuild,loggingPrefs, etc.—strip these down to the essentials first to rule out conflicting settings:capabilities: [ { browserName: 'firefox', platformName: 'macOS', acceptSslCerts: true, pageLoadStrategy: 'normal' } ]
General Troubleshooting Tips
- Update your WebdriverIO dependencies to compatible versions:
npm install webdriverio@latest @wdio/mocha-framework@latest @wdio/chromedriver-service@latest @wdio/geckodriver-service@latest - Enable debug logging in
wdio.conf.js(logLevel: 'debug') to get granular details about why the session is failing. - Clean and reinstall dependencies to rule out corrupt installs:
rm -rf node_modules package-lock.json npm install
内容的提问来源于stack exchange,提问作者user2641604

