Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse the browser session for Selenium WebDriver for Nightwatch.js tests

I need to write multiple tests (e.g. login test, use application once logged in tests, logout test, etc.) and need them all to be in separate files. The issue I run into is after each test, at the beginning of the next test being run, a new browser session start and it is no longer logged in due to the new session, so all my tests will fail except the login test.

So, is there a way to use the same browser session to run all of my tests sequentially without having to duplicate my login code? Sorry if this is a repost but I have searched and researched and not found any answers.

OR, is there a way to chain the test files somehow? Like having one file that you run that just calls all the other test files?

like image 944
David C Avatar asked Nov 19 '14 20:11

David C


1 Answers

Using this function to chain together files:

extend = function(target) {
    var sources = [].slice.call(arguments, 1);
    sources.forEach(function (source) {
        for (var prop in source) {
            target[prop] = source[prop];
        }
    });
    return target;
}

and adding files to this master file like this:

require("./testName.js");
module.exports = extend(module.exports,testName);

and having the test file look like this:

testName = {
    "Test" : function(browser) {

        browser
            // Your test code
    }
};

allowed me to have one file that could link all the tests to, and keep the same browser session the entire time. It runs the tests in the order you require them in the master file and if you do not call browser.end() until the last test is finished it will use one browser window for all tests.

like image 150
David C Avatar answered Sep 27 '22 16:09

David C