Saad Saad - 1 year ago 241
Javascript Question

clear cache in Nightmare.js (Electron)

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


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*() {
console.log("running test");
var link = yield nightmare
.type('.email-field', '')
.type('.password-field', 'password')
.click('.login button')


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
( but I don't know how to access the session object from nightmare.

Answer Source

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: 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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download