Nightwatch: Saving variables for all tests to access

Created on 5 Apr 2014  路  9Comments  路  Source: nightwatchjs/nightwatch

I'm having to run through login every time (user name and password) to test features inside my app. In the past I've just injected logged in cookies obtained by the first test that requires login. I'd love a way to save variables available for all subsequent tests so they can check for the cookies and inject them in setUp (or login using the login screen if they haven't been initialized).

A persistent store would be awesome for runtime variables.

enhancement

Most helpful comment

create an object in the globals.
update this object while testing.

In this case,
I will create an object in globals,

tempObj:{
    cookies: ''
}

once you get a cookies from a test case, just use
browser.globals.tempObj.cookies = variable;

All 9 comments

maybe you can use globals for that? http://nightwatchjs.org/guide#settings-file

Can I save values into globals at run time? I wasn't sure if globals gets reset per test case.

I'm already use globals for keys and stuff, but those are values I know of before the tests start. I need to save values I discover while tests are executing.

no, it's not persistent. Such feature might be useful but not essential.

I meant only persistent while running so tests can share information. After the tests finish I wouldn't need the data anymore:

setUp: function(browser) {
  if (this.userCookies != null && this.userCookies.length > 0) {
    browser.loadCookies();
    listenMain.load(browser);
  } else {
    loginPage.load(browser);
    loginPage.loginWithEmail(browser.globals.active_username, browser.globals.active_password, browser);
    browser.saveCookies();
  }
}

This way I only have to execute the UI flow for logging in when I care to test it. Otherwise I'd rather skip to the bit I want to test.

The goal would be to save the cookies somewhere during execution and skip the UI stuff if possible.

Ok, I'll look into it.

:) Thanks

create an object in the globals.
update this object while testing.

In this case,
I will create an object in globals,

tempObj:{
    cookies: ''
}

once you get a cookies from a test case, just use
browser.globals.tempObj.cookies = variable;

@yumikohey Unfortunately I don't think that solves the problem :(

Any globals that you set during a test are not persisted for the next test.

For future reference, the above now works if you enable the persist_globals setting:

{
  "globals_path": "globals.js",
  "test_settings": {
    "default": {
      "persist_globals": true,
    }
  }
}
Was this page helpful?
0 / 5 - 0 ratings