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

守給你的承諾、 提交于 2019-11-28 08:30:04

问题


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?


回答1:


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.




回答2:


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);
  }
}


来源:https://stackoverflow.com/questions/27026363/reuse-the-browser-session-for-selenium-webdriver-for-nightwatch-js-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!