User Agents

Web browsers and everyday web tech, explained

Fix Protractor Chrome Not Reachable Screenshot Errors

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

When Protractor fails with a "Chrome not reachable" error while taking screenshots, it usually means the ChromeDriver process died or the browser session was lost mid-test. This guide walks you through the most reliable fixes, from updating drivers to adjusting timeouts, so you can get stable screenshots again.

What You'll Need

  • Access to your Protractor configuration file (usually conf.js or protractor.conf.js)
  • Command line access to your project directory
  • Node.js and npm installed (to update packages)
  • Chrome browser installed on the machine running the tests

Step-by-Step

  1. Update ChromeDriver to match your Chrome version. The most common cause of "Chrome not reachable" is a version mismatch. Open your terminal and run webdriver-manager update (if you use Protractor's built-in manager) or npm install --save-dev chromedriver@latest. Check your Chrome version by visiting chrome://version and ensure the driver major version matches.
  2. Set explicit waits before taking screenshots. Often the screenshot is attempted while the page is still loading or the browser is busy. Add a browser.wait() with an expected condition (e.g., waitForAngular() or visibility of an element) before the screenshot call. For example:
    await browser.wait(ExpectedConditions.visibilityOf(element), 5000);
    Then call browser.takeScreenshot().
  3. Increase Protractor's timeouts. In your config file, set getPageTimeout and allScriptsTimeout to higher values, such as 30000 (30 seconds). Also add jasmineNodeOpts: { defaultTimeoutInterval: 60000 } if you use Jasmine. This prevents the session from being torn down prematurely.
  4. Disable Chrome's crash recovery and background mode. Add these Chrome options to your config's capabilities:
    capabilities: {
      browserName: 'chrome',
      chromeOptions: {
        args: ['--disable-background-networking', '--disable-client-side-phishing-detection', '--disable-default-apps', '--disable-extensions', '--disable-sync', '--no-first-run', '--no-default-browser-check']
      }
    }
    This stabilizes the browser session.
  5. Run tests in headless mode. If you're on a server or CI, headless Chrome is more stable. Add args: ['--headless', '--disable-gpu', '--window-size=1920,1080'] under chromeOptions. Note that headless mode may behave slightly differently, but it often eliminates the "not reachable" error.
  6. Use a fresh driver instance per test. In your onPrepare function, restart the browser session to clear any stale state. For example:
    onPrepare: function() {
      browser.restart();
    }
    This ensures each test starts clean.
  7. Add error handling around the screenshot. Wrap your screenshot call in a try-catch block. If it fails, retry after a short delay. This doesn't fix the root cause but prevents the whole test suite from crashing. Example:
    async function takeScreenshot(name) {
      try {
        await browser.takeScreenshot().then(function(png) {
          // save png
        });
      } catch (e) {
        console.log('Screenshot failed, retrying...');
        await browser.sleep(2000);
        await browser.takeScreenshot().then(function(png) {
          // save png
        });
      }
    }
  8. Update Protractor and related packages. Run npm update protractor and npm update jasmine (or your test framework). Old versions have known bugs with ChromeDriver.

Common Mistakes

  • Ignoring the ChromeDriver version. Many developers update Chrome but forget the driver. Always check the mismatch.
  • Taking screenshots immediately after navigation. The page may still be loading. Always wait for Angular or a specific element.
  • Using afterEach for screenshots without checking if the browser is alive. If the test failed and the browser crashed, the screenshot will fail. Check browser.driver.isPresent() first.
  • Setting too-short timeouts. If the timeout is lower than the time it takes to load a heavy page, the session may be killed.
  • Running multiple tests in parallel without enough resources. Each Chrome instance uses significant memory. Reduce parallelism or increase memory.

Tips

  • Use a helper function for screenshots. Centralize screenshot logic so you can add retries and logging in one place.
  • Log the browser session ID. When the error occurs, log browser.driver.getSession() to see if it's null.
  • Run a single test to reproduce. Narrow down to the exact test that fails. It's often a specific page or action that triggers the crash.
  • Keep ChromeDriver and Chrome on the same machine. If you're using a remote Selenium server, ensure the server's Chrome matches the driver.
  • Consider using browser.driver.manage().window().setSize() to set a consistent viewport. Some responsive pages behave oddly with default sizes.

Troubleshooting

If the error persists after following the steps:

  • Check the ChromeDriver logs. Run your test with --verbose or set loggingPrefs in capabilities to capture browser logs. Look for lines like "ChromeDriver did not respond" or "session deleted because of page crash".
  • Try a different browser. Temporarily switch to Firefox to see if the issue is Chrome-specific. If Firefox works, it's a Chrome/ChromeDriver issue.
  • Kill orphaned Chrome processes. On Linux/macOS run pkill chrome; on Windows use Task Manager. Stale processes can lock resources.
  • Check if the test machine has enough memory. Chrome is memory-hungry. Run free -m (Linux) or Task Manager (Windows) to see if memory is exhausted.
  • Disable hardware acceleration. Add --disable-gpu to Chrome options. Sometimes GPU issues cause crashes.
  • Update your OS and Chrome. Outdated OS drivers can cause instability.

Frequently Asked Questions

Why does the error only happen on screenshots, not on normal actions?

Screenshots often occur at the end of a test or after a failure, when the browser may already be in a bad state. Also, the screenshot call itself can trigger a crash if the browser is unresponsive.

Does headless mode fix the issue?

Often yes, because headless mode is more stable on servers and reduces resource usage. But it's not a guaranteed fix; you still need to align versions and wait properly.

Can I use a different screenshot library?

Yes, you can use webdriver-screenshot or screen-capture libraries, but they all use the same underlying WebDriver protocol, so the root cause remains.

How do I update ChromeDriver if I'm using Protractor's built-in manager?

Run ./node_modules/.bin/webdriver-manager update in your project root. This downloads the latest driver that matches your Chrome version.

Why does the error happen intermittently?

Intermittent crashes are often due to resource contention (memory/CPU) or timing issues. Increasing timeouts and adding retries usually helps.

Is it safe to ignore the error and continue?

No, because the screenshot is likely missing, and the test may have failed anyway. It's better to fix the underlying issue.