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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With