Enable/Disable Android Location Services through Appium

HI ,

I have very recently started using Appium. I have got a scenario where in I need to enable GPS and then perform few test cases and later turn off the GPS.

Can any one please help me how can I enable/disable GPS settings using Appium.

Thanks in advance
Sunny

So that you need to launch
1)Settings app, go to location and enable(ON) it first and
2) Then you can perform your test cases by launching another app by driver.startActivity(“appPackage”,“appActivity”);
3)After all test case are done, You can repeat the same for disabling (OFF) as above in 1st step.

Hi Bhaskar

Is there any way to Enable/disable GPS through code ?

Hi @nirmal_kumar As far as i know…there is no direct implementation…!!
You can make it as like,i was written above.
:smile:

In capabilities you can give Settings appPackage and appActivity…so that you may able to perform Enable/disable in the above 1st step, and you can repeat the same in the end…!!!

@bhaskar thanks

Manually I have to enable/disable location service in mobile setting or in application setting ?
if application means i can’t fine any option like enabling location services …

In my device appPackage and appActivity are as below…You can modify like as you required…!!! Please make sure that run your appium server with the given port ,before you run the script.

package appium.settings;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.SwipeElementDirection;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidKeyCode;
import io.appium.java_client.remote.MobileCapabilityType;

import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Settings_App {
static AndroidDriver driver;
@BeforeTest
public static void setUp() throws Exception{
DesiredCapabilities mycap= new DesiredCapabilities();
mycap.setCapability(“deviceName”,“Android”);
mycap.setCapability(CapabilityType.VERSION,“4.4.2”);
mycap.setCapability(“appPackage”,“com.android.settings”);
mycap.setCapability(“appActivity”,".Settings");
mycap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT,60);
driver=new AndroidDriver(new URL(“http://0.0.0.0:4724/wd/hub”),mycap);
}
@AfterTest
public static void tearDown() {
driver.quit();
}
@Test
public static void settingsTest() throws Exception{
System.out.println(driver.currentActivity());
driver.scrollTo(“Location”);
driver.findElement(By.name(“Location”)).click();
driver.findElement(By.className(“android.widget.Switch”)).click();
Thread.sleep(5000);
driver.startActivity(“newAppPackage”,“newAppActivity”);
//Prerform of your Test cases…
/**
* After all test case are done, you can do the same.
*/
driver.startActivity(“com.android.settings”, “.Settings”);
System.out.println(driver.currentActivity());
driver.scrollTo(“Location”);
driver.findElement(By.name(“Location”)).click();
driver.findElement(By.className(“android.widget.Switch”)).click();

@nirmal_kumar .Try it once.Please let me know if it works…!!!

Regards,
Bhaskar.

@bhaskar am getting this error in my console

FAILED CONFIGURATION: @BeforeTest setUp
java.lang.NoSuchMethodError: io.appium.java_client.android.AndroidDriver.substituteMobilePlatform(Lorg/openqa/selenium/Capabilities;Ljava/lang/String;)Lorg/openqa/selenium/Capabilities;
at io.appium.java_client.android.AndroidDriver.(AndroidDriver.java:40)
at com.appium.com.appium.gps.Settings_App.setUp(Settings_App.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.beforeRun(TestRunner.java:641)
at org.testng.TestRunner.run(TestRunner.java:609)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

Please make sure the capabilities you were given correctly and check your script once…!!!

hi @bhaskar

I checked all my capabilities till same error is coming…!!!

I need to add any jar file for this ? i already added some jar file like android, selenium and java client …

where i can get setting.apk ?

In Appium with javascript I use

   return driver.toggleLocationServices()

The toggle seems to work, but how do you know the current status of the GPS?

@dduphorn As of now, I am unable to figure out a way to get the current status of GPS.
I checked the appium github page and found an open enhancement Get GPS location on Android #5047
Once that is released then we should be able to directly get the current location.

if(strGPSEnabled.equals(“True”))
{
try{Runtime.getRuntime().exec("/Users/"+strAutomationUser+"/Library/Android/sdk/platform-tools//adb shell settings put secure location_providers_allowed +network");}
catch(Exception e){}
try{Runtime.getRuntime().exec("/Users/"+strAutomationUser+"/Library/Android/sdk/platform-tools//adb shell settings put secure location_providers_allowed +gps");}
catch(Exception e){}
try{Runtime.getRuntime().exec("/Users/"+strAutomationUser+"/Library/Android/sdk/platform-tools//adb -s “+strDeviceName+” emu geo fix “+strLocation);}
catch(Exception e){}
try {Thread.sleep(2000);}catch (Exception e) {}
}
else
{
try{Runtime.getRuntime().exec(”/Users/"+strAutomationUser+"/Library/Android/sdk/platform-tools//adb shell settings put secure location_providers_allowed -network");}
catch(Exception e){}
try{Runtime.getRuntime().exec("/Users/"+strAutomationUser+"/Library/Android/sdk/platform-tools//adb shell settings put secure location_providers_allowed -gps");}
catch(Exception e){}
}

This worked for me :slight_smile:

@dduphorn this solution didn’t work for Android 6.0.1

First I tried from command line

adb shell settings put secure location_providers_allowed gps

this doesn’t enable location on device

@dduphorn quick update, below worked

adb shell settings put secure location_providers_allowed +gps

reference https://android.stackexchange.com/questions/40147/is-it-possible-to-enable-location-services-via-shell

Did you get it working for iOS as well ? thanks in advance.