| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2018 Google Inc. All rights reserved. | 
					
						
							| 
									
										
										
										
											2019-12-11 05:21:51 +08:00
										 |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-14 02:13:28 +08:00
										 |  |  | module.exports.describe = function({testRunner, expect, FFOX, CHROMIUM, WEBKIT}) { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |   const {describe, xdescribe, fdescribe} = testRunner; | 
					
						
							| 
									
										
										
										
											2019-12-20 07:47:35 +08:00
										 |  |  |   const {it, fit, xit, dit} = testRunner; | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |   const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |   describe('BrowserContext.cookies', function() { | 
					
						
							|  |  |  |     it('should return no cookies in pristine browser context', async({context, page, server}) => { | 
					
						
							|  |  |  |       expect(await context.cookies()).toEqual([]); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should get a cookie', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       await page.evaluate(() => { | 
					
						
							|  |  |  |         document.cookie = 'username=John Doe'; | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       expect(await context.cookies()).toEqual([{ | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: 'username', | 
					
						
							|  |  |  |         value: 'John Doe', | 
					
						
							|  |  |  |         domain: 'localhost', | 
					
						
							|  |  |  |         path: '/', | 
					
						
							|  |  |  |         expires: -1, | 
					
						
							|  |  |  |         httpOnly: false, | 
					
						
							|  |  |  |         secure: false, | 
					
						
							|  |  |  |         session: true, | 
					
						
							|  |  |  |         sameSite: 'None', | 
					
						
							|  |  |  |       }]); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should properly report httpOnly cookie', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       server.setRoute('/empty.html', (req, res) => { | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:46 +08:00
										 |  |  |         res.setHeader('Set-Cookie', 'name=value;HttpOnly; Path=/'); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         res.end(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       const cookies = await context.cookies(); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(cookies.length).toBe(1); | 
					
						
							|  |  |  |       expect(cookies[0].httpOnly).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:46 +08:00
										 |  |  |     it.skip(WEBKIT && process.platform === 'linux')('should properly report "Strict" sameSite cookie', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       server.setRoute('/empty.html', (req, res) => { | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:46 +08:00
										 |  |  |         res.setHeader('Set-Cookie', 'name=value;SameSite=Strict'); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         res.end(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       const cookies = await context.cookies(); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(cookies.length).toBe(1); | 
					
						
							|  |  |  |       expect(cookies[0].sameSite).toBe('Strict'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:46 +08:00
										 |  |  |     it.skip(WEBKIT && process.platform === 'linux')('should properly report "Lax" sameSite cookie', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       server.setRoute('/empty.html', (req, res) => { | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:46 +08:00
										 |  |  |         res.setHeader('Set-Cookie', 'name=value;SameSite=Lax'); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         res.end(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       const cookies = await context.cookies(); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(cookies.length).toBe(1); | 
					
						
							|  |  |  |       expect(cookies[0].sameSite).toBe('Lax'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should get multiple cookies', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       await page.evaluate(() => { | 
					
						
							|  |  |  |         document.cookie = 'username=John Doe'; | 
					
						
							|  |  |  |         document.cookie = 'password=1234'; | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       const cookies = await context.cookies(); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       cookies.sort((a, b) => a.name.localeCompare(b.name)); | 
					
						
							|  |  |  |       expect(cookies).toEqual([ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'password', | 
					
						
							|  |  |  |           value: '1234', | 
					
						
							|  |  |  |           domain: 'localhost', | 
					
						
							|  |  |  |           path: '/', | 
					
						
							|  |  |  |           expires: -1, | 
					
						
							|  |  |  |           httpOnly: false, | 
					
						
							|  |  |  |           secure: false, | 
					
						
							|  |  |  |           session: true, | 
					
						
							|  |  |  |           sameSite: 'None', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           name: 'username', | 
					
						
							|  |  |  |           value: 'John Doe', | 
					
						
							|  |  |  |           domain: 'localhost', | 
					
						
							|  |  |  |           path: '/', | 
					
						
							|  |  |  |           expires: -1, | 
					
						
							|  |  |  |           httpOnly: false, | 
					
						
							|  |  |  |           secure: false, | 
					
						
							|  |  |  |           session: true, | 
					
						
							|  |  |  |           sameSite: 'None', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:46 +08:00
										 |  |  |     it('should get cookies from multiple urls', async({context}) => { | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       await context.setCookies([{ | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         url: 'https://foo.com', | 
					
						
							|  |  |  |         name: 'doggo', | 
					
						
							|  |  |  |         value: 'woofs', | 
					
						
							|  |  |  |       }, { | 
					
						
							|  |  |  |         url: 'https://bar.com', | 
					
						
							|  |  |  |         name: 'catto', | 
					
						
							|  |  |  |         value: 'purrs', | 
					
						
							|  |  |  |       }, { | 
					
						
							|  |  |  |         url: 'https://baz.com', | 
					
						
							|  |  |  |         name: 'birdo', | 
					
						
							|  |  |  |         value: 'tweets', | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       }]); | 
					
						
							|  |  |  |       const cookies = await context.cookies('https://foo.com', 'https://baz.com'); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       cookies.sort((a, b) => a.name.localeCompare(b.name)); | 
					
						
							|  |  |  |       expect(cookies).toEqual([{ | 
					
						
							|  |  |  |         name: 'birdo', | 
					
						
							|  |  |  |         value: 'tweets', | 
					
						
							|  |  |  |         domain: 'baz.com', | 
					
						
							|  |  |  |         path: '/', | 
					
						
							|  |  |  |         expires: -1, | 
					
						
							|  |  |  |         httpOnly: false, | 
					
						
							|  |  |  |         secure: true, | 
					
						
							|  |  |  |         session: true, | 
					
						
							|  |  |  |         sameSite: 'None', | 
					
						
							|  |  |  |       }, { | 
					
						
							|  |  |  |         name: 'doggo', | 
					
						
							|  |  |  |         value: 'woofs', | 
					
						
							|  |  |  |         domain: 'foo.com', | 
					
						
							|  |  |  |         path: '/', | 
					
						
							|  |  |  |         expires: -1, | 
					
						
							|  |  |  |         httpOnly: false, | 
					
						
							|  |  |  |         secure: true, | 
					
						
							|  |  |  |         session: true, | 
					
						
							|  |  |  |         sameSite: 'None', | 
					
						
							|  |  |  |       }]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:46 +08:00
										 |  |  |   describe('BrowserContext.setCookies', function() { | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should work', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       await context.setCookies([{ | 
					
						
							|  |  |  |         url: server.EMPTY_PAGE, | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: 'password', | 
					
						
							|  |  |  |         value: '123456' | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       }]); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(await page.evaluate(() => document.cookie)).toEqual('password=123456'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-12-19 08:23:05 +08:00
										 |  |  |     it('should isolate cookies in browser contexts', async({context, server, newContext}) => { | 
					
						
							|  |  |  |       const anotherContext = await newContext(); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       await context.setCookies([{url: server.EMPTY_PAGE, name: 'page1cookie', value: 'page1value'}]); | 
					
						
							|  |  |  |       await anotherContext.setCookies([{url: server.EMPTY_PAGE, name: 'page2cookie', value: 'page2value'}]); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       const cookies1 = await context.cookies(); | 
					
						
							|  |  |  |       const cookies2 = await anotherContext.cookies(); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(cookies1.length).toBe(1); | 
					
						
							|  |  |  |       expect(cookies2.length).toBe(1); | 
					
						
							|  |  |  |       expect(cookies1[0].name).toBe('page1cookie'); | 
					
						
							|  |  |  |       expect(cookies1[0].value).toBe('page1value'); | 
					
						
							|  |  |  |       expect(cookies2[0].name).toBe('page2cookie'); | 
					
						
							|  |  |  |       expect(cookies2[0].value).toBe('page2value'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should set multiple cookies', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       await context.setCookies([{ | 
					
						
							|  |  |  |         url: server.EMPTY_PAGE, | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: 'password', | 
					
						
							|  |  |  |         value: '123456' | 
					
						
							|  |  |  |       }, { | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |         url: server.EMPTY_PAGE, | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: 'foo', | 
					
						
							|  |  |  |         value: 'bar' | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       }]); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(await page.evaluate(() => { | 
					
						
							|  |  |  |         const cookies = document.cookie.split(';'); | 
					
						
							|  |  |  |         return cookies.map(cookie => cookie.trim()).sort(); | 
					
						
							|  |  |  |       })).toEqual([ | 
					
						
							|  |  |  |         'foo=bar', | 
					
						
							|  |  |  |         'password=123456', | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should have |expires| set to |-1| for session cookies', async({context, server}) => { | 
					
						
							|  |  |  |       await context.setCookies([{ | 
					
						
							|  |  |  |         url: server.EMPTY_PAGE, | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: 'password', | 
					
						
							|  |  |  |         value: '123456' | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       }]); | 
					
						
							|  |  |  |       const cookies = await context.cookies(); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(cookies[0].session).toBe(true); | 
					
						
							|  |  |  |       expect(cookies[0].expires).toBe(-1); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should set cookie with reasonable defaults', async({context, server}) => { | 
					
						
							|  |  |  |       await context.setCookies([{ | 
					
						
							|  |  |  |         url: server.EMPTY_PAGE, | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: 'password', | 
					
						
							|  |  |  |         value: '123456' | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       }]); | 
					
						
							|  |  |  |       const cookies = await context.cookies(); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(cookies.sort((a, b) => a.name.localeCompare(b.name))).toEqual([{ | 
					
						
							|  |  |  |         name: 'password', | 
					
						
							|  |  |  |         value: '123456', | 
					
						
							|  |  |  |         domain: 'localhost', | 
					
						
							|  |  |  |         path: '/', | 
					
						
							|  |  |  |         expires: -1, | 
					
						
							|  |  |  |         httpOnly: false, | 
					
						
							|  |  |  |         secure: false, | 
					
						
							|  |  |  |         session: true, | 
					
						
							|  |  |  |         sameSite: 'None', | 
					
						
							|  |  |  |       }]); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should set a cookie with a path', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       await context.setCookies([{ | 
					
						
							|  |  |  |         domain: 'localhost', | 
					
						
							|  |  |  |         path: '/grid.html', | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: 'gridcookie', | 
					
						
							|  |  |  |         value: 'GRID', | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       }]); | 
					
						
							|  |  |  |       expect(await context.cookies()).toEqual([{ | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: 'gridcookie', | 
					
						
							|  |  |  |         value: 'GRID', | 
					
						
							|  |  |  |         domain: 'localhost', | 
					
						
							|  |  |  |         path: '/grid.html', | 
					
						
							|  |  |  |         expires: -1, | 
					
						
							|  |  |  |         httpOnly: false, | 
					
						
							|  |  |  |         secure: false, | 
					
						
							|  |  |  |         session: true, | 
					
						
							|  |  |  |         sameSite: 'None', | 
					
						
							|  |  |  |       }]); | 
					
						
							|  |  |  |       expect(await page.evaluate('document.cookie')).toBe('gridcookie=GRID'); | 
					
						
							|  |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       expect(await page.evaluate('document.cookie')).toBe(''); | 
					
						
							|  |  |  |       await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							|  |  |  |       expect(await page.evaluate('document.cookie')).toBe('gridcookie=GRID'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should not set a cookie with blank page URL', async function({context, server}) { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       let error = null; | 
					
						
							|  |  |  |       try { | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |         await context.setCookies([ | 
					
						
							|  |  |  |             {url: server.EMPTY_PAGE, name: 'example-cookie', value: 'best'}, | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |             {url: 'about:blank', name: 'example-cookie-blank', value: 'best'} | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       } catch (e) { | 
					
						
							|  |  |  |         error = e; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       expect(error.message).toEqual( | 
					
						
							|  |  |  |           `Blank page can not have cookie "example-cookie-blank"` | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should not set a cookie on a data URL page', async function({context}) { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       let error = null; | 
					
						
							|  |  |  |       try { | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |         await context.setCookies([{url: 'data:,Hello%2C%20World!', name: 'example-cookie', value: 'best'}]); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       } catch (e) { | 
					
						
							|  |  |  |         error = e; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       expect(error.message).toContain('Data URL page can not have cookie "example-cookie"'); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should default to setting secure cookie for HTTPS websites', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       const SECURE_URL = 'https://example.com'; | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       await context.setCookies([{ | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         url: SECURE_URL, | 
					
						
							|  |  |  |         name: 'foo', | 
					
						
							|  |  |  |         value: 'bar', | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       }]); | 
					
						
							|  |  |  |       const [cookie] = await context.cookies(SECURE_URL); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(cookie.secure).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should be able to set unsecure cookie for HTTP website', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       const HTTP_URL = 'http://example.com'; | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       await context.setCookies([{ | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         url: HTTP_URL, | 
					
						
							|  |  |  |         name: 'foo', | 
					
						
							|  |  |  |         value: 'bar', | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       }]); | 
					
						
							|  |  |  |       const [cookie] = await context.cookies(HTTP_URL); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(cookie.secure).toBe(false); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |     it('should set a cookie on a different domain', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       await context.setCookies([{ | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         url: 'https://www.example.com', | 
					
						
							|  |  |  |         name: 'example-cookie', | 
					
						
							|  |  |  |         value: 'best', | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       }]); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(await page.evaluate('document.cookie')).toBe(''); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       expect(await context.cookies('https://www.example.com')).toEqual([{ | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: 'example-cookie', | 
					
						
							|  |  |  |         value: 'best', | 
					
						
							|  |  |  |         domain: 'www.example.com', | 
					
						
							|  |  |  |         path: '/', | 
					
						
							|  |  |  |         expires: -1, | 
					
						
							|  |  |  |         httpOnly: false, | 
					
						
							|  |  |  |         secure: true, | 
					
						
							|  |  |  |         session: true, | 
					
						
							|  |  |  |         sameSite: 'None', | 
					
						
							|  |  |  |       }]); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-12-13 10:27:07 +08:00
										 |  |  |     it('should set cookies from a frame', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       await page.goto(server.PREFIX + '/grid.html'); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       await context.setCookies([ | 
					
						
							|  |  |  |         {url: server.PREFIX, name: 'localhost-cookie', value: 'best'}, | 
					
						
							|  |  |  |         {url: server.CROSS_PROCESS_PREFIX, name: '127-cookie', value: 'worst'} | 
					
						
							|  |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       await page.evaluate(src => { | 
					
						
							|  |  |  |         let fulfill; | 
					
						
							|  |  |  |         const promise = new Promise(x => fulfill = x); | 
					
						
							|  |  |  |         const iframe = document.createElement('iframe'); | 
					
						
							|  |  |  |         document.body.appendChild(iframe); | 
					
						
							|  |  |  |         iframe.onload = fulfill; | 
					
						
							|  |  |  |         iframe.src = src; | 
					
						
							|  |  |  |         return promise; | 
					
						
							|  |  |  |       }, server.CROSS_PROCESS_PREFIX); | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       expect(await page.evaluate('document.cookie')).toBe('localhost-cookie=best'); | 
					
						
							|  |  |  |       expect(await page.frames()[1].evaluate('document.cookie')).toBe('127-cookie=worst'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       expect(await context.cookies(server.PREFIX)).toEqual([{ | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: 'localhost-cookie', | 
					
						
							|  |  |  |         value: 'best', | 
					
						
							|  |  |  |         domain: 'localhost', | 
					
						
							|  |  |  |         path: '/', | 
					
						
							|  |  |  |         expires: -1, | 
					
						
							|  |  |  |         httpOnly: false, | 
					
						
							|  |  |  |         secure: false, | 
					
						
							|  |  |  |         session: true, | 
					
						
							|  |  |  |         sameSite: 'None', | 
					
						
							|  |  |  |       }]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       expect(await context.cookies(server.CROSS_PROCESS_PREFIX)).toEqual([{ | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: '127-cookie', | 
					
						
							|  |  |  |         value: 'worst', | 
					
						
							|  |  |  |         domain: '127.0.0.1', | 
					
						
							|  |  |  |         path: '/', | 
					
						
							|  |  |  |         expires: -1, | 
					
						
							|  |  |  |         httpOnly: false, | 
					
						
							|  |  |  |         secure: false, | 
					
						
							|  |  |  |         session: true, | 
					
						
							|  |  |  |         sameSite: 'None', | 
					
						
							|  |  |  |       }]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-03 08:36:46 +08:00
										 |  |  |   describe('BrowserContext.setCookies', function() { | 
					
						
							|  |  |  |     it('should clear cookies', async({context, page, server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       await context.setCookies([{ | 
					
						
							|  |  |  |         url: server.EMPTY_PAGE, | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |         name: 'cookie1', | 
					
						
							|  |  |  |         value: '1' | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       }]); | 
					
						
							|  |  |  |       expect(await page.evaluate('document.cookie')).toBe('cookie1=1'); | 
					
						
							|  |  |  |       await context.clearCookies(); | 
					
						
							|  |  |  |       expect(await page.evaluate('document.cookie')).toBe(''); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-12-19 08:23:05 +08:00
										 |  |  |     it('should isolate cookies when clearing', async({context, server, newContext}) => { | 
					
						
							|  |  |  |       const anotherContext = await newContext(); | 
					
						
							| 
									
										
										
										
											2019-11-27 03:23:13 +08:00
										 |  |  |       await context.setCookies([{url: server.EMPTY_PAGE, name: 'page1cookie', value: 'page1value'}]); | 
					
						
							|  |  |  |       await anotherContext.setCookies([{url: server.EMPTY_PAGE, name: 'page2cookie', value: 'page2value'}]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect((await context.cookies()).length).toBe(1); | 
					
						
							|  |  |  |       expect((await anotherContext.cookies()).length).toBe(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       await context.clearCookies(); | 
					
						
							|  |  |  |       expect((await context.cookies()).length).toBe(0); | 
					
						
							|  |  |  |       expect((await anotherContext.cookies()).length).toBe(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       await anotherContext.clearCookies(); | 
					
						
							|  |  |  |       expect((await context.cookies()).length).toBe(0); | 
					
						
							|  |  |  |       expect((await anotherContext.cookies()).length).toBe(0); | 
					
						
							| 
									
										
										
										
											2019-11-19 10:18:28 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; |