Ask a Question

Set Mock location on Android

SOLVED
MadhuraKhare
New Contributor

Set Mock location on Android

In geolocation testing on a TC56 device, Android Agent is not getting set as mock location app.

"Select mock location app" setting in developer options is also disabled, I have attached a screenshot for the same.

I am trying to access the script and show a dialog based on it.

Below is code snippet which I used to set mock location.

function MockGPSLocation()
{
var Longt, Lat, Alt, Acc, Gps;

// Enable GPS
Mobile.Device().GPS.GPSEnabled = true;

// Enable the "Allow mock locations" property
Mobile.Device().GPS.AllowMockLocations = true;

Longt = 174.8367068133983;
Lat = -36.87015422083155;
Alt = Mobile.Device().GPS.Location.Altitude;
Acc = Mobile.Device().GPS.Location.Accuracy;

// Specify a mock location
Mobile.Device().GPS.SetLocation(Longt,Lat,Alt,Acc);

// Output the location data
Longt = Mobile.Device().GPS.Location.Longitude;
Lat = Mobile.Device().GPS.Location.Latitude;
Alt = Mobile.Device().GPS.Location.Altitude;
Acc = Mobile.Device().GPS.Location.Accuracy;
Gps = Mobile.Device().GPS.GPSEnabled;
Log.Message("Longitude: "+Longt);
Log.Message("Latitude: "+Lat);
Log.Message("Altitude: "+Alt);
Log.Message("Accuracy: "+Acc);
Log.Message("GPSEnabled: "+Gps);

// Disable mock locations
Mobile.Device().GPS.AllowMockLocations = false;
}

 

 

2 REPLIES 2
Marsha_R
Community Hero

I suggest that you contact Support directly with this question.  Here's the link:

https://support.smartbear.com/message/?prod=TestComplete


Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame

The issue got fixed after performing the below steps:-

1. Downloaded the GPS test app the link which was given by one of the SmartBear representatives.

Link - https://play.google.com/store/apps/details?id=com.chartcross.gpstest 

2. Check the In View and In Use count.

3. If the count is 0 then killing the adb instance. So that count becomes greater than 0.

4. Then executing the script.

5. Created a separate script to disable the mock location rather than disabling in the same script.

6. Disabling the mock location in the same script was just setting the location first and then disabling it and on UI you couldn't see any change.

7. So in short, killing the adb instance and checking for the fix in GPS test app before executing the script fixed the issue.

 

Code snippet:-

function MockGPSLocation()
{
var Longt, Lat, Alt, Acc, Gps;

// Enable GPS
Mobile.Device().GPS.GPSEnabled = true;

Longt = Mobile.Device().GPS.Location.Longitude;
Lat = Mobile.Device().GPS.Location.Latitude;
Log.Message("Original Longitude: "+Longt);
Log.Message("Original Latitude: "+Lat);

// Enable the "Allow mock locations" property
Mobile.Device().GPS.AllowMockLocations = true;

Longt = 174.8367068133983;
Lat = -36.87015422083155;
Alt = Mobile.Device().GPS.Location.Altitude;
Acc = Mobile.Device().GPS.Location.Accuracy;

// Specify a mock location
Mobile.Device().GPS.SetLocation(Longt,Lat,Alt,Acc);

// Output the location data
Longt = Mobile.Device().GPS.Location.Longitude;
Lat = Mobile.Device().GPS.Location.Latitude;
Alt = Mobile.Device().GPS.Location.Altitude;
Acc = Mobile.Device().GPS.Location.Accuracy;
Gps = Mobile.Device().GPS.GPSEnabled;
Log.Message("Longitude: "+Longt);
Log.Message("Latitude: "+Lat);
Log.Message("Altitude: "+Alt);
Log.Message("Accuracy: "+Acc);
Log.Message("GPSEnabled: "+Gps);

}

 

function DisableMockLocation()
{
Mobile.Device().GPS.AllowMockLocations = false;
}

cancel
Showing results for 
Search instead for 
Did you mean: