Search code examples
seleniumselenium-webdriverselenium-chromedrivernightwatch.js

How do I keep the browser open on a failed test using Selenium+Nightwatch+Chromedriver?


I'm using Nightwatch, Selenium, and Chrome Driver to conduct automated UI testing. Sometimes there are test failures on any number of remote environments. To debug the failures locally, I would like to keep the browser open on test failure.

I used to be able to do this using config settings in nightwatch.json and nightwatch.conf.js, but I lost my config settings cheat-sheet in a hard drive failure, and despite my best efforts on google I can't seem to find the right combination again.

If I remember correctly, it wasn't any kind of keepBrowserOpen: true type flag. If memory serves, it was some type of timeout set to a silly high number combined with something else like detachDriver. I've checked the Selenium documentation with specific focus on the Chrome Driver Options, and the list of Chrome Driver options, but I haven't found any working combinations.

What am I missing?

Here is my nightwatch.json

{
    "output_folder": "tests/automation/reports",
    "end_session_on_fail": false,
    "test_settings": {
        "default": {
            "selenium_port": 4444,
            "screenshots": {
                "enabled": true,
                "path": "tests/automation/reports/screenshots"
            },
            "javascriptEnabled": true,
            "acceptSslCerts": true,
            "acceptInsecureCerts": true,
            "globals": {
                "waitForConditionTimeout": 10000
            }
        },
        "chrome": {
            "extends": "default",
            "desiredCapabilities": {
                "browserName": "chrome",
                "javascriptEnabled": true,
                "acceptSslCerts": true,
                "acceptInsecureCerts": true,
                "chromeOptions": {
                    "args": [
                        "--no-sandbox",
                        "--disable-gpu"
                    ]
                }
            }
        }
    }
}

And my nightwatch.conf.js

const geckoDriver = require('geckodriver');
const chromeDriver = require('chromedriver');
const seleniumServer = require('selenium-server');

module.exports = (function (settings) {
    settings.selenium = {
        start_process: true,
        server_path: seleniumServer.path,
        log_path: 'tests/automation/reports/',
        host: 'localhost',
        port: 4444,
        cli_args: {
            'webdriver.chrome.driver': chromeDriver.path,
            'webdriver.gecko.driver': geckoDriver.path,
            'webdriver.edge.driver': 'C:\\windows\\system32\\MicrosoftWebDriver.exe',
            'webdriver.port': '4444'
        }
    };

    return settings;
})(require('./nightwatch.json'));

Selenium Version is 2.35.1


Solution

  • This is not an elegant solution but close to what you are looking for. Basically, we can check the value of browser.currentTest.results.errors in afterEach() and in case if the value is more than 0, then we can just pause the test there, keeping the browser session alive.

    afterEach: function(browser) {
        if(browser.currentTest.results.errors > 0) {
            browser.pause()
        }