Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add setting geolocation by set-simulator-location as the first method #249

Merged
merged 9 commits into from
Dec 7, 2019

Conversation

KazuCocoa
Copy link
Member

@KazuCocoa KazuCocoa commented Dec 6, 2019

https://github.com/lyft/set-simulator-location

This works on Xcode 11.2 environment on my env. (against iOS 12 and iOS 13)
Current idb does not work well on Xcode 11 env, so, let's try another way before idb try.

  • ok
[HTTP] --> POST /wd/hub/session/857ac20d-beac-4409-ac0a-75a37aa833c9/location
[HTTP] {"location":{"latitude":34.18583,"longitude":131.47139,"altitude":0}}
[debug] [W3C (857ac20d)] Calling AppiumDriver.setGeoLocation() with args: [{"latitude":34.18583,"longitude":131.47139,"altitude":0},"857ac20d-beac-4409-ac0a-75a37aa833c9"]
[debug] [XCUITest] Executing command 'setGeoLocation'
[debug] [W3C (857ac20d)] Responding to client with driver.setGeoLocation() result: null
[HTTP] <-- POST /wd/hub/session/857ac20d-beac-4409-ac0a-75a37aa833c9/location 200 279 ms - 14
[HTTP]
  • fail
[HTTP] --> POST /wd/hub/session/ae50e600-8efd-440a-8a92-89f50018ff24/location
[HTTP] {"location":{"latitude":34.18583,"longitude":131.47139,"altitude":0}}
[debug] [W3C (ae50e600)] Calling AppiumDriver.setGeoLocation() with args: [{"latitude":34.18583,"longitude":131.47139,"altitude":0},"ae50e600-8efd-440a-8a92-89f50018ff24"]
[debug] [XCUITest] Executing command 'setGeoLocation'
[iOSSim] Cannot set geolocation with set-simulator-location, because it is not installed or failed to set.
[iOSSim] Cannot set geolocation with idb, because it is not installed. Defaulting to AppleScript
[iOSSim] Cannot focus Simulator window with idb. Defaulting to AppleScript
[debug] [iOSSim] Executing UI Apple Script on Simulator with UDID 3709FCFA-C989-437A-9929-615944085CB8:
[debug] [iOSSim]       tell application "System Events"
[debug] [iOSSim]         tell process "Simulator"
[debug] [iOSSim]           set frontmost to false
[debug] [iOSSim]           set frontmost to true
[debug] [iOSSim]           click (menu item 1 where (its name contains "iPhone 11 " and its name contains "13.2")) of menu 1 of menu bar item "Window" of menu bar 1
[debug] [iOSSim]         end tell
[debug] [iOSSim]       end tell
[debug] [iOSSim]
[debug] [iOSSim]
[debug] [iOSSim]       tell application "System Events"
[debug] [iOSSim]         tell process "Simulator"
[debug] [iOSSim]           set featureName to "Custom Location"
[debug] [iOSSim]           set dstMenuItem to menu item (featureName & "…") of menu 1 of menu item "Location" of menu 1 of menu bar item "Debug" of menu bar 1
[debug] [iOSSim]           click dstMenuItem
[debug] [iOSSim]           delay 1
[debug] [iOSSim]           set value of text field 1 of window featureName to 34.18583 as string
[debug] [iOSSim]           delay 0.5
[debug] [iOSSim]           set value of text field 2 of window featureName to 131.47139 as string
[debug] [iOSSim]           delay 0.5
[debug] [iOSSim]           click button "OK" of window featureName
[debug] [iOSSim]           delay 0.5
[debug] [iOSSim]           set isInvisible to (not (exists (window featureName)))
[debug] [iOSSim]         end tell
[debug] [iOSSim]       end tell
[debug] [iOSSim]
[debug] [iOSSim] Geolocation parameters dialog accepted: true
[debug] [iOSSim]
[debug] [W3C (ae50e600)] Responding to client with driver.setGeoLocation() result: null
[HTTP] <-- POST /wd/hub/session/ae50e6

@KazuCocoa KazuCocoa changed the title feat: Add set geolocation by set-simulator-location as the first method feat: Add setting geolocation by set-simulator-location as the first method Dec 6, 2019
]);
return true;
} catch (ign) {}
log.warn(`Cannot set geolocation with ${LYFT_SET_LOCATION}, because it is not installed or failed to set.`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather log the warning inside catch block and additionally print the stderr output. fs.which should be in separate block, so we could supply more detailed instructions on how to install the tool if it is not installed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be also more logical to extract the whole stuff to a separate method/helper function

* into the corresponding edit field, for example '19,0068'.
* @return {boolean} True if the given geolocation was able to set.
*/
async function execLyftSetLocation (udid, latitude, longitude) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this function to throw an exception in case of failure rather then returning a boolean result

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same below

lib/geolocation.js Outdated Show resolved Hide resolved
* into the corresponding edit field, for example '19,0068'.
* @return {boolean} True if the given geolocation was able to set.
*/
async function execIbdSetLocation (idb, latitude, longitude) {
Copy link
Contributor

@mykola-mokhnach mykola-mokhnach Dec 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather rename the functions to setLocationWith...

`);
log.debug(`Geolocation parameters dialog accepted: ${output}`);
return _.isString(output) && output.trim() === 'true';
return await execLyftSetLocation(this.udid, latitude, longitude)
Copy link
Contributor

@mykola-mokhnach mykola-mokhnach Dec 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method should throw an error if all the possible set location methods failed

*/
async function execLyftSetLocation (udid, latitude, longitude) {
async function setLocationWithLyftSetLocation (udid, latitude, longitude) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setLocationWithLyft should be enough

*/
async function execLyftSetLocation (udid, latitude, longitude) {
async function setLocationWithLyftSetLocation (udid, latitude, longitude) {
try {
await fs.which(LYFT_SET_LOCATION);
} catch (e) {
log.info(`'${LYFT_SET_LOCATION}' binary has not been found in your PATH. ` +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an exception should be throw here

} catch (e) {
log.warn(`Failed to set geolocation with '${LYFT_SET_LOCATION}'. ` +
throw new Error(`Failed to set geolocation with '${LYFT_SET_LOCATION}'. ` +
`Original error: ${e.message}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.stderr || e.message

}
log.info(`Cannot set geolocation with idb, because it is not installed`);
return false;
throw new Error('Failed to set geolocation with idb. because it is not installed');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...with idb because...

@@ -91,9 +87,11 @@ async function execAppleScript (sim, latitude, longitude) {
end tell
`);
log.debug(`Geolocation parameters dialog accepted: ${output}`);
return _.isString(output) && output.trim() === 'true';
if (_.isString(output) && output.trim() !== 'true') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (_.trim(output) !== 'true') {
  throw new Error('Failed to set geolocation with AppleScript. Original error: ${output}');
}

*/
async function execIbdSetLocation (idb, latitude, longitude) {
async function setLocationWithIbdSetLocation (idb, latitude, longitude) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setLocationWithIdb should be fine (also there's a typo ;) )

return await execLyftSetLocation(this.udid, latitude, longitude)
|| await execIbdSetLocation(this.idb, latitude, longitude)
|| await execAppleScript(this, latitude, longitude);
try {
Copy link
Contributor

@mykola-mokhnach mykola-mokhnach Dec 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const locationSetters = [
  async () => await setLocationWithLyft(this.udid, latitude, longitude),
  async () => await setLocationWithIbdSetLocation(this.idb, latitude, longitude),
  ...
];

let lastError;
for (const setter of locationSetters) {
  try {
    await setter();
    return true;
  } catch (e) {
    log.info(e.message);
    lastError = e;
  }
}
throw lastError;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow...

@KazuCocoa KazuCocoa merged commit 7d11c85 into appium:master Dec 7, 2019
@KazuCocoa KazuCocoa deleted the km/add-set-geolocation branch December 7, 2019 01:11
@imurchie imurchie mentioned this pull request Dec 11, 2019
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants