Skip to content

Log in parallel modes - #4440

Merged
garg3133 merged 26 commits into
nightwatchjs:mainfrom
shubhamkd:log-in-parallel-modes
May 25, 2026
Merged

Log in parallel modes#4440
garg3133 merged 26 commits into
nightwatchjs:mainfrom
shubhamkd:log-in-parallel-modes

Conversation

@shubhamkd

@shubhamkd shubhamkd commented Jan 21, 2026

Copy link
Copy Markdown
Contributor

This PR fixes missing webdriver / chromedriver log files when Nightwatch runs tests in parallel (worker-threads or child-process workers). Previously worker-mode disabled the log sink, causing writeLogFile() to no-op and resulting in no log files for worker-hosted driver processes.

Root cause

  • BaseService.needsSinkProcess() returned false in workers when Concurrency.isWorker() was true, so BaseService.createService() set [webdriver.log_path = false] for workers. That made getLogPath() return null and writeLogFile() return early and hence no file was written.
  • No Logs were generated as shown in the below screenshot
    No logs generated for multiple workers

Changes Implemented

Parallel Mode-Specific Logging Behavior

When parallel mode is detected (process.env.__NIGHTWATCH_PARALLEL_MODE === '1'), unique log files are created for each Chromedriver instance spun up:

  • Without log_file_name:
    Log files are named as <module_key>_<timestamp>.log, where <timestamp> is dynamically generated.

  • With log_file_name:
    User-configured log file names (e.g., my_custom_log) are suffixed with timestamp.
    (e.g., my_custom_log_<timestamp>.log).

Fallback to Default Behavior for Non-Parallel Execution

If parallel mode is not enabled or only a single thread is used:

  • The repository’s original logging mechanism is preserved.
  • No disruption to existing workflows.
  • Outputs are aggregated and logged exactly as originally implemented.

Files changed

  • lib/transport/selenium-webdriver/index.js
  • lib/transport/selenium-webdriver/service-builders/base-service.js

How to test (locally)

  1. Use a Nightwatch config with test_workers enabled .
  2. Run parallel tests: npx nightwatch tests/guide/ --env chrome --workers=2
  3. Expect one log file per chromedriver spun up in webdriver.log_path (defaults to ./logs):
  4. Inspect file contents ls -l logs/ and head -n 50 logs/<file>.

Notes

  • Single-process behavior is unchanged.
  • The per-worker sink is only created where sink support is available (hasSinkSupport() still respected).

After Changes

  • I ran ./bin/nightwatch examples/tests/ecosia.js examples/tests/duckDuckGo.js --env chrome
  • Without log_file_name:
Screenshot 2025-12-22 at 9 08 39 PM

-With log_file_name:
Screenshot 2025-12-22 at 9 13 53 PM

@github-actions

github-actions Bot commented Jan 21, 2026

Copy link
Copy Markdown

Status

  • ✅ Type files updated!

@shubhamkd
shubhamkd force-pushed the log-in-parallel-modes branch from 5709f17 to 61c1c0c Compare January 27, 2026 12:00
@shubhamkd
shubhamkd force-pushed the log-in-parallel-modes branch from e2e7bf9 to 2eb0a92 Compare January 31, 2026 19:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes missing webdriver/chromedriver log files when Nightwatch runs tests in parallel mode (using worker threads or child processes). Previously, the log sink was disabled in worker processes, causing writeLogFile() to no-op and resulting in no log files for worker-hosted driver processes.

Changes:

  • Modified needsSinkProcess() to respect a new retain_logs_in_worker configuration option, allowing logs to be generated in parallel workers
  • Added timestamp suffixes to log file names in worker contexts using process.hrtime.bigint() to ensure unique file names
  • Added comprehensive tests for the new concurrency logging behavior

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 10 comments.

File Description
lib/transport/selenium-webdriver/service-builders/base-service.js Modified needsSinkProcess() to check retain_logs_in_worker setting and allow sink process creation in workers; minor formatting changes
lib/transport/selenium-webdriver/index.js Added timestamp suffix to log file names when running in worker mode to prevent file name collisions
test/src/service-builders/testSeleniumServer.js Added test to verify per-worker log file path includes timestamp when running as worker
test/src/service-builders/testBaseServiceConcurrency.js New test file with comprehensive tests for sink process creation behavior in different concurrency scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/transport/selenium-webdriver/index.js
Comment thread test/src/service-builders/testSeleniumServer.js Outdated
Comment thread lib/transport/selenium-webdriver/index.js
Comment thread test/src/service-builders/testSeleniumServer.js Outdated
Comment thread test/src/service-builders/testBaseServiceConcurrency.js Outdated
Comment thread lib/transport/selenium-webdriver/service-builders/base-service.js Outdated
Comment thread lib/transport/selenium-webdriver/service-builders/base-service.js Outdated
Comment thread lib/transport/selenium-webdriver/service-builders/base-service.js Outdated
Comment thread test/src/service-builders/testSeleniumServer.js Outdated
Comment thread test/src/service-builders/testSeleniumServer.js
Comment thread test/src/service-builders/testSeleniumServer.js Outdated
Comment thread test/src/service-builders/testSeleniumServer.js Outdated
Comment thread test/src/service-builders/testBaseServiceConcurrency.js Outdated
Comment thread test/src/service-builders/testBaseServiceConcurrency.js Outdated
Comment thread test/src/service-builders/testBaseServiceConcurrency.js Outdated
Comment thread test/src/service-builders/testBaseServiceConcurrency.js Outdated
Comment thread test/src/service-builders/testBaseServiceConcurrency.js Outdated
Comment thread lib/transport/selenium-webdriver/service-builders/base-service.js Outdated
Comment thread test/src/service-builders/testSeleniumServer.js Outdated
Restore and override Concurrency.isWorker deterministically per test case so worker/non-worker assertions are stable on Windows and other platforms.

Made-with: Cursor
@shubhamkd
shubhamkd requested a review from garg3133 April 3, 2026 10:17
shubhamkd and others added 4 commits April 3, 2026 16:06
Mock the exact concurrency module path loaded by BaseService so isWorker stubbing is reliable with clean module cache on Windows.

Made-with: Cursor
Concurrency.isWorker() reads __NIGHTWATCH_PARALLEL_MODE on each call, so setting it avoids mockery/cache issues across platforms.

Made-with: Cursor
@garg3133

Copy link
Copy Markdown
Member

@shubhamkd This PR looks good now. The only thing remaining now is the two open Copilot comments (TypeScript type and default value addition), and the documentation for this feature.

garg3133 and others added 2 commits April 13, 2026 15:39
Add the option to defaults.js and the WebdriverOptions TypeScript
interface so users can discover it and TS consumers don't get type
errors when setting it.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@garg3133

Copy link
Copy Markdown
Member

LGTM! Thank you for working on this @Samriddhi-35 @shubhamkd

@garg3133
garg3133 merged commit eaefd59 into nightwatchjs:main May 25, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants