9

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?

David C
  • 389
  • 1
  • 5
  • 13
  • Not familiar with Nightwatch.js but I am familiar with the login/logout session issue with selenium test cases. I'd suggest taking a look at [nightwatch test tags](http://nightwatchjs.org/guide#test-tags) and/or the [test hooks](http://nightwatchjs.org/guide#setup-teardown) found below that in the documentation. – Arthur Weborg Nov 19 '14 at 21:13

2 Answers2

12

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.

David C
  • 389
  • 1
  • 5
  • 13
  • wouldn't this break the tag functionality? – SgtPooki Nov 03 '17 at 01:26
  • To add other test... the test file should have the same "testName" name or must be different? because this line in the master file module.exports = extend(module.exports,testName); should be written different also. – Takatalvi Mar 08 '21 at 16:41
4

Reuse of session is not good idea as you may run tests in different oreder, but You could place login code into before function or even extract it into custom commands.

Example: https://github.com/dimetron/backbone_app/blob/master/nightwatch/custom-commands/login.js

1 - In nightwatch config add

"custom_commands_path" : "nightwatch/custom-commands",

2 - Create custom-commands/login.js

exports.command = function(username, password, callback) {
    var self = this;
    this
        .frame(null)
        .waitForElementPresent('input[name=username]', 10000)
        .setValue('input[name=username]', username)
        .waitForElementPresent('input[name=password]', 10000)
        .setValue('input[name=password]', password)
        .click('#submit');

    if( typeof callback === "function"){
        callback.call(self);
    }
    return this; // allows the command to be chained.
};

3 - Test code - Before using .login(user, apssword)

 module.exports = {

  before: function(browser) {
    console.log("Setting up...");
    browser
      .windowSize('current', 1024, 768)
      .url("app:8000/") 
      .waitForElementVisible("body", 1000)
      .login('user', 'password')
  },

  after : function(browser) {
    browser.end()
    console.log("Closing down...");
  },

  beforeEach: function(browser) {
    browser
      .pause(2000)
      .useCss()
  },

  "Test 1": function(browser) {
    browser
      .assert.containsText("#div1", "some tex")
      .pause(5000);
  },

  "Test 2": function(browser) {
    browser
      .assert.containsText("#div2", "some text")
      .pause(5000);
  }
}
  • Multiple logins is still unnecessary and wasting a lot of time for suites with a large number of tests... unless your appstate is actually different between each login, which means you have bigger problems. – SgtPooki May 23 '17 at 21:23
  • There are also frameworks (like rails) where you can enable disabling of accounts after multiple sessions are detected. It's both more efficient and more like the typical browser interactions to have a single session – Dockstar Sep 02 '21 at 18:21