Working With Files on an Android Device (Legacy)

Applies to TestComplete 15.70, last modified on December 17, 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.

You can work with files on an Android device using the methods of the FileSystemManager object. You can copy files to and from the device, delete them, find files or folders and check if a file exists. This is useful if your application produces some output or modifies a file that may later be analyzed on the computer.

Considerations for Working With the FileSystemManager Object

  • There are two types of storages on Android devices - external and internal.

    • External storage– this storage is available to the user. The user can modify any files and folders in it.

      Note: An external storage is not necessarily a removable storage device.
    • Internal storage– this storage contains Android system files and installed applications. Android does not allow saving files to this storage. However, if you have root permissions, you can execute the chmod 777 /foldername/ command to provide full access to the specified folder.

  • Android's FileSystemManager object uses case-sensitive file names. This means that /data/local/tmp/ and /Data/Local/Tmp/ are two different paths. Using a wrong case will cause an error.

Working With Files on a Mobile Device in Keyword Tests

In keyword tests, use the Call Object Method operation to call an object's methods. To configure this operation:

  • Add the Call Object Method operation to your keyword test. TestComplete will display the Operation Parameters wizard.

  • Enter Mobile.Device("MyDevice").FileSystemManager as the object name and click Next.

  • Select the method you want to use and click Next.

  • Depending on the method you chose, the following fields are displayed:

    • PathOnTheHost is used to specify the path to a file located on the computer for Copy methods.

    • PathOnTheDevice is used to specify the path to a file located on the device for Copy methods.

    • Path is used to specify the path to a file to check if it exists or to delete it.

    • FileMask is used to specify a mask when searching for a file.

    • FolderMask is used to specify a mask when searching for a folder.

    Enter the appropriate values and click Finish

The screenshot below demonstrates a keyword test that copies a file to the connected device, runs a test, copies the resulting file from the device to the computer and deletes the original file from the device.

Working with files on the mobile device

Working With Files on a Mobile Device in Script Tests

When working with the FileSystemManager object in scripts, you can call its methods directly. The example below copies a file to the connected device, then copies the resulting file from the device to the computer and deletes the original file.

JavaScript, JScript

function Test()
{
  Mobile.SetCurrent("VirtualBox");
  var PathOnHost = "C:\\Users\\Public\\Documents\\TestComplete 11 Samples\\Desktop\\Orders\\";
  var PathOnDevice = "/data/local/tmp/";
  var FileName = "MyTable.tbl";
  
  Mobile.Device().FileSystemManager.CopyToDevice(PathOnHost+FileName, PathOnDevice);
 
  // Do something
  //...
  
  Mobile.Device().FileSystemManager.CopyFromDevice(PathOnDevice+FileName, PathOnHost);
  Mobile.Device().FileSystemManager.Delete(PathOnDevice+FileName);
}

Python

def Test():
  Mobile.SetCurrent("Nexus 7");
  PathOnHost = "C:\\Users\\Public\\Documents\\TestComplete 11 Samples\\Desktop\\Orders\\";
  PathOnDevice = "/data/local/tmp/";
  FileName = "MyTable.tbl";
  
  Log.Message(Mobile.Device().FileSystemManager.CopyToDevice(PathOnHost+FileName, PathOnDevice));
 
  # Do something
  #...
  
  Mobile.Device().FileSystemManager.CopyFromDevice(PathOnDevice+FileName, PathOnHost);
  Mobile.Device().FileSystemManager.Delete(PathOnDevice+FileName);

VBScript

Sub Test()

  Mobile.SetCurrent("VirtualBox")
  Dim PathOnHost, PathOnDevice, FileName
  pathOnHost = "C:\Users\Public\Documents\TestComplete 11 Samples\Desktop\Orders\"
  pathOnDevice = "/data/local/tmp/"
  FileName = "MyTable.tbl"
  
  
  Call Mobile.Device.FileSystemManager.CopyToDevice(pathOnHost&FileName, pathOnDevice)
 

  'Mobile.Device.FileSystemManager.CopyToDevice(PathOnHost&File, PathOnDevice)
 
  ' Do something
  '...
  
  Call Mobile.Device.FileSystemManager.CopyFromDevice(pathOnDevice&FileName, pathOnHost)
  Call Mobile.Device.FileSystemManager.Delete(pathOnDevice&FileName)
End Sub

DelphiScript

procedure Test();
var
  PathOnHost, PathOnDevice, FileName;
begin
  Mobile.SetCurrent('VirtualBox');
  PathOnHost := 'C:\Users\Public\Documents\TestComplete 11 Samples\Desktop\Orders\';
  PathOnDevice := '/data/local/tmp/';
  FileName := 'MyTable.tbl';
  
  Mobile.Device.FileSystemManager.CopyToDevice(PathOnHost+FileName, PathOnDevice);
 
  // Do something
  //...
  
  Mobile.Device.FileSystemManager.CopyFromDevice(PathOnDevice+FileName, PathOnHost);
  Mobile.Device.FileSystemManager.Delete(PathOnDevice+FileName);
end;

C++Script, C#Script

function Test()
{
  Mobile["SetCurrent"]("VirtualBox");
  var PathOnHost = "C:\\Users\\Public\\Documents\\TestComplete 11 Samples\\Desktop\\Orders\\";
  var PathOnDevice = "/data/local/tmp/";
  var FileName = "MyTable.tbl";
  
  Mobile["Device"].FileSystemManager["CopyToDevice"](PathOnHost+FileName, PathOnDevice);
 
  // Do something
  //...
  
  Mobile["Device"].FileSystemManager["CopyFromDevice"](PathOnDevice+FileName, PathOnHost);
  Mobile["Device"].FileSystemManager["Delete"](PathOnDevice+FileName);
}

See Also

AndroidDesktop Object

Highlight search results