diff --git a/lib/actions.js b/lib/actions.js index bcf5e7d1..5f586662 100644 --- a/lib/actions.js +++ b/lib/actions.js @@ -881,9 +881,19 @@ exports.cookies.clear = function(name, done) { */ exports.cookies.clearAll = function(done) { + debug('cookies.clearAll()') this.child.call('cookie.clearAll', done) } +/** + * Flush cookies + */ + +exports.cookies.flush = function(done) { + debug('cookies.flush()') + this.child.call('cookie.flush', done) +} + /** * Authentication */ diff --git a/lib/runner.js b/lib/runner.js index 36ab45c9..bea6f0a6 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -606,6 +606,17 @@ app.on('ready', function() { ) }) + /** + * Flush cookies + */ + + parent.respondTo('cookie.flush', function(done) { + win.webContents.session.cookies.flushStore(function(err) { + if (err) return done(err) + return done() + }) + }) + /** * Add custom functionality */ diff --git a/test/index.js b/test/index.js index f5da92b1..6365fdae 100644 --- a/test/index.js +++ b/test/index.js @@ -1383,6 +1383,43 @@ describe('Nightmare', function() { cookies.length.should.equal(0) }) + it('cookies.flush() should flush cookies to dist', function*() { + nightmare = Nightmare({ + webPreferences: { partition: 'persist:test-partition' } + }) + + yield nightmare.goto(fixture('cookie')) + + // set expirationDate to make a non-session cookie + var expiration = new Date() + expiration.setHours(expiration.getHours() + 6) + + yield nightmare.cookies.set({ + name: 'flushedcookie', + value: 'hello', + path: '/', + expirationDate: expiration.getTime() + }) + + var cookies = yield nightmare.cookies.get() + cookies.length.should.equal(1) + + yield nightmare.cookies.flush() + + yield nightmare.end() + + nightmare = Nightmare({ + webPreferences: { partition: 'persist:test-partition' } + }) + + yield nightmare.goto(fixture('cookie')) + + cookies = yield nightmare.cookies.get({ url: null }) + cookies.length.should.equal(1) + + yield nightmare.cookies.clearAll() + }) + it('should return a proper error', function*() { try { yield nightmare.goto(fixture('cookie')).cookies.set({