How do I set my location on a simulator? Recently I started getting a dialog while running tests that pretty much says the location of the device couldn’t be found and because of this one dialog my tests fail. Happens maybe 10% of the time. Completely random and has no specific steps in order to reproduce this problem. I’d like to set my location before each test case if possible, but not sure how?
P.S. I could possible just write something that expects the dialog to come up and handle it, but then my location isn’t set and my tests would fail anyways. Instead of some hack solution I’d like to find some long term, reliable solution like setting the location to custom before each launch or something.
I know a full-reset before each test case isn’t the best option because the regression test suite would take way longer, so I wanted to ask how other folks are handling it. I thought about bundling up some test cases because right now for each test case I relaunch the sim and install the app so that the user is always “brand new”. Any other ideas would be greatly appreciated.
Option1:
If you don’t want to handle Location services alert anywhere in your scripts, then use the below appium capabilities.
"locationServicesAuthorized" = true
"bundleId" = "bundle id name"
Option2:
If you need location services alert for some scripts and if you can’t use above capabilities, then you can use appium’s waitForAppScript capability and handle the unwanted alerts.
Ex:-
You can use the capability in this way.
"waitForAppScript", "if (target.frontMostApp().alert().name()=='the location of the device couldn't be found') {$.acceptAlert(); true;}"
This capability will take care of handling the “location not found” alert at any point of time, while running the scripts.
I would love to write something to handle the location alert whenever it comes up. However, I can’t because the results in the app I am testing depend on the location that is being returned. This means that if during the test “location not found” dialog comes up, then the whole app content will not be displayed and therefore fail the tests.
@aleksei - thank you very much!
For anyone else looking for help on how to set simulator location: Ruby: set_location({latitude: 50.000, longitude: -50.000})
So the problem is still happening…
My tests launch the emulator, set the location to XXX,XXX and I still randomly get a dialog on the simulator stating that the location wasn’t returned correctly.
I can’t think of any other solution, I’ve tried the following:
Reset the simulator for every single test case
Manually / automatically allow location services
Manually / automatically set location to XXX,XXX
What else can I do? Any help would be highly appreciated.
here it is below. NOTE: only with simulator! and on same machine where test code is running.
And only with xCODE7. with xCODE6 you need to update simulator name.
public void setLocationWithEmulator(Location loc) {
try {
String[] cmd = {"osascript", "-e",
"on menu_click(mList)\n" +
" local appName, topMenu, r\n" +
"\n" +
" -- Validate our input\n" +
" if mList's length < 3 then error \"Menu list is not long enough\"\n" +
"\n" +
" -- Set these variables for clarity and brevity later on\n" +
" set {appName, topMenu} to (items 1 through 2 of mList)\n" +
" set r to (items 3 through (mList's length) of mList)\n" +
"\n" +
" -- This overly-long line calls the menu_recurse function with\n" +
" -- two arguments: r, and a reference to the top-level menu\n" +
" tell application \"System Events\" to my menu_click_recurse(r, ((process appName)'s ¬\n" +
" (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))\n" +
"end menu_click\n" +
"\n" +
"on menu_click_recurse(mList, parentObject)\n" +
" local f, r\n" +
"\n" +
" -- `f` = first item, `r` = rest of items\n" +
" set f to item 1 of mList\n" +
" if mList's length > 1 then set r to (items 2 through (mList's length) of mList)\n" +
"\n" +
" -- either actually click the menu item, or recurse again\n" +
" tell application \"System Events\"\n" +
" if mList's length is 1 then\n" +
" click parentObject's menu item f\n" +
" else\n" +
" my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))\n" +
" end if\n" +
" end tell\n" +
"end menu_click_recurse\n" +
"\n" +
"application \""+simulatorAppName()+"\" activate \n" +
"delay 0.2\n" +
"menu_click({\""+simulatorAppName()+"\",\"Debug\", \"Location\", \"None\"})\n" +
"\n" +
"delay 0.2\n" +
"menu_click({\""+simulatorAppName()+"\",\"Debug\", \"Location\", \"Custom Location…\"})\n" +
"\n" +
"delay 0.2\n" +
"tell application \"System Events\"\n" +
" tell process \""+simulatorAppName()+"\"\n" +
" set value of text field 1 of window \"Custom Location\" to \""+loc.getLatitude()+"\"\n" +
" set value of text field 2 of window \"Custom Location\" to \""+loc.getLongitude()+"\"\n" +
" click UI Element \"OK\" of window \"Custom Location\"\n" +
" end tell\n" +
"end tell"
};
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String lsString;
while ((lsString = bufferedReader.readLine()) != null) {
System.out.println(lsString);
}
try{Thread.sleep(10000);}catch (Exception e1){}
} catch (Exception e) {}
}
public String simulatorAppName() {
return "Simulator";
}
I’m almost sure that my problem would be solved if only I could figure out the syntax in Ruby to set location to none. At the moment I’m just trying anything that comes to my mind
do you know if there is any way to set location to none just like we set location so some actual coordinates?
for example I use: set_location({latitude: 50.000, longitude: -50.000})
but I just cant figure out the syntax to set it to none… tried the following:
Hi there,
I tried this and it works but how do i reset it to the current location afterwards…??
Somehow - after quiting android driver and restarting appium also -
The location that was set earlier is being fetched on doing getLocation()