SetLocation Method

Applies to TestComplete 15.69, last modified on November 13, 2024
The information below concerns legacy mobile tests that work with mobile devices connected to the local computer. For new mobile tests, we recommend using the newer cloud-compatible approach.

Description

Allows specifying a mock location, which is a location that overrides the actual/physical location generated by the GPS sensor. Mock locations are used for testing purposes, for example, to specify location data on emulators that have no GPS sensor. See Location Strategies in the Android developer documentation.

TestComplete can specify mock location data only when Android Agent is installed and running, and the Allow mock locations setting is enabled in the Android developer settings.

Once the SetLocation method is called, the device starts working in the mock location mode (that is, it does not display the actual location). To disable this mode, you need to clear the Allow mock locations setting and reboot the device.

Declaration

AndroidGPSObj.SetLocation(Longitude, Latitude, Altitude, Accuracy)

AndroidGPSObj An expression, variable or parameter that specifies a reference to an AndroidGPS object
Longitude [in]    Required    Double    
Latitude [in]    Required    Double    
Altitude [in]    Required    Double    
Accuracy [in]    Required    Double    
Result None

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Longitude

Specifies the desired longitude value (in degrees).

Latitude

Specifies the desired latitude value (in degrees).

Altitude

Specifies the desired altitude value (in meters above the sea level).

Accuracy

Specifies the desired accuracy (in meters) of the location.

Result Value

None.

Remarks

TestComplete replaces the data retrieved from the GPS sensor (the so-called GPS provider). The data received from a cell tower and/or from Wi-Fi access points (the so-called network location provider) are not modified.

To get the current location of the mobile device (no matter actual or mock), read the Location property.

Example

The example below demonstrates how to specify a mock location from a test. The sample code retrieves information about the current location of the device, changes the coordinates and writes the modified data to the device. The current and modified coordinates are posted to the test log and are displayed in Google Maps.

JavaScript, JScript

function MockGPSLocation()
{
  var Longt, Lat, Alt, Acc;
  Mobile.SetCurrent("Nexus 7");
  // Enable GPS
  Mobile.Device().GPS.GPSEnabled = true;
  // Enable the "Allow mock locations" property
  Mobile.Device().GPS.AllowMockLocations = true;
  // Obtain current 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;
  // Output the location data
  Log.Message("The current device location is:");
  Log.Message("Longitude: "+Longt);
  Log.Message("Latitude: "+Lat);
  Log.Message("Altitude: "+Alt);
  Log.Message("Accuracy: "+Acc);
  // Open Google Maps in the browser and pass the current coordinates as URL parameters
  Browsers.Item(Browsers.btIExplorer).Run("http://maps.google.com/maps?q=loc:"+Lat+","+Longt);
    
  // Change the coordinates
  Longt = Longt+0.005;
  Lat = Lat+0.005;
  
  // Specify a mock location
  Mobile.Device().GPS.SetLocation(Longt,Lat,Alt,Acc);
  
  // Output the new location data
  Log.Message("The device mock location is:");
  Log.Message("Longitude: "+Longt);
  Log.Message("Latitude: "+Lat);
  Log.Message("Altitude: "+Alt);
  Log.Message("Accuracy: "+Acc);
  
  // Open a new tab in the browser
  Sys.Browser("iexplore").BrowserWindow(0).Keys("^t"); Delay(1000);
  // Open the new location in Google Maps
  Sys.Browser("iexplore").Page("about:Tabs").ToUrl("http://maps.google.com/maps?q=loc:"+Lat+","+Longt);
  // Disable mock locations
  Mobile.Device().GPS.AllowMockLocations = false;
}

Python

def MockGPSLocation():
  Mobile.SetCurrent("Nexus 7")
  # Enable GPS
  Mobile.Device().GPS.GPSEnabled = True
  # Enable the "Allow mock locations" property
  Mobile.Device().GPS.AllowMockLocations = True
  # Obtain current 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
  # Output the location data
  Log.Message("The current device location is:")
  Log.Message("Longitude: "+ IntToStr(Longt))
  Log.Message("Latitude: "+IntToStr(Lat))
  Log.Message("Altitude: "+IntToStr(Alt))
  Log.Message("Accuracy: "+IntToStr(Acc))
  # Open Google Maps in the browser and pass the current coordinates as URL parameters
  Browsers.Item[Browsers.btIExplorer].Run("http://maps.google.com/maps?q=loc:"+IntToStr(Lat)+","+IntToStr(Longt))
    
  # Change the coordinates
  Longt = Longt+0.005
  Lat = Lat+0.005
  
  # Specify a mock location
  Mobile.Device().GPS.SetLocation(Longt,Lat,Alt,Acc)
  
  # Output the new location data
  Log.Message("The device mock location is:")
  Log.Message("Longitude: "+IntToStr(Longt))
  Log.Message("Latitude: "+IntToStr(Lat))
  Log.Message("Altitude: "+IntToStr(Alt))
  Log.Message("Accuracy: "+IntToStr(Acc))
  
  # Open a new tab in the browser
  Sys.Browser("iexplore").BrowserWindow(0).Keys("^t"); Delay(1000)
  # Open the new location in Google Maps
  Sys.Browser("iexplore").Page("about:blank").ToUrl("http://maps.google.com/maps?q=loc:"+IntToStr(Lat)+","+IntToStr(Longt))
  # Disable mock locations
  Mobile.Device().GPS.AllowMockLocations = False

VBScript

Sub MockGPSLocation
  Call Mobile.SetCurrent("Nexus 7")
  ' Enable GPS
  Mobile.Device.GPS.GPSEnabled = true
  ' Enable the "Allow mock locations" property
  Mobile.Device.GPS.AllowMockLocations = true
  ' Obtain current 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
  ' Output the location data
  Call Log.Message("The current device location is:")
  Call Log.Message("Longitude: "&Longt)
  Call Log.Message("Latitude: "&Lat)
  Call Log.Message("Altitude: "&Alt)
  Call Log.Message("Accuracy: "&Acc)
  ' Open Google Maps in the browser and pass the current coordinates as URL parameters
  Call Browsers.Item(Browsers.btIExplorer).Run("http://maps.google.com/maps?q=loc:"&Lat&","&Longt)
    
  ' Change the coordinates
  Longt = Longt+0.005
  Lat = Lat+0.005
  
  ' Specify a mock location
  Call Mobile.Device.GPS.SetLocation(Longt,Lat,Alt,Acc)
  
  ' Output the new location data
  Call Log.Message("The device mock location is:")
  Call Log.Message("Longitude: "&Longt)
  Call Log.Message("Latitude: "&Lat)
  Call Log.Message("Altitude: "&Alt)
  Call Log.Message("Accuracy: "&Acc)
  
  ' Open a new tab in the browser
  Call Sys.Browser("iexplore").BrowserWindow(0).Keys("^t")
  Call Delay(1000)
  ' Open the new location in Google Maps
  Call Sys.Browser("iexplore").Page("about:Tabs").ToUrl("http://maps.google.com/maps?q=loc:"&Lat&","&Longt)
End Sub  ' Disable mock locations
  Mobile.Device.GPS.AllowMockLocations = false

DelphiScript

function MockGPSLocation;
var Longt, Lat, Alt, Acc;
begin
  Mobile.SetCurrent('Nexus 7');
  // Enable GPS
  Mobile.Device.GPS.GPSEnabled := true;
  // Enable the "Allow mock locations" property
  Mobile.Device.GPS.AllowMockLocations := true;
  // Obtain current 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;
  // Output the location data
  Log.Message('The current device location is:');
  Log.Message('Longitude: '+aqConvert.FloatToStr(Longt));
  Log.Message('Latitude: '+aqConvert.FloatToStr(Lat));
  Log.Message('Altitude: '+aqConvert.FloatToStr(Alt));
  Log.Message('Accuracy: '+aqConvert.FloatToStr(Acc));
  // Open Google Maps in the browser and pass the current coordinates as URL parameters
  Browsers.Item(Browsers.btIExplorer).Run('http://maps.google.com/maps?q=loc:'+aqConvert.FloatToStr(Lat)+','+aqConvert.FloatToStr(Longt));
    
  // Change the coordinates
  Longt := Longt+0.005;
  Lat := Lat+0.005;
  
  // Specify a mock location
  Mobile.Device.GPS.SetLocation(Longt,Lat,Alt,Acc);
  
  // Output the new location data
  Log.Message('The device mock location is:');
  Log.Message('Longitude: '+aqConvert.FloatToStr(Longt));
  Log.Message('Latitude: '+aqConvert.FloatToStr(Lat));
  Log.Message('Altitude: '+aqConvert.FloatToStr(Alt));
  Log.Message('Accuracy: '+aqConvert.FloatToStr(Acc));
  
  // Open a new tab in the browser
  Sys.Browser('iexplore').BrowserWindow(0).Keys('^t'); Delay(1000);
  // Open the new location in Google Maps
  Sys.Browser('iexplore').Page('about:Tabs').ToUrl('http://maps.google.com/maps?q=loc:'+aqConvert.FloatToStr(Lat)+','+aqConvert.FloatToStr(Longt));
  // Disable mock locations
  Mobile.Device.GPS.AllowMockLocations := false;
end;

C++Script, C#Script

function MockGPSLocation()
{
  var Longt, Lat, Alt, Acc;
  Mobile["SetCurrent"]("Nexus 7");
  // Enable GPS
  Mobile["Device"].GPS["GPSEnabled"] = true;
  // Enable the "Allow mock locations" property
  Mobile["Device"].GPS["AllowMockLocations"] = true;
  // Obtain current 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"];
  // Output the location data
  Log["Message"]("The current device location is:");
  Log["Message"]("Longitude: "+Longt);
  Log["Message"]("Latitude: "+Lat);
  Log["Message"]("Altitude: "+Alt);
  Log["Message"]("Accuracy: "+Acc);
  // Open Google Maps in the browser and pass the current coordinates as URL parameters
  Browsers["Item"](Browsers["btIExplorer"])["Run"]("http://maps.google.com/maps?q=loc:"+Lat+","+Longt);
    
  // Change the coordinates
  Longt = Longt+0.005;
  Lat = Lat+0.005;
  
  // Specify a mock location
  Mobile["Device"]["GPS"]["SetLocation"](Longt,Lat,Alt,Acc);
  
  // Output the new location data
  Log["Message"]("The device mock location is:");
  Log["Message"]("Longitude: "+Longt);
  Log["Message"]("Latitude: "+Lat);
  Log["Message"]("Altitude: "+Alt);
  Log["Message"]("Accuracy: "+Acc);
  
  // Open a new tab in the browser
  Sys["Browser"]("iexplore")["BrowserWindow"](0)["Keys"]("^t"); Delay(1000);
  // Open the new location in Google Maps
  Sys["Browser"]("iexplore")["Page"]("about:Tabs")["ToUrl"]("http://maps.google.com/maps?q=loc:"+Lat+","+Longt);
  // Disable mock locations
  Mobile["Device"].GPS["AllowMockLocations"] = false;
}

See Also

Geolocation Testing (Legacy)
AndroidGPS Object
AndroidGPSData Object

Highlight search results