Skip to content

Commit 417981a

Browse files
authored
fix(firefox): fix cookies in default browser context (#4850)
This patch adds tests and fixes the nodejs part of the problem. The issue will be fixed once we roll a new version of Firefox. References #4470
1 parent b6b2950 commit 417981a

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

experimental/puppeteer-firefox/lib/Page.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Page extends EventEmitter {
8888
async cookies(...urls) {
8989
const connection = Connection.fromSession(this._session);
9090
return (await connection.send('Browser.getCookies', {
91-
browserContextId: this._target._context._browserContextId,
91+
browserContextId: this._target._context._browserContextId || undefined,
9292
urls: urls.length ? urls : [this.url()]
9393
})).cookies;
9494
}
@@ -113,7 +113,7 @@ class Page extends EventEmitter {
113113

114114
const connection = Connection.fromSession(this._session);
115115
await connection.send('Browser.deleteCookies', {
116-
browserContextId: this._target._context._browserContextId,
116+
browserContextId: this._target._context._browserContextId || undefined,
117117
cookies: items,
118118
});
119119
}
@@ -136,7 +136,7 @@ class Page extends EventEmitter {
136136
if (items.length) {
137137
const connection = Connection.fromSession(this._session);
138138
await connection.send('Browser.setCookies', {
139-
browserContextId: this._target._context._browserContextId,
139+
browserContextId: this._target._context._browserContextId || undefined,
140140
cookies: items
141141
});
142142
}

test/defaultbrowsercontext.spec.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright 2017 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer}) {
18+
const {describe, xdescribe, fdescribe, describe_fails_ffox} = testRunner;
19+
const {it, fit, xit, it_fails_ffox} = testRunner;
20+
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
21+
22+
describe_fails_ffox('DefaultBrowserContext', function() {
23+
beforeEach(async state => {
24+
state.browser = await puppeteer.launch(defaultBrowserOptions);
25+
state.page = await state.browser.newPage();
26+
});
27+
afterEach(async state => {
28+
await state.browser.close();
29+
delete state.browser;
30+
delete state.page;
31+
});
32+
it('page.cookies() should work', async({page, server}) => {
33+
await page.goto(server.EMPTY_PAGE);
34+
await page.evaluate(() => {
35+
document.cookie = 'username=John Doe';
36+
});
37+
expect(await page.cookies()).toEqual([{
38+
name: 'username',
39+
value: 'John Doe',
40+
domain: 'localhost',
41+
path: '/',
42+
expires: -1,
43+
size: 16,
44+
httpOnly: false,
45+
secure: false,
46+
session: true
47+
}]);
48+
});
49+
it('page.setCookie() should work', async({page, server}) => {
50+
await page.goto(server.EMPTY_PAGE);
51+
await page.setCookie({
52+
name: 'username',
53+
value: 'John Doe'
54+
});
55+
expect(await page.evaluate(() => document.cookie)).toBe('username=John Doe');
56+
expect(await page.cookies()).toEqual([{
57+
name: 'username',
58+
value: 'John Doe',
59+
domain: 'localhost',
60+
path: '/',
61+
expires: -1,
62+
size: 16,
63+
httpOnly: false,
64+
secure: false,
65+
session: true
66+
}]);
67+
});
68+
it('page.deleteCookie() should work', async({page, server}) => {
69+
await page.goto(server.EMPTY_PAGE);
70+
await page.setCookie({
71+
name: 'cookie1',
72+
value: '1'
73+
}, {
74+
name: 'cookie2',
75+
value: '2'
76+
});
77+
expect(await page.evaluate('document.cookie')).toBe('cookie1=1; cookie2=2');
78+
await page.deleteCookie({name: 'cookie2'});
79+
expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
80+
expect(await page.cookies()).toEqual([{
81+
name: 'cookie1',
82+
value: '1',
83+
domain: 'localhost',
84+
path: '/',
85+
expires: -1,
86+
size: 8,
87+
httpOnly: false,
88+
secure: false,
89+
session: true
90+
}]);
91+
});
92+
});
93+
};

test/puppeteer.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ module.exports.addTests = ({testRunner, product, puppeteerPath}) => {
155155

156156
// Top-level tests that launch Browser themselves.
157157
require('./ignorehttpserrors.spec.js').addTests(testOptions);
158+
require('./defaultbrowsercontext.spec.js').addTests(testOptions);
158159
require('./launcher.spec.js').addTests(testOptions);
159160
require('./fixtures.spec.js').addTests(testOptions);
160161
if (CHROME) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy