/
setGeolocation.js
95 lines (80 loc) · 3.23 KB
/
setGeolocation.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
const ClientCommand = require('./_base-command.js');
const {Logger} = require('../../utils');
/**
* Mock the geolocation of the browser. Call without any arguments to reset the geolocation back to original.
*
* This command also supports mocking the geolocation of mobile devices (when automated using Appium), with a few caveats:
* - It accepts `{latitude: number, longitude: number, altitude: number}` as the first argument with `altitude` as optional.
* - Calling the command without any argument does not reset the geolocation of the mobile device.
*
* @example
* describe('mock geolocation', function() {
* it('sets the geolocation to Tokyo, Japan and then resets it', () => {
* browser
* .setGeolocation({
* latitude: 35.689487,
* longitude: 139.691706,
* accuracy: 100
* }) // sets the geolocation to Tokyo, Japan
* .navigateTo('https://www.gps-coordinates.net/my-location')
* .pause(3000)
* .setGeolocation() // resets the geolocation
* .navigateTo('https://www.gps-coordinates.net/my-location')
* .pause(3000);
* });
* });
*
* @method setGeolocation
* @syntax .setGeolocation({latitude, longitude, accuracy|altitude}, [callback])
* @param {object} [coordinates] `latitude` and `longitude` are required; `accuracy` is optional for desktop browsers, and `altitude` is optional for mobile devices.
* @param {function} [callback] Callback function to be called when the command finishes.
* @api protocol.cdp
* @since 2.2.0
* @moreinfo nightwatchjs.org/guide/network-requests/mock-geolocation.html
*/
class SetGeolocation extends ClientCommand {
performAction(callback) {
// TODO: use better predicate to find out if Appium is being used.
if (this.api.browserName === null) {
return this.transportActions.setDeviceGeolocation(this.coordinates, callback);
}
if (!this.api.isChrome() && !this.api.isEdge()) {
const error = new Error('The command .setGeolocation() is only supported in Chrome and Edge drivers');
Logger.error(error);
return callback(error);
}
const {latitude, longitude, accuracy} = this.coordinates;
if (latitude !== undefined && longitude !== undefined) {
// Both latitude and longitude are provided
const coordinates = {latitude, longitude, accuracy};
// Set accuracy as 100 if not provided.
if (accuracy === undefined) {coordinates.accuracy = 100}
this.transportActions
.setGeolocation(coordinates, callback)
.catch(err => {
Logger.error(err);
callback(err);
});
return;
}
if (latitude === undefined && longitude === undefined) {
// Clear geolocation override.
this.transportActions
.clearGeolocation(callback)
.catch(err => {
Logger.error(err);
callback(err);
});
return;
}
// Exactly one of them is undefined, throw error.
const error = new Error('Please provide both latitude and longitude while using setGeolocation.');
Logger.error(error);
return callback(error);
}
command(coordinates, callback) {
this.coordinates = coordinates || {};
return super.command(callback);
}
}
module.exports = SetGeolocation;