Skip to content

Commit c21977b

Browse files
committed
wip: add more converted tests
1 parent fefbebc commit c21977b

File tree

3 files changed

+323
-293
lines changed

3 files changed

+323
-293
lines changed

tests/async/test_browsercontext_add_cookies.py

Lines changed: 93 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,91 @@
1-
# /**
2-
# * Copyright 2018 Google Inc. All rights reserved.
3-
# * Modifications copyright (c) Microsoft Corporation.
4-
# *
5-
# * Licensed under the Apache License, Version 2.0 (the "License");
6-
# * you may not use this file except in compliance with the License.
7-
# * You may obtain a copy of the License at
8-
# *
9-
# * http://www.apache.org/licenses/LICENSE-2.0
10-
# *
11-
# * Unless required by applicable law or agreed to in writing, software
12-
# * distributed under the License is distributed on an "AS IS" BASIS,
13-
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# * See the License for the specific language governing permissions and
15-
# * limitations under the License.
16-
# */
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
1714

18-
# const {FFOX, CHROMIUM, WEBKIT, WIN} = testOptions;
15+
import datetime
1916

20-
# it('should work', async({context, page, server}) => {
21-
# await page.goto(server.EMPTY_PAGE);
22-
# await context.addCookies([{
23-
# url: server.EMPTY_PAGE,
24-
# name: 'password',
25-
# value: '123456'
26-
# }]);
27-
# expect(await page.evaluate(() => document.cookie)).toEqual('password=123456');
28-
# });
29-
# it('should roundtrip cookie', async({context, page, server}) => {
30-
# await page.goto(server.EMPTY_PAGE);
31-
# // @see https://en.wikipedia.org/wiki/Year_2038_problem
32-
# const date = +(new Date('1/1/2038'));
33-
# const documentCookie = await page.evaluate(timestamp => {
34-
# const date = new Date(timestamp);
35-
# document.cookie = `username=John Doe;expires=${date.toUTCString()}`;
36-
# return document.cookie;
37-
# }, date);
38-
# expect(documentCookie).toBe('username=John Doe');
39-
# const cookies = await context.cookies();
40-
# await context.clearCookies();
41-
# expect(await context.cookies()).toEqual([]);
42-
# await context.addCookies(cookies);
43-
# expect(await context.cookies()).toEqual(cookies);
44-
# });
45-
# it('should send cookie header', async({server, context}) => {
46-
# let cookie = '';
47-
# server.setRoute('/empty.html', (req, res) => {
48-
# cookie = req.headers.cookie;
49-
# res.end();
50-
# });
51-
# await context.addCookies([{url: server.EMPTY_PAGE, name: 'cookie', value: 'value'}]);
52-
# const page = await context.newPage();
53-
# await page.goto(server.EMPTY_PAGE);
54-
# expect(cookie).toBe('cookie=value');
55-
# });
56-
# it('should isolate cookies in browser contexts', async({context, server, browser}) => {
57-
# const anotherContext = await browser.newContext();
58-
# await context.addCookies([{url: server.EMPTY_PAGE, name: 'isolatecookie', value: 'page1value'}]);
59-
# await anotherContext.addCookies([{url: server.EMPTY_PAGE, name: 'isolatecookie', value: 'page2value'}]);
6017

61-
# const cookies1 = await context.cookies();
62-
# const cookies2 = await anotherContext.cookies();
63-
# expect(cookies1.length).toBe(1);
64-
# expect(cookies2.length).toBe(1);
65-
# expect(cookies1[0].name).toBe('isolatecookie');
66-
# expect(cookies1[0].value).toBe('page1value');
67-
# expect(cookies2[0].name).toBe('isolatecookie');
68-
# expect(cookies2[0].value).toBe('page2value');
69-
# await anotherContext.close();
70-
# });
71-
# it('should isolate session cookies', async({context, server, browser}) => {
18+
async def test_should_work(context, page, server):
19+
await page.goto(server.EMPTY_PAGE)
20+
await context.addCookies(
21+
[{"url": server.EMPTY_PAGE, "name": "password", "value": "123456"}]
22+
)
23+
assert await page.evaluate("() => document.cookie") == "password=123456"
24+
25+
26+
async def test_should_roundtrip_cookie(context, page, server):
27+
await page.goto(server.EMPTY_PAGE)
28+
# @see https://en.wikipedia.org/wiki/Year_2038_problem
29+
date = int(datetime.datetime(2038, 1, 1).timestamp() * 1000)
30+
document_cookie = await page.evaluate(
31+
"""timestamp => {
32+
const date = new Date(timestamp);
33+
document.cookie = `username=John Doe;expires=${date.toUTCString()}`;
34+
return document.cookie;
35+
}""",
36+
date,
37+
)
38+
assert document_cookie == "username=John Doe"
39+
cookies = await context.cookies()
40+
await context.clearCookies()
41+
assert await context.cookies() == []
42+
await context.addCookies(cookies)
43+
assert await context.cookies() == cookies
44+
45+
46+
async def test_should_send_cookie_header(server, context):
47+
cookie = []
48+
49+
def handler(request):
50+
cookie.extend(request.requestHeaders.getRawHeaders("cookie"))
51+
request.finish()
52+
53+
server.set_route("/empty.html", handler)
54+
await context.addCookies(
55+
[{"url": server.EMPTY_PAGE, "name": "cookie", "value": "value"}]
56+
)
57+
page = await context.newPage()
58+
await page.goto(server.EMPTY_PAGE)
59+
assert cookie == ["cookie=value"]
60+
61+
62+
async def test_should_isolate_cookies_in_browser_contexts(context, server, browser):
63+
another_context = await browser.newContext()
64+
await context.addCookies(
65+
[{"url": server.EMPTY_PAGE, "name": "isolatecookie", "value": "page1value"}]
66+
)
67+
await another_context.addCookies(
68+
[{"url": server.EMPTY_PAGE, "name": "isolatecookie", "value": "page2value"}]
69+
)
70+
71+
cookies_1 = await context.cookies()
72+
cookies_2 = await another_context.cookies()
73+
assert len(cookies_1) == 1
74+
assert len(cookies_2) == 1
75+
assert cookies_1[0]["name"] == "isolatecookie"
76+
assert cookies_1[0]["value"] == "page1value"
77+
assert cookies_2[0]["name"] == "isolatecookie"
78+
assert cookies_2[0]["value"] == "page2value"
79+
await another_context.close()
80+
81+
82+
# async def test_should isolate session cookies', async({context, server, browser}) => {
7283
# server.setRoute('/setcookie.html', (req, res) => {
7384
# res.setHeader('Set-Cookie', 'session=value');
7485
# res.end();
7586
# });
7687
# {
77-
# const page = await context.newPage();
88+
# page = await context.newPage();
7889
# await page.goto(server.PREFIX + '/setcookie.html');
7990
# }
8091
# {
@@ -93,7 +104,7 @@
93104
# await context2.close();
94105
# }
95106
# });
96-
# it('should isolate persistent cookies', async({context, server, browser}) => {
107+
# async def test_should isolate persistent cookies', async({context, server, browser}) => {
97108
# server.setRoute('/setcookie.html', (req, res) => {
98109
# res.setHeader('Set-Cookie', 'persistent=persistent-value; max-age=3600');
99110
# res.end();
@@ -112,7 +123,7 @@
112123
# expect(cookies2.length).toBe(0);
113124
# await context2.close();
114125
# });
115-
# it('should isolate send cookie header', async({server, context, browser}) => {
126+
# async def test_should isolate send cookie header', async({server, context, browser}) => {
116127
# let cookie = [];
117128
# server.setRoute('/empty.html', (req, res) => {
118129
# cookie = req.headers.cookie || '';
@@ -144,7 +155,7 @@
144155
# expect(cookies.length).toBe(0);
145156
# await browser2.close();
146157
# });
147-
# it('should set multiple cookies', async({context, page, server}) => {
158+
# async def test_should set multiple cookies', async({context, page, server}) => {
148159
# await page.goto(server.EMPTY_PAGE);
149160
# await context.addCookies([{
150161
# url: server.EMPTY_PAGE,
@@ -163,7 +174,7 @@
163174
# 'multiple-2=bar',
164175
# ]);
165176
# });
166-
# it('should have |expires| set to |-1| for session cookies', async({context, server}) => {
177+
# async def test_should have |expires| set to |-1| for session cookies', async({context, server}) => {
167178
# await context.addCookies([{
168179
# url: server.EMPTY_PAGE,
169180
# name: 'expires',
@@ -172,7 +183,7 @@
172183
# const cookies = await context.cookies();
173184
# expect(cookies[0].expires).toBe(-1);
174185
# });
175-
# it('should set cookie with reasonable defaults', async({context, server}) => {
186+
# async def test_should set cookie with reasonable defaults', async({context, server}) => {
176187
# await context.addCookies([{
177188
# url: server.EMPTY_PAGE,
178189
# name: 'defaults',
@@ -190,7 +201,7 @@
190201
# sameSite: 'None',
191202
# }]);
192203
# });
193-
# it('should set a cookie with a path', async({context, page, server}) => {
204+
# async def test_should set a cookie with a path', async({context, page, server}) => {
194205
# await page.goto(server.PREFIX + '/grid.html');
195206
# await context.addCookies([{
196207
# domain: 'localhost',
@@ -214,7 +225,7 @@
214225
# await page.goto(server.PREFIX + '/grid.html');
215226
# expect(await page.evaluate('document.cookie')).toBe('gridcookie=GRID');
216227
# });
217-
# it('should not set a cookie with blank page URL', async function({context, server}) {
228+
# async def test_should not set a cookie with blank page URL', async function({context, server}) {
218229
# let error = null;
219230
# try {
220231
# await context.addCookies([
@@ -228,7 +239,7 @@
228239
# `Blank page can not have cookie "example-cookie-blank"`
229240
# );
230241
# });
231-
# it('should not set a cookie on a data URL page', async function({context}) {
242+
# async def test_should not set a cookie on a data URL page', async function({context}) {
232243
# let error = null;
233244
# try {
234245
# await context.addCookies([{url: 'data:,Hello%2C%20World!', name: 'example-cookie', value: 'best'}]);
@@ -237,7 +248,7 @@
237248
# }
238249
# expect(error.message).toContain('Data URL page can not have cookie "example-cookie"');
239250
# });
240-
# it('should default to setting secure cookie for HTTPS websites', async({context, page, server}) => {
251+
# async def test_should default to setting secure cookie for HTTPS websites', async({context, page, server}) => {
241252
# await page.goto(server.EMPTY_PAGE);
242253
# const SECURE_URL = 'https://example.com';
243254
# await context.addCookies([{
@@ -248,7 +259,7 @@
248259
# const [cookie] = await context.cookies(SECURE_URL);
249260
# expect(cookie.secure).toBe(true);
250261
# });
251-
# it('should be able to set unsecure cookie for HTTP website', async({context, page, server}) => {
262+
# async def test_should be able to set unsecure cookie for HTTP website', async({context, page, server}) => {
252263
# await page.goto(server.EMPTY_PAGE);
253264
# const HTTP_URL = 'http://example.com';
254265
# await context.addCookies([{
@@ -259,7 +270,7 @@
259270
# const [cookie] = await context.cookies(HTTP_URL);
260271
# expect(cookie.secure).toBe(false);
261272
# });
262-
# it('should set a cookie on a different domain', async({context, page, server}) => {
273+
# async def test_should set a cookie on a different domain', async({context, page, server}) => {
263274
# await page.goto(server.EMPTY_PAGE);
264275
# await context.addCookies([{
265276
# url: 'https://www.example.com',
@@ -278,7 +289,7 @@
278289
# sameSite: 'None',
279290
# }]);
280291
# });
281-
# it('should set cookies for a frame', async({context, page, server}) => {
292+
# async def test_should set cookies for a frame', async({context, page, server}) => {
282293
# await page.goto(server.EMPTY_PAGE);
283294
# await context.addCookies([
284295
# {url: server.PREFIX, name: 'frame-cookie', value: 'value'}
@@ -295,7 +306,7 @@
295306

296307
# expect(await page.frames()[1].evaluate('document.cookie')).toBe('frame-cookie=value');
297308
# });
298-
# it('should(not) block third party cookies', async({context, page, server}) => {
309+
# async def test_should(not) block third party cookies', async({context, page, server}) => {
299310
# await page.goto(server.EMPTY_PAGE);
300311
# await page.evaluate(src => {
301312
# let fulfill;
Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
1-
# /**
2-
# * Copyright 2018 Google Inc. All rights reserved.
3-
# * Modifications copyright (c) Microsoft Corporation.
4-
# *
5-
# * Licensed under the Apache License, Version 2.0 (the "License");
6-
# * you may not use this file except in compliance with the License.
7-
# * You may obtain a copy of the License at
8-
# *
9-
# * http://www.apache.org/licenses/LICENSE-2.0
10-
# *
11-
# * Unless required by applicable law or agreed to in writing, software
12-
# * distributed under the License is distributed on an "AS IS" BASIS,
13-
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# * See the License for the specific language governing permissions and
15-
# * limitations under the License.
16-
# */
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
1714

18-
# const {FFOX, CHROMIUM, WEBKIT, WIN} = testOptions;
1915

20-
# it('should clear cookies', async({context, page, server}) => {
21-
# await page.goto(server.EMPTY_PAGE);
22-
# await context.addCookies([{
23-
# url: server.EMPTY_PAGE,
24-
# name: 'cookie1',
25-
# value: '1'
26-
# }]);
27-
# expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
28-
# await context.clearCookies();
29-
# expect(await context.cookies()).toEqual([]);
30-
# await page.reload();
31-
# expect(await page.evaluate('document.cookie')).toBe('');
32-
# });
16+
async def test_should_clear_cookies(context, page, server):
17+
await page.goto(server.EMPTY_PAGE)
18+
await context.addCookies(
19+
[{"url": server.EMPTY_PAGE, "name": "cookie1", "value": "1"}]
20+
)
21+
assert await page.evaluate("document.cookie") == "cookie1=1"
22+
await context.clearCookies()
23+
assert await context.cookies() == []
24+
await page.reload()
25+
assert await page.evaluate("document.cookie") == ""
3326

34-
# it('should isolate cookies when clearing', async({context, server, browser}) => {
35-
# const anotherContext = await browser.newContext();
36-
# await context.addCookies([{url: server.EMPTY_PAGE, name: 'page1cookie', value: 'page1value'}]);
37-
# await anotherContext.addCookies([{url: server.EMPTY_PAGE, name: 'page2cookie', value: 'page2value'}]);
3827

39-
# expect((await context.cookies()).length).toBe(1);
40-
# expect((await anotherContext.cookies()).length).toBe(1);
28+
async def test_should_isolate_cookies_when_clearing(context, server, browser):
29+
another_context = await browser.newContext()
30+
await context.addCookies(
31+
[{"url": server.EMPTY_PAGE, "name": "page1cookie", "value": "page1value"}]
32+
)
33+
await another_context.addCookies(
34+
[{"url": server.EMPTY_PAGE, "name": "page2cookie", "value": "page2value"}]
35+
)
4136

42-
# await context.clearCookies();
43-
# expect((await context.cookies()).length).toBe(0);
44-
# expect((await anotherContext.cookies()).length).toBe(1);
37+
assert len(await context.cookies()) == 1
38+
assert len(await another_context.cookies()) == 1
4539

46-
# await anotherContext.clearCookies();
47-
# expect((await context.cookies()).length).toBe(0);
48-
# expect((await anotherContext.cookies()).length).toBe(0);
49-
# await anotherContext.close();
50-
# });
40+
await context.clearCookies()
41+
assert len(await context.cookies()) == 0
42+
assert len(await another_context.cookies()) == 1
43+
44+
await another_context.clearCookies()
45+
assert len(await context.cookies()) == 0
46+
assert len(await another_context.cookies()) == 0
47+
await another_context.close()

0 commit comments

Comments
 (0)