Working With Open File and Save File Dialogs

Applies to TestComplete 15.48, last modified on March 01, 2023

Most modern applications use the standard Open File and Save File dialogs that are provided by the operating system. These dialogs let you choose a folder, explore files located in it, choose the file type and select the desired file. The dialogs “remember” the location of the last opened (or saved) file. So, when you open the dialog next time, it will show the contents of the folder in which the last opened (or saved) file resides. You should keep this in mind when designing your automated tests.

For example, suppose that during test recording, the Open File dialog points to the C:\My Folder1 folder and the file that you need to open is in the C:\My Files\My Folder2 folder. In the dialog, you navigate to the target folder and open the needed file. TestComplete will record all your actions over this dialog. However, since the dialog remembers the path to the file opened last time, it will open the C:\My Files\My Folder2 folder when the test opens the dialog during the subsequent test runs. This can cause an error, because the recorded actions may be simulated incorrectly.

To work around this problem, use the OpenFile and SaveFile methods that TestComplete provides for the Open File and Save File dialogs. During test recording, TestComplete recognizes the standard Open File and Save File dialogs and records calls to the OpenFile or SaveFile method rather than your actions over the dialogs:

OpenFile method

Click the image to enlarge it.

These methods simulate the file selection operation in the Open File or Save File dialog respectively. They make your tests shorter, easier to understand and more reliable.

To call the methods from keyword tests, use the On-Screen Action operation:

Calling the OpenFile method from a keyword test

Click the image to enlarge it.

The following script demonstrates the use of the OpenFile and SaveFile methods. It opens a text file in Notepad and then saves the file to the desktop. The example requires that Notepad be running. The example also uses the C:\MyFile.txt file. Create the file manually before running the script, or modify the script to use another file you find suitable:

JavaScript

function WorkWithFiles()
{
  var notepad = Sys.Process("notepad");
  var wndNotepad = notepad.Window("Notepad");

  // Open a file in Notepad
  wndNotepad.MainMenu.Click("File|Open...");
  notepad.Window("#32770", "Open").OpenFile("C:\\MyFile.txt");

  // Save the file to the desktop
  var strDesktop = WshShell.SpecialFolders.Item("Desktop");
  wndNotepad.MainMenu.Click("File|Save as...");
  notepad.Window("#32770", "Save As").SaveFile(strDesktop + "\\MyFile.txt");
}

JScript

function WorkWithFiles()
{
  var notepad = Sys.Process("notepad");
  var wndNotepad = notepad.Window("Notepad");

  // Open a file in Notepad
  wndNotepad.MainMenu.Click("File|Open...");
  notepad.Window("#32770", "Open").OpenFile("C:\\MyFile.txt");

  // Save the file to the desktop
  var strDesktop = WshShell.SpecialFolders("Desktop");
  wndNotepad.MainMenu.Click("File|Save as...");
  notepad.Window("#32770", "Save As").SaveFile(strDesktop + "\\MyFile.txt");
}

Python

def WorkWithFiles():
  notepad = Sys.Process("notepad")
  wndNotepad = notepad.Window("Notepad")

  # Open a file in Notepad
  wndNotepad.MainMenu.Click("File|Open...")
  notepad.Window("#32770", "Open").OpenFile("C:\\MyFile.txt")

  # Save the file to the desktop
  strDesktop = WshShell.SpecialFolders.Item("Desktop")
  wndNotepad.MainMenu.Click("File|Save as...")
  notepad.Window("#32770", "Save As").SaveFile(strDesktop + "\\MyFile.txt")

VBScript

Sub WorkWithFiles
  Dim notepad, wndNotepad, strDesktop

  Set notepad = Sys.Process("notepad")
  Set wndNotepad = notepad.Window("Notepad")

  ' Open a file in Notepad
  wndNotepad.MainMenu.Click("File|Open...")
  notepad.Window("#32770", "Open").OpenFile "C:\MyFile.txt"

  ' Save the file to the desktop
  strDesktop = WshShell.SpecialFolders("Desktop")
  wndNotepad.MainMenu.Click "File|Save as..."
  notepad.Window("#32770", "Save As").SaveFile strDesktop & "\MyFile.txt"
End Sub

DelphiScript

procedure WorkWithFiles;
var notepad, wndNotepad, strDesktop;
begin
  notepad := Sys.Process('notepad');
  wndNotepad := notepad.Window('Notepad');

  // Open a file in Notepad
  wndNotepad.MainMenu.Click('File|Open...');
  notepad.Window('#32770', 'Open').OpenFile('C:\MyFile.txt');
 
  // Save the file to the desktop
  strDesktop := WshShell.SpecialFolders('Desktop');
  wndNotepad.MainMenu.Click('File|Save as...');
  notepad.Window('#32770', 'Save As').SaveFile(strDesktop + '\MyFile.txt');
end;

C++Script, C#Script

function WorkWithFiles()
{
  var notepad = Sys["Process"]("notepad");
  var wndNotepad = notepad["Window"]("Notepad");

  // Open a file in Notepad
  wndNotepad["MainMenu"]["Click"]("File|Open...");
  notepad["Window"]("#32770", "Open")["OpenFile"]("C:\\MyFile.txt");

  // Save the file to the desktop
  var strDesktop = WshShell["SpecialFolders"]("Desktop");
  wndNotepad["MainMenu"]["Click"]("File|Save as...");
  notepad["Window"]("#32770", "Save As")["SaveFile"](strDesktop + "\\MyFile.txt");
}

See Also

OpenFile Method
SaveFile Method

Highlight search results