iOSGPS.SetLocation Method

Applies to TestComplete 15.47, last modified on January 20, 2023
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.

You can emulate mock locations only on cellular iOS devices.

Once the SetLocation method is called, the application starts working in mock location mode (that is, it does not display the actual location). This mode is disabled when you quit the application.

Declaration

iOSGPS.SetLocation(Longitude, Latitude)

Longitude [in]    Required    Double    
Latitude [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).

Result Value

None.

Remarks

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()
{
  Mobile.SetCurrent("iPad");
  // Check whether GPS is enabled
  if (Mobile.Device().GPS.GPSEnabled)
  {
    // Enable the "Allow mock locations" property
    Mobile.Device().GPS.AllowMockLocations = true;
    // Obtain current location data
    var Longt = Mobile.Device().GPS.Location.Longitude;
    var Lat = Mobile.Device().GPS.Location.Latitude;
    // Output the location data
    Log.Message('Longitude: ' + aqConvert.VarToStr(Longt));
    Log.Message('Latitude: ' + aqConvert.VarToStr(Lat));
    Log.Message('Altitude: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Altitude));
    Log.Message('VerticalAccuracy: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.VerticalAccuracy));
    Log.Message('HorizontalAccuracy: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.HorizontalAccuracy));
    Log.Message('Course: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Course));
    Log.Message('Speed: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Speed));
    // 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.VarToStr(Lat) + "," + aqConvert.VarToStr(Longt));
      
    // Change the coordinates
    Longt = Longt + 0.005;
    Lat = Lat + 0.005;
    
    // Specify a mock location
    Mobile.Device().GPS.SetLocation(Longt, Lat);
    
    // Output the new location data
    Log.Message("The device mock location is:");
    Log.Message("Longitude: " + Longt);
    Log.Message("Latitude: " + Lat);
    
    // Open a new tab in the browser
    Sys.Browser("iexplore").BrowserWindow(0).Keys("^t");
    Delay(1500);
    // Open the new location in Google Maps
    Sys.Browser("iexplore").WaitPage("*about*", 3000).ToUrl("http://maps.google.com/maps?q=loc:" + aqConvert.VarToStr(Lat) + "," + aqConvert.VarToStr(Longt));
    // Disable mock locations
    Mobile.Device().GPS.AllowMockLocations = false;
}
}

Python

def MockGPSLocation():
  Mobile.SetCurrent('iPad')
  # Check whether GPS is enabled
  if Mobile.Device().GPS.GPSEnabled:
    # 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
    
    # Output the location data
    Log.Message('The current device location is:')
    Log.Message('Longitude: ' + aqConvert.VarToStr(Longt))
    Log.Message('Latitude: ' + aqConvert.VarToStr(Lat))
    Log.Message('Altitude: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Altitude));
    Log.Message('VerticalAccuracy: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.VerticalAccuracy));
    Log.Message('HorizontalAccuracy: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.HorizontalAccuracy));
    Log.Message('Course: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Course));
    Log.Message('Speed: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Speed));
    
    # 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)
  
    # Output the new location data
    Log.Message('The device mock location is:')
    Log.Message('Longitude: ' + IntToStr(Longt))
    Log.Message('Latitude: ' + IntToStr(Lat))
    
    # Open a new tab in the browser
    Sys.Browser('iexplore').BrowserWindow(0).Keys('^t')
    Delay(1500)
    # Open the new location in Google Maps
    Sys.Browser('iexplore').WaitPage('*about*', 3000).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("iPad")
  ' Check whether GPS is enabled
  If Mobile.Device.GPS.GPSEnabled Then
    ' 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
    ' Output the location data
    Call Log.Message("The current device location is:")
    Call Log.Message("Longitude: " & aqConvert.VarToStr(Longt))
    Call Log.Message("Latitude: " & aqConvert.VarToStr(Lat))
    Call Log.Message("VerticalAccuracy: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.VerticalAccuracy))
    Call Log.Message("HorizontalAccuracy: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.HorizontalAccuracy))
    Call Log.Message("Course: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.Course))
    Call Log.Message("Speed: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.Speed))
    ' 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:" & aqConvert.VarToStr(Lat) & "," & aqConvert.VarToStr(Longt))
      
    ' Change the coordinates
    Longt = Longt + 0.005
    Lat = Lat + 0.005
    
    ' Specify a mock location
    Call Mobile.Device.GPS.SetLocation(Longt, Lat)
    
    ' Output the new location data
    Call Log.Message("The device mock location is:")
    Call Log.Message("Longitude: " & aqConvert.VarToStr(Longt))
    Call Log.Message("Latitude: " & aqConvert.VarToStr(Lat))
    
    ' Open a new tab in the browser
    Call Sys.Browser("iexplore").BrowserWindow(0).Keys("^t")
    Call Delay(1500)
    ' Open the new location in Google Maps
    Call Sys.Browser("iexplore").WaitPage("*about*", 3000).ToUrl("http://maps.google.com/maps?q=loc:" & aqConvert.VarToStr(Lat) & "," & aqConvert.VarToStr(Longt))
    ' Disable mock locations
    Mobile.Device.GPS.AllowMockLocations = false
  End If
End Sub

DelphiScript

procedure MockGPSLocation;
var Longt, Lat;
begin
  Mobile.SetCurrent('iPad');
  // Check whether GPS is enabled
  if Mobile.Device.GPS.GPSEnabled then
  begin
    // 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;
    // Output the location data
    Log.Message('The current device location is:');
    Log.Message('Longitude: ' + aqConvert.VarToStr(Longt));
    Log.Message('Latitude: ' + aqConvert.VarToStr(Lat));
    Log.Message('Altitude: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Altitude));
    Log.Message('VerticalAccuracy: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.VerticalAccuracy));
    Log.Message('HorizontalAccuracy: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.HorizontalAccuracy));
    Log.Message('Course: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Course));
    Log.Message('Speed: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Speed));
    // 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.VarToStr(Lat) + ',' + aqConvert.VarToStr(Longt));
      
    // Change the coordinates
    Longt := Longt + 0.005;
    Lat := Lat + 0.005;
    
    // Specify a mock location
    Mobile.Device.GPS.SetLocation(Longt, Lat);
    
    // Output the new location data
    Log.Message('The device mock location is:');
    Log.Message('Longitude: ' + aqConvert.VarToStr(Longt));
    Log.Message('Latitude: ' + aqConvert.VarToStr(Lat));
    
    // Open a new tab in the browser
    Sys.Browser('iexplore').BrowserWindow(0).Keys('^t');
    Delay(1500);
    // Open the new location in Google Maps
    Sys.Browser('iexplore').WaitPage('*about*', 3000).ToUrl('http://maps.google.com/maps?q=loc:' + aqConvert.VarToStr(Lat) + ',' + aqConvert.VarToStr(Longt));
    // Disable mock locations
    Mobile.Device.GPS.AllowMockLocations := false;
  end;
end;

C++Script, C#Script

function MockGPSLocation()
{
  Mobile["SetCurrent"]("iPad");
  // Check whether GPS is enabled
  if (Mobile["Device"]["GPS"]["GPSEnabled"])
  {
    // 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"];
    // Output the location data
    Log["Message"]("The current device location is:");
    Log["Message"]("Longitude: " + aqConvert["VarToStr"](Longt));
    Log["Message"]("Latitude: " + aqConvert["VarToStr"](Lat));
    Log["Message"]("Altitude: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Altitude"]));
    Log["Message"]("VerticalAccuracy: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["VerticalAccuracy"]));
    Log["Message"]("HorizontalAccuracy: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["HorizontalAccuracy"]));
    Log["Message"]("Course: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Course"]));
    Log["Message"]("Speed: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Speed"]));
    // 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["VarToStr"](Lat) + "," + aqConvert["VarToStr"](Longt));
      
    // Change the coordinates
    Longt = Longt + 0.005;
    Lat = Lat + 0.005;
    
    // Specify a mock location
    Mobile["Device"]["GPS"]["SetLocation"](Longt, Lat);
    
    // Output the new location data
    Log["Message"]("The device mock location is:");
    Log["Message"]("Longitude: " + aqConvert["VarToStr"](Longt));
    Log["Message"]("Latitude: " + aqConvert["VarToStr"](Lat));
    
    // Open a new tab in the browser
    Sys["Browser"]("iexplore")["BrowserWindow"](0)["Keys"]("^t");
    Delay(1500);
    // Open the new location in Google Maps
    Sys["Browser"]("iexplore")["WaitPage"]("*about*", 3000)["ToUrl"]("http://maps.google.com/maps?q=loc:" + aqConvert["VarToStr"](Lat) + "," + aqConvert["VarToStr"](Longt));
    // Disable mock locations
    Mobile["Device"].GPS["AllowMockLocations"] = false;
  }
}

See Also

Geolocation Testing (Legacy)
iOSGPS Object
iOSGPSData Object

Highlight search results