简体   繁体   中英

How to create new browser instance with Nightwatch.js

I'm trying to test with nightwatch that when a user logs in while already being logged in in a different browser, it automatically logs you out from the first session.

But I have no idea how to create a new browser instance.

That's how my code looks like:

// For authoring Nightwatch tests, see // http://nightwatchjs.org/guide#usage

module.exports = {

'Landing Page': function (browser) {
  // automatically uses dev Server port from /config.index.js
  // default: http://localhost:8080
  // see nightwatch.conf.js
    const devServer = browser.globals.devServerURL;

    browser
        .url(devServer)
        .waitForElementVisible('#app', 5000)
        .assert.urlContains('login')
        .assert.title('Customer Relation Manager')
        .assert.elementPresent('.login')
        .assert.containsText('.login-header', 'Log into CRM')
},

'A User not logged in will be redirected to login page': function (browser) {
    browser
        .url('http://localhost:8080/cust')
        .pause(1000)
        .assert.urlContains('login')
},

'A User can login': function (browser) {
    browser
        .setValue('input[type=text]', '********')
        .setValue('input[type=password]', '*******')
        .click('#submit')
        .pause(1000)
        .assert.urlContains('dashboard')
        .assert.containsText(".header-logo", "CRM V3")
},

'A User who is logged in cannot access the Login page again': function (browser) {
    browser
        .url('http://localhost:8080/login')
        .pause(1000)
        .assert.urlContains('dashboard')
        // .end();
},

'A user can refresh the page and redirect to the same page': function (browser) {
    browser
        .refresh()
        .pause(1000)
        .assert.urlContains('dashboard')
},

'A User can logout': function (browser) {
    browser
        .click('.header-user-info')
        .pause(1000)
        .assert.urlContains('login')
        .assert.containsText('.login-header', 'Log into CRM')
        .end();
  }

}

Add a beforeEach hook in your tests and add Deletecookies to it. So for every tests a fresh browser instance would be launched.

beforeEach: function (browser) {
    browser.maximizeWindow();
    browser.deleteCookies();
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM