Skip to content

Fix #4256 : write webdriver/chromedriver logs per worker in parallel mode - #4424

Closed
Samriddhi-35 wants to merge 7 commits into
nightwatchjs:mainfrom
Samriddhi-35:fix/worker-logs-per-worker
Closed

Fix #4256 : write webdriver/chromedriver logs per worker in parallel mode#4424
Samriddhi-35 wants to merge 7 commits into
nightwatchjs:mainfrom
Samriddhi-35:fix/worker-logs-per-worker

Conversation

@Samriddhi-35

@Samriddhi-35 Samriddhi-35 commented Dec 11, 2025

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 and a generated random string of length 10
    (e.g., my_custom_log_<timestamp>_abcd1234ef.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

@CLAassistant

CLAassistant commented Dec 11, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

Copy link
Copy Markdown

Status

  • ❌ No modified files found in the types directory.
    Please make sure to include types for any changes you have made. Thank you!.

…arallel mode and use timestamp instead of worker id >
Comment thread lib/transport/selenium-webdriver/index.js Outdated
Comment thread lib/transport/selenium-webdriver/index.js Outdated
Comment thread lib/transport/selenium-webdriver/index.js Outdated
Comment thread lib/transport/selenium-webdriver/index.js Outdated
Comment thread lib/transport/selenium-webdriver/service-builders/base-service.js Outdated
Comment thread lib/transport/selenium-webdriver/index.js
Comment thread lib/transport/selenium-webdriver/service-builders/base-service.js Outdated
Comment thread lib/transport/selenium-webdriver/service-builders/base-service.js Outdated
@garg3133
garg3133 requested a review from Copilot December 23, 2025 16:36

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 an issue where webdriver/chromedriver log files were not being generated when Nightwatch runs tests in parallel mode (using worker threads or child processes). The fix enables log file creation in worker processes and ensures each worker generates a uniquely-named log file using high-resolution timestamps.

Key Changes:

  • Modified needsSinkProcess() to allow log retention in worker processes when retain_logs_in_worker is enabled
  • Added timestamp-based unique naming for log files in parallel mode to prevent conflicts between workers

Reviewed changes

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

File Description
lib/transport/selenium-webdriver/service-builders/base-service.js Updated needsSinkProcess() to support log retention in workers via retain_logs_in_worker config option; minor formatting adjustments
lib/transport/selenium-webdriver/index.js Added logic to append timestamps to log file names in worker mode for unique per-worker log files; imported Concurrency module

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

async createDriverService({options, moduleKey, reuseBrowser = false}) {
try {
moduleKey = this.settings.webdriver.log_file_name || moduleKey || '';
if (Concurrency.isWorker) {

Copilot AI Dec 23, 2025

Copy link

Choose a reason for hiding this comment

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

Concurrency.isWorker is a static method and should be called as a function with parentheses. Currently, it's being accessed as a property, which will always evaluate to a truthy value (the function object itself) rather than executing the function to check if the current process is a worker. This means the timestamp logic will execute in all scenarios, not just in worker mode as intended. Change to Concurrency.isWorker().

Suggested change
if (Concurrency.isWorker) {
if (Concurrency.isWorker()) {

Copilot uses AI. Check for mistakes.
return !Concurrency.isWorker();
const {retain_logs_in_worker} = this.settings.webdriver || {};

return !Concurrency.isWorker() || retain_logs_in_worker ;

Copilot AI Dec 23, 2025

Copy link

Choose a reason for hiding this comment

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

There is a trailing space after retain_logs_in_worker before the semicolon. Remove the extra whitespace for consistent code formatting.

Suggested change
return !Concurrency.isWorker() || retain_logs_in_worker ;
return !Concurrency.isWorker() || retain_logs_in_worker;

Copilot uses AI. Check for mistakes.

@garg3133 garg3133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! Can we please add tests here?

This feature would also need to be documented.

@garg3133

garg3133 commented May 25, 2026

Copy link
Copy Markdown
Member

Completed in #4440 after rebasing the changes from this branch. Thanks for working on this!

@garg3133 garg3133 closed this May 25, 2026
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