| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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-08-19 21:32:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 11:11:32 -07:00
										 |  |  | import { browserTest as it, expect } from './config/browserTest'; | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work', async ({ server, contextFactory }) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |   const context = await contextFactory(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await context.grantPermissions(['geolocation']); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   await context.setGeolocation({ longitude: 10, latitude: 10 }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const geolocation = await page.evaluate(() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => { | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     resolve({ latitude: position.coords.latitude, longitude: position.coords.longitude }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   }))); | 
					
						
							|  |  |  |   expect(geolocation).toEqual({ | 
					
						
							|  |  |  |     latitude: 10, | 
					
						
							|  |  |  |     longitude: 10 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should throw when invalid longitude', async ({ contextFactory }) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |   const context = await contextFactory(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   let error = null; | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     await context.setGeolocation({ longitude: 200, latitude: 10 }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   } catch (e) { | 
					
						
							|  |  |  |     error = e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(error.message).toContain('geolocation.longitude: precondition -180 <= LONGITUDE <= 180 failed.'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should isolate contexts', async ({ server, contextFactory, browser }) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |   const context = await contextFactory(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await context.grantPermissions(['geolocation']); | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   await context.setGeolocation({ longitude: 10, latitude: 10 }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const context2 = await browser.newContext({ | 
					
						
							|  |  |  |     permissions: ['geolocation'], | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     geolocation: { longitude: 20, latitude: 20 } | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   const page2 = await context2.newPage(); | 
					
						
							|  |  |  |   await page2.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const geolocation = await page.evaluate(() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => { | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     resolve({ latitude: position.coords.latitude, longitude: position.coords.longitude }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   }))); | 
					
						
							|  |  |  |   expect(geolocation).toEqual({ | 
					
						
							|  |  |  |     latitude: 10, | 
					
						
							|  |  |  |     longitude: 10 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const geolocation2 = await page2.evaluate(() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => { | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     resolve({ latitude: position.coords.latitude, longitude: position.coords.longitude }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   }))); | 
					
						
							|  |  |  |   expect(geolocation2).toEqual({ | 
					
						
							|  |  |  |     latitude: 20, | 
					
						
							|  |  |  |     longitude: 20 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await context2.close(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should throw with missing latitude', async ({ contextFactory }) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |   const context = await contextFactory(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   let error = null; | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2020-09-09 03:06:52 -07:00
										 |  |  |     // @ts-expect-error setGeolocation must have latitude
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     await context.setGeolocation({ longitude: 10 }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   } catch (e) { | 
					
						
							|  |  |  |     error = e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(error.message).toContain('geolocation.latitude: expected number, got undefined'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should not modify passed default options object', async ({ browser }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const geolocation = { longitude: 10, latitude: 10 }; | 
					
						
							|  |  |  |   const options = { geolocation }; | 
					
						
							|  |  |  |   const context = await browser.newContext(options); | 
					
						
							|  |  |  |   await context.setGeolocation({ longitude: 20, latitude: 20 }); | 
					
						
							|  |  |  |   expect(options.geolocation).toBe(geolocation); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should throw with missing longitude in default options', async ({ browser }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   let error = null; | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2020-09-09 03:06:52 -07:00
										 |  |  |     // @ts-expect-error geolocation must have longitude
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     const context = await browser.newContext({ geolocation: { latitude: 10 } }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |     await context.close(); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     error = e; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   expect(error.message).toContain('geolocation.longitude: expected number, got undefined'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should use context options', async ({ browser, server }) => { | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   const options = { geolocation: { longitude: 10, latitude: 10 }, permissions: ['geolocation'] }; | 
					
						
							|  |  |  |   const context = await browser.newContext(options); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const geolocation = await page.evaluate(() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => { | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |     resolve({ latitude: position.coords.latitude, longitude: position.coords.longitude }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   }))); | 
					
						
							|  |  |  |   expect(geolocation).toEqual({ | 
					
						
							|  |  |  |     latitude: 10, | 
					
						
							|  |  |  |     longitude: 10 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await context.close(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('watchPosition should be notified', async ({ server, contextFactory }) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |   const context = await contextFactory(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await context.grantPermissions(['geolocation']); | 
					
						
							|  |  |  |   await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |   const messages = []; | 
					
						
							|  |  |  |   page.on('console', message => messages.push(message.text())); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   await context.setGeolocation({ latitude: 0, longitude: 0 }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.evaluate(() => { | 
					
						
							|  |  |  |     navigator.geolocation.watchPosition(pos => { | 
					
						
							|  |  |  |       const coords = pos.coords; | 
					
						
							|  |  |  |       console.log(`lat=${coords.latitude} lng=${coords.longitude}`); | 
					
						
							|  |  |  |     }, err => {}); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   await context.setGeolocation({ latitude: 0, longitude: 10 }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.waitForEvent('console', message => message.text().includes('lat=0 lng=10')); | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   await context.setGeolocation({ latitude: 20, longitude: 30 }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.waitForEvent('console', message => message.text().includes('lat=20 lng=30')); | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   await context.setGeolocation({ latitude: 40, longitude: 50 }); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await page.waitForEvent('console', message => message.text().includes('lat=40 lng=50')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const allMessages = messages.join('|'); | 
					
						
							|  |  |  |   expect(allMessages).toContain('lat=0 lng=10'); | 
					
						
							|  |  |  |   expect(allMessages).toContain('lat=20 lng=30'); | 
					
						
							|  |  |  |   expect(allMessages).toContain('lat=40 lng=50'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-08-05 11:43:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should use context options for popup', async ({ contextFactory, server }) => { | 
					
						
							| 
									
										
										
										
											2021-04-02 21:07:45 -07:00
										 |  |  |   const context = await contextFactory(); | 
					
						
							|  |  |  |   const page = await context.newPage(); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   await context.grantPermissions(['geolocation']); | 
					
						
							|  |  |  |   await context.setGeolocation({ longitude: 10, latitude: 10 }); | 
					
						
							|  |  |  |   const [popup] = await Promise.all([ | 
					
						
							|  |  |  |     page.waitForEvent('popup'), | 
					
						
							| 
									
										
										
										
											2020-08-28 04:20:29 -07:00
										 |  |  |     page.evaluate(url => window['_popup'] = window.open(url), server.PREFIX + '/geolocation.html'), | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   await popup.waitForLoadState(); | 
					
						
							| 
									
										
										
										
											2020-08-11 15:50:53 -07:00
										 |  |  |   const geolocation = await popup.evaluate(() => window['geolocationPromise']); | 
					
						
							| 
									
										
										
										
											2020-08-03 15:23:53 -07:00
										 |  |  |   expect(geolocation).toEqual({ longitude: 10, latitude: 10 }); | 
					
						
							|  |  |  | }); |