Skip to content

Commit

Permalink
feat: return the result of getGeoLocation if available for ios 17+ (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
KazuCocoa authored Feb 18, 2024
1 parent 473c7c5 commit fc0ba2c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Returns the location of the device under test. Location Services for WebDriverAg
The 'latitude', 'longitude' and 'altitude' could be zero even if the Location Services are set to
'Always', because the device may need some time to update the location data.

For iOS 17+ simulators and real devices, this method will return the result of
[`mobile: getSimulatedLocation`](./execute-methods.md#mobile-getsimulatedlocation) extension
if the simulated location was previously set by [`mobile: setSimulatedLocation`](./execute-methods.md#mobile-setsimulatedlocation).

**`Throws`**

If the device under test returns an error message. i.e.: tvOS returns unsupported error
Expand Down
19 changes: 18 additions & 1 deletion lib/commands/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,29 @@ export default {
* if the Location Services are set to 'Always', because the device
* needs some time to update the location data.
*
* For iOS 17, the return value could be the result of
* "mobile:getSimulatedLocation" if the simulated location has been previously set
* "mobile:setSimulatedLocation" already.
*
* @returns {Promise<import('./types').LocationWithAltitude>}
* @throws {Error} If the device under test returns an error message.
* i.e.: tvOS returns unsupported error
* @this {XCUITestDriver}
*/
async getGeoLocation() {
// Currently we proxy the setGeoLocation to mobile:setSimulatedLocation for iOS 17+.
// It would be helpful to address to use "mobile:getSimulatedLocation" for iOS 17+.
if (this.opts.platformVersion && util.compareVersions(this.opts.platformVersion, '>=', '17.0')) {
const {latitude, longitude} = await this.mobileGetSimulatedLocation();
if (latitude && longitude) {
this.log.debug('Returning the geolocation that has been previously set by mobile:setSimulatedLocation. ' +
'mobile:resetSimulatedLocation can reset the location configuration.');
return {latitude, longitude, altitude: 0};
}

this.log.warn(`No location was set by mobile:setSimulatedLocation. Trying to return the location from the device.`);
}

// Please do not change the way to get the location here with '/wda/simulatedLocation'
// endpoint because they could return different value before setting the simulated location.
// '/wda/device/location' returns current device location information,
Expand Down Expand Up @@ -66,7 +83,7 @@ export default {
}

if (this.opts.platformVersion && util.compareVersions(this.opts.platformVersion, '>=', '17.0')) {
this.log.debug(`Proxy mobile:setSimulatedLocation method as iOS 17+ platform version`);
this.log.info(`Proxying to mobile:setSimulatedLocation method for iOS 17+ platform version`);
await this.mobileSetSimulatedLocation(latitude, longitude);
} else {
const service = await services.startSimulateLocationService(this.opts.udid);
Expand Down

0 comments on commit fc0ba2c

Please sign in to comment.