Skip to content

Commit

Permalink
feat(firefox): implement cookies api (#4076)
Browse files Browse the repository at this point in the history
This patch implements `page.setCookie()`, `page.deleteCookie()` and
`page.cookies()` and doubles the test coverage for cookies so that
we can feel safer on cross-browser compatibility.
  • Loading branch information
aslushnikov committed Feb 27, 2019
1 parent 03d06f5 commit 9ef23b1
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 72 deletions.
62 changes: 62 additions & 0 deletions experimental/puppeteer-firefox/lib/Page.js
Expand Up @@ -8,6 +8,7 @@ const util = require('util');
const EventEmitter = require('events');
const {createHandle} = require('./JSHandle');
const {Events} = require('./Events');
const {Connection} = require('./Connection');
const {FrameManager, normalizeWaitUntil} = require('./FrameManager');
const {NetworkManager} = require('./NetworkManager');
const {TimeoutSettings} = require('./TimeoutSettings');
Expand Down Expand Up @@ -80,6 +81,67 @@ class Page extends EventEmitter {
});
}

/**
* @param {!Array<string>} urls
* @return {!Promise<!Array<Network.Cookie>>}
*/
async cookies(...urls) {
const connection = Connection.fromSession(this._session);
return (await connection.send('Browser.getCookies', {
browserContextId: this._target._context._browserContextId,
urls: urls.length ? urls : [this.url()]
})).cookies;
}

/**
* @param {Array<Protocol.Network.deleteCookiesParameters>} cookies
*/
async deleteCookie(...cookies) {
const pageURL = this.url();
const items = [];
for (const cookie of cookies) {
const item = {
url: cookie.url,
domain: cookie.domain,
path: cookie.path,
name: cookie.name,
};
if (!item.url && pageURL.startsWith('http'))
item.url = pageURL;
items.push(item);
}

const connection = Connection.fromSession(this._session);
await connection.send('Browser.deleteCookies', {
browserContextId: this._target._context._browserContextId,
cookies: items,
});
}

/**
* @param {Array<Network.CookieParam>} cookies
*/
async setCookie(...cookies) {
const pageURL = this.url();
const startsWithHTTP = pageURL.startsWith('http');
const items = cookies.map(cookie => {
const item = Object.assign({}, cookie);
if (!item.url && startsWithHTTP)
item.url = pageURL;
assert(item.url !== 'about:blank', `Blank page can not have cookie "${item.name}"`);
assert(!String.prototype.startsWith.call(item.url || '', 'data:'), `Data URL page can not have cookie "${item.name}"`);
return item;
});
await this.deleteCookie(...items);
if (items.length) {
const connection = Connection.fromSession(this._session);
await connection.send('Browser.setCookies', {
browserContextId: this._target._context._browserContextId,
cookies: items
});
}
}

async setRequestInterception(enabled) {
await this._networkManager.setRequestInterception(enabled);
}
Expand Down
2 changes: 1 addition & 1 deletion experimental/puppeteer-firefox/package.json
Expand Up @@ -9,7 +9,7 @@
"node": ">=8.9.4"
},
"puppeteer": {
"firefox_revision": "d69636bbb91f42286e81ef673b33a1459bcdfcea"
"firefox_revision": "fd63770c54de8a6e4ac28bd9f010405c12105d63"
},
"scripts": {
"install": "node install.js",
Expand Down
10 changes: 2 additions & 8 deletions lib/Page.js
Expand Up @@ -388,14 +388,8 @@ class Page extends EventEmitter {
const item = Object.assign({}, cookie);
if (!item.url && startsWithHTTP)
item.url = pageURL;
assert(
item.url !== 'about:blank',
`Blank page can not have cookie "${item.name}"`
);
assert(
!String.prototype.startsWith.call(item.url || '', 'data:'),
`Data URL page can not have cookie "${item.name}"`
);
assert(item.url !== 'about:blank', `Blank page can not have cookie "${item.name}"`);
assert(!String.prototype.startsWith.call(item.url || '', 'data:'), `Data URL page can not have cookie "${item.name}"`);
return item;
});
await this.deleteCookie(...items);
Expand Down
2 changes: 1 addition & 1 deletion test/browsercontext.spec.js
Expand Up @@ -18,7 +18,7 @@ const utils = require('./utils');

module.exports.addTests = function({testRunner, expect, puppeteer, Errors}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit, it_fails_ffox} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const {TimeoutError} = Errors;

Expand Down

0 comments on commit 9ef23b1

Please sign in to comment.