Geolocation Testing (Legacy)

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.

In TestComplete, you can retrieve and emulate data obtained from a GPS receiver of your mobile device.

Note: For detailed information on Android geolocation technologies, see the Location and Maps and Location Strategies sections of the Android Developer documentation.

For detailed information on iOS geolocation technologies, see the About Location Services sections of the iOS Developer documentation.

Requirements

Android Requirements
  • Currently, TestComplete supports only GPS data providers, it cannot access network location providers that obtain location data from cell towers or Wi-Fi access points.

  • To access geolocation information:

  • To emulate location data, go to Settings > Developer options > Select mock location app > TestComplete Android Agent on the mobile device. Besides the GPS location, some applications may use other ways to determine the location of the device. In that case, select Settings > Location > Mode > Device only. Also, disable Wi-Fi and Bluetooth scanning in the Scanning menu.

    Some devices may not have the Location > Mode setting and have Google Location Accuracy instead. This setting should be switched off to disable Wi-Fi.

Emulate location data on Android 5.1 and earlier

iOS Requirements
  • The prepared application must be running on the device.

  • Location Services must be enabled on the device, and your running application must have a permission to use your location.

    To allow the application to use your location:

    1. Trigger the "AppName" Would Like to Use Your Current Location popup window on your device. You can do this from TestComplete by viewing the Mobile.Device("MyDevice").GPS.Location property in the Object Browser or by running the following code:

      JavaScript, JScript

      Mobile.Device("MyDevice").GPS.Location;

      Python

      Mobile.Device('MyDevice').GPS.Location

      VBScript

      Mobile.Device("MyDevice").GPS.Location

      DelphiScript

      Mobile.Device('MyDevice').GPS.Location;

      C++Script, C#Script

      Mobile["Device"]("MyDevice")["GPS"]["Location"];

    2. Touch OK to allow TestComplete to access the GPS data. The popup window will not appear again until you reinstall the application.

  • You can emulate location data only on mobile iOS devices.

    To emulate location data, enable simulating mock locations for the application. You can do this by using the Mobile.Device("MyDevice").GPS.AllowMockLocations property.

    The AllowMockLocations property is not persistent. It will be reset to false the next time you run the application.

About Geolocation Testing

Many mobile devices have a built-in GPS receiver that determines the geographic location of the device. Geolocation data can be used in many mobile applications: maps, navigation, tracking, cartography and so on.

When developing an application that uses geolocation data, you need to verify whether the application processes the data correctly. To test the application behavior in locations that differ from the actual location, or to test the application on emulators and virtual machines that have no GPS receiver, you may need to specify the so-called mock location - a predefined location that overrides the actual coordinates.

With TestComplete, you can retrieve GPS data from the device and create mobile tests. Besides, with TestComplete, you can specify and change mock location data directly from tests.

Accessing GPS Location Data From Scripts

To work with GPS locations on Android devices in tests, you use the AndroidGPS object; to work with locations on iOS devices, you use the iOSGPS object. You can use the object to get the current location or to set a mock location. To get the object, use the GPS property of your tested mobile device.

To learn the current location, read the Mobile.Device.GPS.Location property. It returns the current coordinates of the device.

Notes:

  • TestComplete does not determine whether the current location is a physical location (that is, retrieved from the GPS sensor) or a mock location (that is, it was emulated programmatically).

  • Location data is retrieved upon initializing the GPSData object. Once created, the GPSData object stores the values that are actual at the moment. To update the location coordinates, re-initialize the GPSData object by rereading the GPS.Location property.

    When exploring GPS data in the Object Browser, keep in mind that the Refresh All button does not re-initialize the object.

The code below retrieves location data from the mobile device’s GPS sensor, posts it to the test log and shows the location in Google Maps:

JavaScript, JScript

function PostGPSLocation()
{
  Mobile.SetCurrent("MyDevice");
  // Check whether GPS is enabled
  if (Mobile.Device().GPS.GPSEnabled)
  {
    // Obtain location data
    var Longt = Mobile.Device().GPS.Location.Longitude;
    var Lat = Mobile.Device().GPS.Location.Latitude;
    // Output the location data
    Log.Message("The device location is:");
    switch (Mobile.Device().OSType)
    {
      case "Android":
      {
        Log.Message("Longitude: " + aqConvert.VarToStr(Longt));
        Log.Message("Latitude: " + aqConvert.VarToStr(Lat));
        Log.Message("Altitude: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Altitude));
        Log.Message("Accuracy: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Accuracy));
      };
      break;
      case "iOS":
      {
        Log.Message("Longitude: " + aqConvert.VarToStr(Longt));
        Log.Message("Latitude: " + aqConvert.VarToStr(Lat));
        Log.Message("Altitude: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Altitude));
        Log.Message("Speed: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Speed));
        Log.Message("Course: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Course));
        Log.Message("Horizontal Accuracy: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.HorizontalAccuracy));
        Log.Message("Vertical Accuracy: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.VerticalAccuracy));
      };
      break;
    }
    // Open Google Maps in the browser and pass the coordinates as URL parameters
    Browsers.Item(Browsers.btIExplorer).Run("http://maps.google.com/maps?q=loc:" + aqConvert.VarToStr(Lat) + "," + aqConvert.VarToStr(Longt));
  }
}

Python

def PostGPSLocation():
  Mobile.SetCurrent('MyDevice')
  # Check if GPS is enabled
  if Mobile.Device().GPS.GPSEnabled:
    # Obtain location data
    Longt = Mobile.Device().GPS.Location.Longitude
    Lat = Mobile.Device().GPS.Location.Latitude
    # Output the location data
    Log.Message('The device location is:')
    if Mobile.Device().OSType == 'Android':
      Log.Message('Longitude: ' + IntToStr(Longt))
      Log.Message('Latitude: ' + IntToStr(Lat))
      Log.Message('Altitude: ' + VarToStr(Mobile.Device().GPS.Location.Altitude))
      Log.Message('Accuracy: '+ VarToStr(Mobile.Device().GPS.Location.Accuracy))
    elif Mobile.Device().OSType == 'iOS':
      Log.Message('Longitude: ' + IntToStr(Longt))
      Log.Message('Latitude: ' + IntToStr(Lat))
      Log.Message('Altitude: ' + VarToStr(Mobile.Device().GPS.Location.Altitude))
      Log.Message('Speed: ' + VarToStr(Mobile.Device().GPS.Location.Speed))
      Log.Message('Horizontal Accuracy: ' + VarToStr(Mobile.Device().GPS.Location.HorizontalAccuracy))
      Log.Message('Vertical Accuracy: ' + VarToStr(Mobile.Device().GPS.Location.VerticalAccuracy))
    # Open Google Maps in the browser and pass the coordinates as URL parameters
    Browsers.Item[Browsers.btIExplorer].Run('http://maps.google.com/maps?q=loc:' + IntToStr(Lat) + ',' + IntToStr(Longt))

VBScript

Sub PostGPSLocation
  Call Mobile.SetCurrent("MyDevice")
  ' Check whether GPS is enabled
  If Mobile.Device.GPS.GPSEnabled Then
    ' Obtain location data
    Longt = Mobile.Device.GPS.Location.Longitude
    Lat = Mobile.Device.GPS.Location.Latitude
    ' Output the location data
    Call Log.Message("The device location is:")
    Select Case Mobile.Device.OSType
      Case "Android"
        Call Log.Message("Longitude: "& aqConvert.VarToStr(Longt))
        Call Log.Message("Latitude: "& aqConvert.VarToStr(Lat))
        Call Log.Message("Altitude: "& aqConvert.VarToStr(Mobile.Device.GPS.Location.Altitude))
        Call Log.Message("Accuracy: "& aqConvert.VarToStr(Mobile.Device.GPS.Location.Accuracy))
      Case "iOS"
        Call Log.Message("Longitude: "& aqConvert.VarToStr(Longt))
        Call Log.Message("Latitude: "& aqConvert.VarToStr(Lat))
        Call Log.Message("Altitude: "& aqConvert.VarToStr(Mobile.Device.GPS.Location.Altitude))
        Call Log.Message("Speed: "& aqConvert.VarToStr(Mobile.Device.GPS.Location.Speed))
        Call Log.Message("Course: "& aqConvert.VarToStr(Mobile.Device.GPS.Location.Course))
        Call Log.Message("Horizontal Accuracy: "& aqConvert.VarToStr(Mobile.Device.GPS.Location.HorizontalAccuracy))
        Call Log.Message("Vertical Accuracy: "& aqConvert.VarToStr(Mobile.Device.GPS.Location.VerticalAccuracy))
    End Select
    ' Open Google Maps in the browser and pass the coordinates as URL parameters
    Call Browsers.Item(Browsers.btIExplorer).Run("http://maps.google.com/maps?q=loc:" & aqConvert.VarToStr(Lat) & "," & aqConvert.VarToStr(Longt))
  End If
End Sub

DelphiScript

procedure PostGPSLocation;
var Longt, Lat;
begin
  Mobile.SetCurrent('MyDevice');
  // Check whether GPS is enabled
  if Mobile.Device.GPS.GPSEnabled then
  begin
    // Obtain location data
    Longt := Mobile.Device.GPS.Location.Longitude;
    Lat := Mobile.Device.GPS.Location.Latitude;
    // Output the location data
    Log.Message('The device location is:');
    case Mobile.Device.OSType of
      'Android':
        begin
          Log.Message('Longitude: ' + aqConvert.VarToStr(Longt));
          Log.Message('Latitude: ' + aqConvert.VarToStr(Lat));
          Log.Message('Altitude: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Altitude));
          Log.Message('Accuracy: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Accuracy));
        end;
      'iOS':
        begin
          Log.Message('Longitude: ' + aqConvert.VarToStr(Longt));
          Log.Message('Latitude: ' + aqConvert.VarToStr(Lat));
          Log.Message('Altitude: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Altitude));
          Log.Message('Speed: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Speed));
          Log.Message('Course: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Course));
          Log.Message('Horizontal Accuracy: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.HorizontalAccuracy));
          Log.Message('Vertical Accuracy: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.VerticalAccuracy));
        end;
    end;
    // Open Google Maps in the browser and pass the coordinates as URL parameters
    Browsers.Item(Browsers.btIExplorer).Run('http://maps.google.com/maps?q=loc:'+aqConvert.VarToStr(Lat) + ',' + aqConvert.VarToStr(Longt));
  end;
end;

C++Script, C#Script

function PostGPSLocation()
{
  Mobile.SetCurrent("MyDevice");
  // Check whether GPS is enabled
  if (Mobile["Device"]["GPS"]["GPSEnabled"])
  {
    // Obtain location data
    var Longt = Mobile["Device"]["GPS"]["Location"]["Longitude"];
    var Lat = Mobile["Device"]["GPS"]["Location"]["Latitude"];
    // Output the location data
    Log["Message"]("The device location is:");
    switch (Mobile["Device"]["OSType"])
    {
      case "Android":
      {
        Log["Message"]("Longitude: " + aqConvert["VarToStr"](Longt));
        Log["Message"]("Latitude: " + aqConvert["VarToStr"](Lat));
        Log["Message"]("Altitude: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Altitude"]));
        Log["Message"]("Accuracy: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Accuracy"]));
      };
      break;
      case "iOS":
      {
        Log["Message"]("Longitude: " + aqConvert["VarToStr"](Longt));
        Log["Message"]("Latitude: " + aqConvert["VarToStr"](Lat));
        Log["Message"]("Altitude: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Altitude"]));
        Log["Message"]("Speed: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Speed"]));
        Log["Message"]("Course: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Course"]));
        Log["Message"]("Horizontal Accuracy: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["HorizontalAccuracy"]));
        Log["Message"]("Vertical Accuracy: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["VerticalAccuracy"]));
      };
      break;
    }
    // Open Google Maps in the browser and pass the coordinates as URL parameters
    Browsers["Item"](Browsers["btIExplorer"])["Run"]("http://maps.google.com/maps?q=loc:" + aqConvert["VarToStr"](Lat) + "," + aqConvert["VarToStr"](Longt));
  }
}

To specify mock location data, use the Mobile.Device.GPS.SetLocation method. The input parameters of this method define the coordinates and the accuracy of the desired location.

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

function MockGPSLocation()
{
  Mobile.SetCurrent("MyDevice");
  // Check if GPS is enabled
  if (Mobile.Device().GPS.GPSEnabled)
  {
    // Enable the "Allow mock locations" property
    Mobile.Device().GPS.AllowMockLocations = true;
    // Obtain current location data
    let Longt = Mobile.Device().GPS.Location.Longitude;
    let Lat = Mobile.Device().GPS.Location.Latitude;
    // Output the location data
    Log.Message("The current device location is:");
    PostLocationData(Mobile.Device());
    // 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
    if (equal(Mobile.Device().OSType, "Android"))
      Mobile.Device().GPS.SetLocation(Longt, Lat, Mobile.Device().GPS.Location.Altitude, Mobile.Device().GPS.Location.Accuracy);
    else
      Mobile.Device().GPS.SetLocation(Longt, Lat);
    // Output the new location data
    Log.Message("The device mock location is:");
    PostLocationData(Mobile.Device());
    // Open a new tab in the browser
    Sys.Browser("iexplore").BrowserWindow(0).Keys("[Hold]^t[Release]");
    Delay(1000);
    // 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;
  }
}

// A helper function that posts the location data to the test log
function PostLocationData(aDevice)
{
  if (!strictEqual(aDevice, null))
  {
    switch (aDevice.OSType)
    {
      case "Android":
      {
        Log.Message("Longitude: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Longitude));
        Log.Message("Latitude: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Latitude));
        Log.Message("Altitude: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Altitude));
        Log.Message("Accuracy: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Accuracy));
      };
      break;
      case "iOS":
      {
        Log.Message("Longitude: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Longitude));
        Log.Message("Latitude: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Latitude));
        Log.Message("Altitude: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Altitude));
        Log.Message("Speed: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Speed));
        Log.Message("Course: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.Course));
        Log.Message("Horizontal Accuracy: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.HorizontalAccuracy));
        Log.Message("Vertical Accuracy: " + aqConvert.VarToStr(Mobile.Device().GPS.Location.VerticalAccuracy));
      };
      break;
    }
  }
}

JScript

function MockGPSLocation()
{
  Mobile.SetCurrent("MyDevice");
  // Check if 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("The current device location is:");
    PostLocationData(Mobile.Device);
    // 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
    if (Mobile.Device.OSType == "Android")
      Mobile.Device.GPS.SetLocation(Longt, Lat, Mobile.Device.GPS.Location.Altitude, Mobile.Device.GPS.Location.Accuracy);
    else
      Mobile.Device.GPS.SetLocation(Longt, Lat);
    // Output the new location data
    Log.Message("The device mock location is:");
    PostLocationData(Mobile.Device);
    // Open a new tab in the browser
    Sys.Browser("iexplore").BrowserWindow(0).Keys("[Hold]^t[Release]");
    Delay(1000);
    // 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;
  }
}

// A helper function that posts the location data to the test log
function PostLocationData(aDevice)
{
  if (aDevice != null)
  {
    switch (aDevice.OSType)
    {
      case "Android":
      {
        Log.Message("Longitude: " + aqConvert.VarToStr(Mobile.Device.GPS.Location.Longitude));
        Log.Message("Latitude: " + aqConvert.VarToStr(Mobile.Device.GPS.Location.Latitude));
        Log.Message("Altitude: " + aqConvert.VarToStr(Mobile.Device.GPS.Location.Altitude));
        Log.Message("Accuracy: " + aqConvert.VarToStr(Mobile.Device.GPS.Location.Accuracy));
      };
      break;
      case "iOS":
      {
        Log.Message("Longitude: " + aqConvert.VarToStr(Mobile.Device.GPS.Location.Longitude));
        Log.Message("Latitude: " + aqConvert.VarToStr(Mobile.Device.GPS.Location.Latitude));
        Log.Message("Altitude: " + aqConvert.VarToStr(Mobile.Device.GPS.Location.Altitude));
        Log.Message("Speed: " + aqConvert.VarToStr(Mobile.Device.GPS.Location.Speed));
        Log.Message("Course: " + aqConvert.VarToStr(Mobile.Device.GPS.Location.Course));
        Log.Message("Horizontal Accuracy: " + aqConvert.VarToStr(Mobile.Device.GPS.Location.HorizontalAccuracy));
        Log.Message("Vertical Accuracy: " + aqConvert.VarToStr(Mobile.Device.GPS.Location.VerticalAccuracy));
      };
      break;
    }
  }
}

Python

def MockGPSLocation():
  Mobile.SetCurrent('MyDevice')
  # Check if 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:')
    PostLocationData(Mobile.Device())
    # 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
    if Mobile.Device().OSType == 'Android':
      Mobile.Device().GPS.SetLocation(Longt, Lat, Mobile.Device().GPS.Location.Altitude, Mobile.Device().GPS.Location.Accuracy)
    else:
      Mobile.Device().GPS.SetLocation(Longt, Lat)
    # Output the new location data
    Log.Message('The device mock location is:')
    PostLocationData(Mobile.Device())
    # Open a new tab in the browser
    Sys.Browser('iexplore').BrowserWindow(0).Keys('[Hold]^t[Release]')
    Delay(1000)
    # Open the new location in Google Maps
    Sys.Browser('iexplore').WaitPage('*about*').ToUrl('http://maps.google.com/maps?q=loc:' + aqConvert.VarToStr(Lat) + ',' + aqConvert.VarToStr(Longt))
    # Disable mock locations
    Mobile.Device().GPS.AllowMockLocations = False

# A helper function that posts the location data to the test log
def PostLocationData(aDevice):
  if aDevice != None:
    if aDevice.OSType == 'Android':
      Log.Message('Longitude: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Longitude))
      Log.Message('Latitude: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Latitude))
      Log.Message('Altitude: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Altitude))
      Log.Message('Accuracy: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Accuracy))
    elif aDevice.OSType == 'iOS':
      Log.Message('Longitude: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Longitude))
      Log.Message('Latitude: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Latitude))
      Log.Message('Altitude: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Altitude))
      Log.Message('Speed: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Speed))
      Log.Message('Course: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.Course))
      Log.Message('Horizontal Accuracy: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.HorizontalAccuracy))
      Log.Message('Vertical Accuracy: ' + aqConvert.VarToStr(Mobile.Device().GPS.Location.VerticalAccuracy))

VBScript

Sub MockGPSLocation
  Call Mobile.SetCurrent("MyDevice")
  ' Check if 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 PostLocationData(Mobile.Device)
    ' 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
    If Mobile.Device.OSType = "Android" Then
      Call Mobile.Device.GPS.SetLocation(Longt, Lat, Alt, Acc)
    Else
      Call Mobile.Device.GPS.SetLocation(Longt, Lat)
    End If
    
    ' Output the new location data
    Call Log.Message("The device mock location is:")
    Call PostLocationData(Mobile.Device)
    ' Open a new tab in the browser
    Call Sys.Browser("iexplore").BrowserWindow(0).Keys("[Hold]^t[Release]")
    Call Delay(1000)
    ' Open the new location in Google Maps
    Call Sys.Browser("iexplore").WaitPage("*about*", 3000).ToUrl("http://maps.google.com/maps?q=loc:"&Lat&","&Longt)
    ' Disable mock locations
    Mobile.Device.GPS.AllowMockLocations = false
  End If
End Sub

' A helper function that posts the location data to the test log
Sub PostLocationData(aDevice)
  If Not aDevice Is Nothing Then
    Select Case aDevice.OSType
      Case "Android"
        Call Log.Message("Longitude: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.Longitude))
        Call Log.Message("Latitude: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.Latitude))
        Call Log.Message("Altitude: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.Altitude))
        Call Log.Message("Accuracy: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.Accuracy))
      Case "iOS"
        Call Log.Message("Longitude: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.Longitude))
        Call Log.Message("Latitude: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.Latitude))
        Call Log.Message("Altitude: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.Altitude))
        Call Log.Message("Speed: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.Speed))
        Call Log.Message("Course: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.Course))
        Call Log.Message("Horizontal Accuracy: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.HorizontalAccuracy))
        Call Log.Message("Vertical Accuracy: " & aqConvert.VarToStr(Mobile.Device.GPS.Location.VerticalAccuracy))
    End Select
  End If
End Sub

DelphiScript

// A helper procedure that posts the location data to the test log
procedure PostLocationData(aDevice);
begin
  if aDevice <> nil then
  begin
    case aDevice.OSType of
      'Android':
      begin
        Log.Message('Longitude: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Longitude));
        Log.Message('Latitude: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Latitude));
        Log.Message('Altitude: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Altitude));
        Log.Message('Accuracy: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Accuracy));
      end;
      'iOS':
      begin
        Log.Message('Longitude: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Longitude));
        Log.Message('Latitude: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Latitude));
        Log.Message('Altitude: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Altitude));
        Log.Message('Speed: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Speed));
        Log.Message('Course: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.Course));
        Log.Message('Horizontal Accuracy: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.HorizontalAccuracy));
        Log.Message('Vertical Accuracy: ' + aqConvert.VarToStr(Mobile.Device.GPS.Location.VerticalAccuracy));
      end;
    end;
  end;
end;

procedure MockGPSLocation;
var Longt, Lat;
begin
  Mobile.SetCurrent('MyDevice');
  // Check if 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:');
    PostLocationData(Mobile.Device);
    // 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
    if Mobile.Device.OSType = 'Android' then
      Mobile.Device.GPS.SetLocation(Longt, Lat, Mobile.Device.GPS.Location.Altitude, Mobile.Device.GPS.Location.Accuracy)
    else
      Mobile.Device.GPS.SetLocation(Longt, Lat);
    // Output the new location data
    Log.Message('The device mock location is:');
    PostLocationData(Mobile.Device);
    // Open a new tab in the browser
    Sys.Browser('iexplore').BrowserWindow(0).Keys('[Hold]^t[Release]');
    Delay(1000);
    // 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"]("MyDevice");
  // Check if 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"]("The current device location is:");
    PostLocationData(Mobile["Device"]);
    // 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
    if (Mobile["Device"]["OSType"] == "Android")
      Mobile["Device"]["GPS"]["SetLocation"](Longt, Lat, Mobile["Device"]["GPS"]["Location"]["Altitude"], Mobile["Device"]["GPS"]["Location"]["Accuracy"]);
    else
      Mobile["Device"]["GPS"]["SetLocation"](Longt, Lat);
    // Output the new location data
    Log["Message"]("The device mock location is:");
    PostLocationData(Mobile["Device"]);
    // Open a new tab in the browser
    Sys["Browser"]("iexplore")["BrowserWindow"](0)["Keys"]("[Hold]^t[Release]");
    Delay(1000);
    // 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;
  }
}

// A helper function that posts the location data to the test log
function PostLocationData(aDevice)
{
  if (aDevice != null)
  {
    switch (aDevice["OSType"])
    {
      case "Android":
      {
        Log["Message"]("Longitude: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Longitude"]));
        Log["Message"]("Latitude: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Latitude"]));
        Log["Message"]("Altitude: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Altitude"]));
        Log["Message"]("Accuracy: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Accuracy"]));
      };
      break;
      case "iOS":
      {
        Log["Message"]("Longitude: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Longitude"]));
        Log["Message"]("Latitude: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Latitude"]));
        Log["Message"]("Altitude: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Altitude"]));
        Log["Message"]("Speed: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Speed"]));
        Log["Message"]("Course: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["Course"]));
        Log["Message"]("Horizontal Accuracy: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["HorizontalAccuracy"]));
        Log["Message"]("Vertical Accuracy: " + aqConvert["VarToStr"](Mobile["Device"]["GPS"]["Location"]["VerticalAccuracy"]));
      };
      break;
    }
  }
}

When you call the SetLocation method on your Android device, the device starts working in the mock location mode (that is, it does not display the physical location). To disable this mode, you need to clear the Allow mock locations setting and reboot the device.

Accessing GPS Location Data From Keyword Tests

To access GPS location data from keyword tests, you need to address the same program objects and properties described above. You can use the Call Object Method or Run Code Snippet keyword operation for this purpose, or specify operation parameters in the Code Expression mode. See Getting and Setting Object Property Values and Calling Object Methods for instructions.

For example, the following keyword test posts the current location of the device to the test log and displays it in Google Maps:

Accessing GPS data from keyword tests
Accessing GPS data from keyword tests

Samples

TestComplete includes a sample project that demonstrates how to perform geolocation testing on mobile devices:

Android

<TestComplete Samples>\Mobile\Android\Sensors

iOS

<TestComplete Samples>\Mobile\iOS\Sensors

Note: If you do not have the sample, download the TestComplete Samples installation package from the support.smartbear.com/testcomplete/downloads/samples page of our website and run it.

See Also

Geolocation Testing and Sensors (Legacy)
Sensor Property
AndroidSensor Object
Testing Android Applications (Legacy)

Highlight search results