Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alter default behavior to non-persistent partition #919

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,34 @@ __nightmare.ipc = require('electron').ipcRenderer;

To benefit of all of nightmare's feedback from the browser, you can instead copy the contents of nightmare's [preload script](lib/preload.js).

#### Storage Persistence between nightmare instances

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. If you would like to persist state between instances you can use the [webPreferences.partition](http://electron.atom.io/docs/api/browser-window/#new-browserwindowoptions) api in electron.

```js
var Nightmare = require('nightmare');

nightmare = Nightmare(); // non persistent paritition by default
yield nightmare
.evaluate(function () {
window.localStorage.setItem('testing', 'This will not be persisted');
})
.end();

nightmare = Nightmare({
webPreferences: {
partition: 'persist: testing'
}
});
yield nightmare
.evaluate(function () {
window.localStorage.setItem('testing', 'This is persisted for other instances with the same paritition name');
})
.end();
```

If you specify a `null` paritition then it will use the electron default behavior (persistent) or any string that starts with `'persist:'` will persist under that partition name, any other string will result in in-memory only storage.

## Usage
#### Installation
Nightmare is a Node.js module, so you'll need to [have Node.js installed](http://nodejs.org/). Then you just need to `npm install` the module:
Expand Down
8 changes: 8 additions & 0 deletions lib/nightmare.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const MAX_AUTH_RETRIES = 3;
const DEFAULT_EXECUTION_TIMEOUT = 30 * 1000
// Error message when halted
const DEFAULT_HALT_MESSAGE = 'Nightmare Halted';
// Non-persistent partition to use by defaults
const DEFAULT_PARTITION = 'nightmare';

/**
* Export `Nightmare`
Expand Down Expand Up @@ -77,6 +79,12 @@ function Nightmare(options) {

options.typeInterval = options.typeInterval || DEFAULT_TYPE_INTERVAL;
options.executionTimeout = options.executionTimeout || DEFAULT_EXECUTION_TIMEOUT;
options.webPreferences = options.webPreferences || {};

// null is a valid value, which will result in the use of the electron default behavior, which is to persist storage.
// The default behavior for nightmare will be to use non-persistent storage.
// http://electron.atom.io/docs/api/browser-window/#new-browserwindowoptions
options.webPreferences.partition = options.webPreferences.partition !== undefined ? options.webPreferences.partition : DEFAULT_PARTITION;

var electron_path = options.electronPath || default_electron_path

Expand Down
85 changes: 85 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,91 @@ describe('Nightmare', function () {
logged.should.be.true;
});
});

describe('partitioning', function(){
afterEach(function*() {
yield nightmare.end();
});

// The default behavior for nightmare is to use a non-persistent partition name
it('should not persist between instances by default', function *() {
nightmare = Nightmare();
yield nightmare
.goto(fixture('simple'))
.evaluate(function() {
window.localStorage.setItem('testing', 'This string should not persist between instances.')
})
.end();

nightmare = Nightmare();
var value = yield nightmare
.goto(fixture('simple'))
.evaluate(function() {
return window.localStorage.getItem('testing') || ''
});

value.should.equal('');
})

// Setting the partition to null we default to the electron default behavior
// which is to use a persistent storage between instances.
it('should persist between instances if partition is null', function *() {
nightmare = Nightmare({ webPreferences: { partition: null } });
yield nightmare
.goto(fixture('simple'))
.evaluate(function() {
window.localStorage.setItem('testing', 'This string should persist between instances.')
})
.end();

nightmare = Nightmare({ webPreferences: { partition: null } });
var value = yield nightmare
.goto(fixture('simple'))
.evaluate(function() {
return window.localStorage.getItem('testing') || ''
});

value.should.equal('This string should persist between instances.');
});

it('should not persist between instances if partition name doesnt start with "persist:"', function *(){
nightmare = Nightmare({ webPreferences: { partition: 'nonpersist' } });
yield nightmare
.goto(fixture('simple'))
.evaluate(function() {
window.localStorage.setItem('testing', 'This string not should persist between instances.')
})
.end();

nightmare = Nightmare({ webPreferences: { partition: 'nonpersist' } });
var value = yield nightmare
.goto(fixture('simple'))
.evaluate(function() {
return window.localStorage.getItem('testing') || ''
});

value.should.equal('');
});

it('should persist between instances if partition starts with "persist:"', function *() {
nightmare = Nightmare({ webPreferences: { partition: 'persist: testing' } });
yield nightmare
.goto(fixture('simple'))
.evaluate(function() {
window.localStorage.setItem('testing', 'This string should persist between instances.')
})
.end();

nightmare = Nightmare({ webPreferences: { partition: 'persist: testing' } });
var value = yield nightmare
.goto(fixture('simple'))
.evaluate(function() {
return window.localStorage.getItem('testing') || ''
});

value.should.equal('This string should persist between instances.');
});
})
});

/**
Expand Down