Skip to content

Commit

Permalink
fix: idb and AppleScript Geo Location setters (appium#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
nextlevelbeard authored Apr 6, 2021
1 parent 3e617ba commit dd6ed2f
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ async function setLocationWithLyft (udid, latitude, longitude) {
* @throws {Error} If it failed to set the location
*/
async function setLocationWithIdb (idb, latitude, longitude) {
if (idb) {
try {
await idb.setLocation(latitude, longitude);
} catch (e) {
throw new Error(`Failed to set geolocation with idb. Original error: ${e.stderr || e.message}`);
}
if (!idb) {
throw new Error('Failed to set geolocation with idb because it is not installed or the "launchWithIDB" capability was not set');
}

try {
await idb.setLocation(latitude, longitude);
} catch (e) {
throw new Error(`Failed to set geolocation with idb. Original error: ${e.stderr || e.message}`);
}
throw new Error('Failed to set geolocation with idb because it is not installed');
}


Expand All @@ -70,10 +71,16 @@ async function setLocationWithIdb (idb, latitude, longitude) {
* @throws {Error} If it failed to set the location
*/
async function setLocationWithAppleScript (sim, latitude, longitude, menu = 'Debug') {
const decimalSeparator = (0.1).toLocaleString().replace(/\d/g, '');
// Make sure system-wide decimal separator is used
const latitudeStr = `${latitude}`.replace(/\D/g, decimalSeparator);
const longitudeStr = `${longitude}`.replace(/\D/g, decimalSeparator);
const decimalSeparator = (await exec('/usr/bin/python', [
'-c',
'from AppKit import NSNumberFormatter;' +
'import sys;' +
'sys.stdout.write(NSNumberFormatter.alloc().init().decimalSeparator())'
])).stdout;

const [latitudeStr, longitudeStr] = [latitude, longitude].map((coord) => `${coord}`.replace(/\D/g, decimalSeparator));

const output = await sim.executeUIClientScript(`
tell application "System Events"
tell process "Simulator"
Expand Down

0 comments on commit dd6ed2f

Please sign in to comment.