diff --git a/lib/api/client-commands/setGeolocation.js b/lib/api/client-commands/setGeolocation.js index 36c7701e90..02753d3f7d 100644 --- a/lib/api/client-commands/setGeolocation.js +++ b/lib/api/client-commands/setGeolocation.js @@ -4,6 +4,10 @@ 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', () => { @@ -22,16 +26,19 @@ const {Logger} = require('../../utils'); * }); * * @method setGeolocation - * @syntax .setGeolocation({latitude, longitude, accuracy}, [callback]) - * @param {object} [coordinates] Latitude, longitude, and accuracy. + * @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'); diff --git a/lib/api/protocol/getDeviceGeolocation.js b/lib/api/protocol/getGeolocation.js similarity index 61% rename from lib/api/protocol/getDeviceGeolocation.js rename to lib/api/protocol/getGeolocation.js index 91d351e538..affeef3bb5 100644 --- a/lib/api/protocol/getDeviceGeolocation.js +++ b/lib/api/protocol/getGeolocation.js @@ -1,15 +1,15 @@ const ProtocolAction = require('./_base-action.js'); /** - * Get the current geo-location of the mobile device. + * Get the current geolocation of the mobile device. Getting the current geolocation for desktop browsers is not supported. * * This command is a part of old [JSON Wire Protocol](https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidlocation), and works with Appium v1 only. * - * @syntax browser.getDeviceGeolocation([callback]) - * @method getDeviceGeolocation + * @syntax .getGeolocation([callback]) + * @method getGeolocation * @param {function} [callback] Callback function which is called with the result value. - * @returns {object} The current geo-location: `{latitude: number, longitude: number, altitude: number}`. - * @see setDeviceGeolocation + * @returns {object} The current geolocation: `{latitude: number, longitude: number, altitude: number}`. + * @see setGeolocation * @api protocol.mobile */ module.exports = class Session extends ProtocolAction { diff --git a/lib/api/protocol/setDeviceGeolocation.js b/lib/api/protocol/setDeviceGeolocation.js deleted file mode 100644 index 92a1104bd0..0000000000 --- a/lib/api/protocol/setDeviceGeolocation.js +++ /dev/null @@ -1,19 +0,0 @@ -const ProtocolAction = require('./_base-action.js'); - -/** - * Set the current geo-location of the mobile device. - * - * This command is a part of old [JSON Wire Protocol](https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidlocation), and works with Appium v1 only. - * - * @syntax browser.setDeviceGeolocation({latitude, longitude, altitude}, [callback]) - * @method setDeviceGeolocation - * @param {object} location The new location (`{latitude: number, longitude: number, altitude: number}`). - * @param {function} [callback] Optional callback function to be called when the command finishes. - * @see getDeviceGeolocation - * @api protocol.mobile - */ -module.exports = class Session extends ProtocolAction { - command(location, callback) { - return this.transportActions.setDeviceGeolocation(location, callback); - } -}; diff --git a/lib/transport/selenium-webdriver/method-mappings.js b/lib/transport/selenium-webdriver/method-mappings.js index 180d9ac9d0..d75538bbfa 100644 --- a/lib/transport/selenium-webdriver/method-mappings.js +++ b/lib/transport/selenium-webdriver/method-mappings.js @@ -948,7 +948,7 @@ module.exports = class MethodMappings { return { method: 'POST', path: '/location', - data: location + data: {location} }; },