Skip to content

Commit

Permalink
api(addCookies): setCookies -> addCookies (#1367)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Mar 13, 2020
1 parent 3fa4255 commit cfd3ae2
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 50 deletions.
38 changes: 19 additions & 19 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ await context.close();
<!-- GEN:toc -->
- [event: 'close'](#event-close)
- [event: 'page'](#event-page)
- [browserContext.addCookies(cookies)](#browsercontextaddcookiescookies)
- [browserContext.addInitScript(script[, ...args])](#browsercontextaddinitscriptscript-args)
- [browserContext.clearCookies()](#browsercontextclearcookies)
- [browserContext.clearPermissions()](#browsercontextclearpermissions)
Expand All @@ -291,7 +292,6 @@ await context.close();
- [browserContext.newPage()](#browsercontextnewpage)
- [browserContext.pages()](#browsercontextpages)
- [browserContext.route(url, handler)](#browsercontextrouteurl-handler)
- [browserContext.setCookies(cookies)](#browsercontextsetcookiescookies)
- [browserContext.setDefaultNavigationTimeout(timeout)](#browsercontextsetdefaultnavigationtimeouttimeout)
- [browserContext.setDefaultTimeout(timeout)](#browsercontextsetdefaulttimeouttimeout)
- [browserContext.setExtraHTTPHeaders(headers)](#browsercontextsetextrahttpheadersheaders)
Expand Down Expand Up @@ -323,6 +323,23 @@ const [event] = await Promise.all([
const newPage = await event.page();
```

#### browserContext.addCookies(cookies)
- `cookies` <[Array]<[Object]>>
- `name` <[string]> **required**
- `value` <[string]> **required**
- `url` <[string]> either url or domain / path are **required**
- `domain` <[string]> either url or domain / path are **required**
- `path` <[string]> either url or domain / path are **required**
- `expires` <[number]> Unix time in seconds.
- `httpOnly` <[boolean]>
- `secure` <[boolean]>
- `sameSite` <"Strict"|"Lax"|"None">
- returns: <[Promise]>

```js
await browserContext.addCookies([cookieObject1, cookieObject2]);
```

#### browserContext.addInitScript(script[, ...args])
- `script` <[function]|[string]|[Object]> Script to be evaluated in all pages in the browser context.
- `path` <[string]> Path to the JavaScript file. If `path` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd).
Expand Down Expand Up @@ -472,23 +489,6 @@ Page routes (set up with [page.route(url, handler)](#pagerouteurl-handler)) take

> **NOTE** Enabling request interception disables http cache.
#### browserContext.setCookies(cookies)
- `cookies` <[Array]<[Object]>>
- `name` <[string]> **required**
- `value` <[string]> **required**
- `url` <[string]> either url or domain / path are **required**
- `domain` <[string]> either url or domain / path are **required**
- `path` <[string]> either url or domain / path are **required**
- `expires` <[number]> Unix time in seconds.
- `httpOnly` <[boolean]>
- `secure` <[boolean]>
- `sameSite` <"Strict"|"Lax"|"None">
- returns: <[Promise]>

```js
await browserContext.setCookies([cookieObject1, cookieObject2]);
```

#### browserContext.setDefaultNavigationTimeout(timeout)
- `timeout` <[number]> Maximum navigation time in milliseconds

Expand Down Expand Up @@ -3928,6 +3928,7 @@ const backgroundPage = await backroundPageTarget.page();
<!-- GEN:toc-extends-BrowserContext -->
- [event: 'close'](#event-close)
- [event: 'page'](#event-page)
- [browserContext.addCookies(cookies)](#browsercontextaddcookiescookies)
- [browserContext.addInitScript(script[, ...args])](#browsercontextaddinitscriptscript-args)
- [browserContext.clearCookies()](#browsercontextclearcookies)
- [browserContext.clearPermissions()](#browsercontextclearpermissions)
Expand All @@ -3937,7 +3938,6 @@ const backgroundPage = await backroundPageTarget.page();
- [browserContext.newPage()](#browsercontextnewpage)
- [browserContext.pages()](#browsercontextpages)
- [browserContext.route(url, handler)](#browsercontextrouteurl-handler)
- [browserContext.setCookies(cookies)](#browsercontextsetcookiescookies)
- [browserContext.setDefaultNavigationTimeout(timeout)](#browsercontextsetdefaultnavigationtimeouttimeout)
- [browserContext.setDefaultTimeout(timeout)](#browsercontextsetdefaulttimeouttimeout)
- [browserContext.setExtraHTTPHeaders(headers)](#browsercontextsetextrahttpheadersheaders)
Expand Down
4 changes: 2 additions & 2 deletions src/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface BrowserContext {
pages(): Promise<Page[]>;
newPage(): Promise<Page>;
cookies(urls?: string | string[]): Promise<network.NetworkCookie[]>;
setCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>;
addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>;
clearCookies(): Promise<void>;
setPermissions(origin: string, permissions: string[]): Promise<void>;
clearPermissions(): Promise<void>;
Expand Down Expand Up @@ -92,7 +92,7 @@ export abstract class BrowserContextBase extends platform.EventEmitter implement
abstract pages(): Promise<Page[]>;
abstract newPage(): Promise<Page>;
abstract cookies(...urls: string[]): Promise<network.NetworkCookie[]>;
abstract setCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>;
abstract addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>;
abstract clearCookies(): Promise<void>;
abstract setPermissions(origin: string, permissions: string[]): Promise<void>;
abstract clearPermissions(): Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion src/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export class CRBrowserContext extends BrowserContextBase {
}), urls);
}

async setCookies(cookies: network.SetNetworkCookieParam[]) {
async addCookies(cookies: network.SetNetworkCookieParam[]) {
await this._browser._session.send('Storage.setCookies', { cookies: network.rewriteCookies(cookies), browserContextId: this._browserContextId || undefined });
}

Expand Down
2 changes: 1 addition & 1 deletion src/firefox/ffBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class FFBrowserContext extends BrowserContextBase {
}), urls);
}

async setCookies(cookies: network.SetNetworkCookieParam[]) {
async addCookies(cookies: network.SetNetworkCookieParam[]) {
await this._browser._connection.send('Browser.setCookies', { browserContextId: this._browserContextId || undefined, cookies: network.rewriteCookies(cookies) });
}

Expand Down
2 changes: 1 addition & 1 deletion src/webkit/wkBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class WKBrowserContext extends BrowserContextBase {
}), urls);
}

async setCookies(cookies: network.SetNetworkCookieParam[]) {
async addCookies(cookies: network.SetNetworkCookieParam[]) {
const cc = network.rewriteCookies(cookies).map(c => ({
...c,
session: c.expires === -1 || c.expires === undefined,
Expand Down
44 changes: 22 additions & 22 deletions test/cookies.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
]);
});
it('should get cookies from multiple urls', async({context}) => {
await context.setCookies([{
await context.addCookies([{
url: 'https://foo.com',
name: 'doggo',
value: 'woofs',
Expand Down Expand Up @@ -161,10 +161,10 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
});
});

describe('BrowserContext.setCookies', function() {
describe('BrowserContext.addCookies', function() {
it('should work', async({context, page, server}) => {
await page.goto(server.EMPTY_PAGE);
await context.setCookies([{
await context.addCookies([{
url: server.EMPTY_PAGE,
name: 'password',
value: '123456'
Expand All @@ -182,7 +182,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
const cookies = await context.cookies();
await context.clearCookies();
expect(await context.cookies()).toEqual([]);
await context.setCookies(cookies);
await context.addCookies(cookies);
expect(await context.cookies()).toEqual(cookies);
});
it('should send cookie header', async({server, context}) => {
Expand All @@ -191,15 +191,15 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
cookie = req.headers.cookie;
res.end();
});
await context.setCookies([{url: server.EMPTY_PAGE, name: 'cookie', value: 'value'}]);
await context.addCookies([{url: server.EMPTY_PAGE, name: 'cookie', value: 'value'}]);
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
expect(cookie).toBe('cookie=value');
});
it('should isolate cookies in browser contexts', async({context, server, browser}) => {
const anotherContext = await browser.newContext();
await context.setCookies([{url: server.EMPTY_PAGE, name: 'isolatecookie', value: 'page1value'}]);
await anotherContext.setCookies([{url: server.EMPTY_PAGE, name: 'isolatecookie', value: 'page2value'}]);
await context.addCookies([{url: server.EMPTY_PAGE, name: 'isolatecookie', value: 'page1value'}]);
await anotherContext.addCookies([{url: server.EMPTY_PAGE, name: 'isolatecookie', value: 'page2value'}]);

const cookies1 = await context.cookies();
const cookies2 = await anotherContext.cookies();
Expand Down Expand Up @@ -261,7 +261,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
cookie = req.headers.cookie || '';
res.end();
});
await context.setCookies([{url: server.EMPTY_PAGE, name: 'sendcookie', value: 'value'}]);
await context.addCookies([{url: server.EMPTY_PAGE, name: 'sendcookie', value: 'value'}]);
{
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
Expand All @@ -278,7 +278,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
it.slow()('should isolate cookies between launches', async({server}) => {
const browser1 = await playwright.launch(defaultBrowserOptions);
const context1 = await browser1.newContext();
await context1.setCookies([{url: server.EMPTY_PAGE, name: 'cookie-in-context-1', value: 'value', expires: Date.now() / 1000 + 10000}]);
await context1.addCookies([{url: server.EMPTY_PAGE, name: 'cookie-in-context-1', value: 'value', expires: Date.now() / 1000 + 10000}]);
await browser1.close();

const browser2 = await playwright.launch(defaultBrowserOptions);
Expand All @@ -289,7 +289,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
});
it('should set multiple cookies', async({context, page, server}) => {
await page.goto(server.EMPTY_PAGE);
await context.setCookies([{
await context.addCookies([{
url: server.EMPTY_PAGE,
name: 'multiple-1',
value: '123456'
Expand All @@ -307,7 +307,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
]);
});
it('should have |expires| set to |-1| for session cookies', async({context, server}) => {
await context.setCookies([{
await context.addCookies([{
url: server.EMPTY_PAGE,
name: 'expires',
value: '123456'
Expand All @@ -316,7 +316,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
expect(cookies[0].expires).toBe(-1);
});
it('should set cookie with reasonable defaults', async({context, server}) => {
await context.setCookies([{
await context.addCookies([{
url: server.EMPTY_PAGE,
name: 'defaults',
value: '123456'
Expand All @@ -335,7 +335,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
});
it('should set a cookie with a path', async({context, page, server}) => {
await page.goto(server.PREFIX + '/grid.html');
await context.setCookies([{
await context.addCookies([{
domain: 'localhost',
path: '/grid.html',
name: 'gridcookie',
Expand All @@ -360,7 +360,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
it('should not set a cookie with blank page URL', async function({context, server}) {
let error = null;
try {
await context.setCookies([
await context.addCookies([
{url: server.EMPTY_PAGE, name: 'example-cookie', value: 'best'},
{url: 'about:blank', name: 'example-cookie-blank', value: 'best'}
]);
Expand All @@ -374,7 +374,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
it('should not set a cookie on a data URL page', async function({context}) {
let error = null;
try {
await context.setCookies([{url: 'data:,Hello%2C%20World!', name: 'example-cookie', value: 'best'}]);
await context.addCookies([{url: 'data:,Hello%2C%20World!', name: 'example-cookie', value: 'best'}]);
} catch (e) {
error = e;
}
Expand All @@ -383,7 +383,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
it('should default to setting secure cookie for HTTPS websites', async({context, page, server}) => {
await page.goto(server.EMPTY_PAGE);
const SECURE_URL = 'https://example.com';
await context.setCookies([{
await context.addCookies([{
url: SECURE_URL,
name: 'foo',
value: 'bar',
Expand All @@ -394,7 +394,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
it('should be able to set unsecure cookie for HTTP website', async({context, page, server}) => {
await page.goto(server.EMPTY_PAGE);
const HTTP_URL = 'http://example.com';
await context.setCookies([{
await context.addCookies([{
url: HTTP_URL,
name: 'foo',
value: 'bar',
Expand All @@ -404,7 +404,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
});
it('should set a cookie on a different domain', async({context, page, server}) => {
await page.goto(server.EMPTY_PAGE);
await context.setCookies([{
await context.addCookies([{
url: 'https://www.example.com',
name: 'example-cookie',
value: 'best',
Expand All @@ -423,7 +423,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
});
it('should set cookies from a frame', async({context, page, server}) => {
await page.goto(server.PREFIX + '/grid.html');
await context.setCookies([
await context.addCookies([
{url: server.PREFIX, name: 'localhost-cookie', value: 'best'},
{url: server.CROSS_PROCESS_PREFIX, name: '127-cookie', value: 'worst'}
]);
Expand Down Expand Up @@ -467,7 +467,7 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
describe('BrowserContext.clearCookies', function() {
it('should clear cookies', async({context, page, server}) => {
await page.goto(server.EMPTY_PAGE);
await context.setCookies([{
await context.addCookies([{
url: server.EMPTY_PAGE,
name: 'cookie1',
value: '1'
Expand All @@ -480,8 +480,8 @@ module.exports.describe = function({testRunner, expect, playwright, defaultBrows
});
it('should isolate cookies when clearing', async({context, server, browser}) => {
const anotherContext = await browser.newContext();
await context.setCookies([{url: server.EMPTY_PAGE, name: 'page1cookie', value: 'page1value'}]);
await anotherContext.setCookies([{url: server.EMPTY_PAGE, name: 'page2cookie', value: 'page2value'}]);
await context.addCookies([{url: server.EMPTY_PAGE, name: 'page1cookie', value: 'page1value'}]);
await anotherContext.addCookies([{url: server.EMPTY_PAGE, name: 'page2cookie', value: 'page2value'}]);

expect((await context.cookies()).length).toBe(1);
expect((await anotherContext.cookies()).length).toBe(1);
Expand Down
6 changes: 3 additions & 3 deletions test/defaultbrowsercontext.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ module.exports.describe = function ({ testRunner, expect, defaultBrowserOptions,
sameSite: 'None',
}]);
});
it('context.setCookies() should work', async({page, server}) => {
it('context.addCookies() should work', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
await page.context().setCookies([{
await page.context().addCookies([{
url: server.EMPTY_PAGE,
name: 'username',
value: 'John Doe'
Expand All @@ -74,7 +74,7 @@ module.exports.describe = function ({ testRunner, expect, defaultBrowserOptions,
});
it('context.clearCookies() should work', async({page, server}) => {
await page.goto(server.EMPTY_PAGE);
await page.context().setCookies([{
await page.context().addCookies([{
url: server.EMPTY_PAGE,
name: 'cookie1',
value: '1'
Expand Down
2 changes: 1 addition & 1 deletion test/interception.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
it('should properly return navigation response when URL has cookies', async({context, page, server}) => {
// Setup cookie.
await page.goto(server.EMPTY_PAGE);
await context.setCookies([{ url: server.EMPTY_PAGE, name: 'foo', value: 'bar'}]);
await context.addCookies([{ url: server.EMPTY_PAGE, name: 'foo', value: 'bar'}]);

// Setup request interception.
await page.route('**/*', request => request.continue());
Expand Down

0 comments on commit cfd3ae2

Please sign in to comment.