clear cache in Nightmare.js (Electron)
Asked Answered
G

1

10

I'm using nightmare js to log into a site that sets a token in local storage. However, any future tests I run the user is already logged in. I'm guessing the local storage wasn't cleared. Is there any way to do this? My code in test.js

require('mocha-generators').install();

var Nightmare = require('nightmare');
var expect = require('chai').expect;

describe('test login', function() {
  var nightmare = Nightmare({show: true})

  after(function*() {
    yield nightmare.end();
  })

  it('should login given right credentials', function*() {
    this.timeout(50000);
    console.log("running test");
    var link = yield nightmare
      .goto('http://127.0.0.1:3000/login')
      .wait(1000)
      .type('.email-field', '[email protected]')
      .type('.password-field', 'password')
      .click('.login button')
      .wait(1000)

  });
})

I run the test using: mocha

the test runs fine and closes. However when I run again the user starts off as logged in. Is there anyway to clear the cache or local storage in nightmarejs?

Electron has a way to clear session info via session.clearCache (http://electron.atom.io/docs/v0.32.0/api/session/) but I don't know how to access the session object from nightmare.

Gnosticize answered 26/11, 2015 at 18:11 Comment(2)
Did you figure it out ? Just wondering. Thanks.Nary
@FelixG answered belowGnosticize
G
14

Alright figured it out, we can use Electrons 'web-preferences' property.

var nightmare = Nightmare({
  show: false,
  webPreferences: {
    partition: 'nopersist'
  }
});

when initiating nightmare use 'web-prefences' partition property to handle sessions. more info here: https://github.com/atom/electron/blob/master/docs/api/browser-window.md. The gist of is as follows:

the page will use a persistent session available to all pages in the app with the same partition. if there is no persist: prefix, the page will use an in-memory session.

so basically if you init nightmare as:

var nightmare = Nightmare({
  show: false,
  webPreferences: {
    partition: 'persist:derp'
  }
});

then the session will persist under 'derp', this can be helpful when you are testing features inside authenticated routes. (derp isn't significant, can be anything following persist:)

if you don't want session to persist don't use persist:. I use nopersist but this could be any string that isn't prefixed by persist:

EDIT: show:false isn't significant to session, it just shows what electron (which nightmare uses) is doing if you set show:true, but this line can be removed

Gnosticize answered 9/12, 2015 at 3:58 Comment(5)
It seems the property name is actually webPreferences, not web-preferencesGlyphography
depends on the version of nightmare, they might have updated it for newer version.Gnosticize
Maksim hi5! webPreferences it isHartill
@Gnosticize update your answer. The correct property is now webPreferences.Hying
i dont get it, By default nightmare will create an in-memory partition for each instance. This means that any localStorage or cookies or any other form of persistent state will be DESTROYED when nightmare is ended.Stiffen

© 2022 - 2024 — McMap. All rights reserved.