-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlocateApi.test.js
107 lines (95 loc) · 3.23 KB
/
locateApi.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { test, expect, chromium } from '@playwright/test';
test.use({
geolocation: { longitude: -73.56766530667056, latitude: 45.5027789304487 },
permissions: ['geolocation']
});
test.describe('Locate API Test', () => {
let page;
let context;
test.beforeAll(async function () {
context = await chromium.launchPersistentContext('');
page =
context.pages().find((page) => page.url() === 'about:blank') ||
(await context.newPage());
await context.grantPermissions(['geolocation']);
await page.goto('locateApi.html');
});
test.afterAll(async function () {
await context.close();
});
test('Using locate API to find myself', async () => {
await page.$eval('body > mapml-viewer', (viewer) => viewer.locate());
let locateAPI_lat = await page.$eval(
'body > mapml-viewer',
(viewer) => viewer.lat
);
let locateAPI_lng = await page.$eval(
'body > mapml-viewer',
(viewer) => viewer.lon
);
//rounding to three decimal places
locateAPI_lat = parseFloat(locateAPI_lat).toFixed(3);
locateAPI_lng = parseFloat(locateAPI_lng).toFixed(3);
expect(locateAPI_lat).toEqual('45.503');
expect(locateAPI_lng).toEqual('-73.568');
});
test('Testing map-locationfound event', async () => {
const latlng = await page.evaluate(() => {
const viewer = document.querySelector('body > mapml-viewer');
return new Promise((resolve) => {
viewer.addEventListener(
'map-locationfound',
(e) => {
resolve(e.detail.latlng);
},
{ once: true }
);
viewer.locate();
});
});
expect(latlng.lat).toEqual(45.5027789304487);
expect(latlng.lng).toEqual(-73.56766530667056);
});
test('Testing map-locationerror event', async () => {
const error = await page.evaluate(() => {
const viewer = document.querySelector('body > mapml-viewer');
return new Promise((resolve) => {
viewer.addEventListener(
'map-locationerror',
(e) => {
resolve(e.detail.error);
},
{ once: true }
);
const errorEvent = new CustomEvent('map-locationerror', {
detail: { error: 'Your location could not be determined.' }
});
viewer.dispatchEvent(errorEvent);
viewer.locate();
});
});
expect(error).toEqual('Your location could not be determined.');
});
test('Testing API when the button is used', async () => {
await page.reload();
await page.click('body > mapml-viewer');
await page.getByTitle('Show my location - location tracking off').click();
await page.mouse.move(600, 300);
await page.mouse.down();
await page.mouse.move(1200, 450, { steps: 5 });
await page.mouse.up();
await page.$eval('body > mapml-viewer', (viewer) => viewer.locate());
let locateAPI_lat = await page.$eval(
'body > mapml-viewer',
(viewer) => viewer.lat
);
let locateAPI_lng = await page.$eval(
'body > mapml-viewer',
(viewer) => viewer.lon
);
locateAPI_lat = parseFloat(locateAPI_lat).toFixed(1);
locateAPI_lng = parseFloat(locateAPI_lng).toFixed(1);
expect(locateAPI_lat).toEqual('45.5');
expect(locateAPI_lng).toEqual('-73.6');
});
});