1. Preliminary Operations

Applies to TestComplete 15.64, last modified on May 16, 2024

The object-driven testing (ODT) functionality is deprecated. Do not use it to create new tests. It will be removed from the product in one of the future releases. As an alternative, you can create custom classes in your scripts. For more information, see Alternatives to the ODT functionality.

In this step we create a new TestComplete project and specify the source code for routines that we will use in the further steps.

  1. Select File | New | New Project from the TestComplete main menu. This will call the Create New Project wizard.
  2. On the first page of the wizard, specify the project name and the path.

    Choose the preferable scripting language.

    Select the Tested application check box and click Next.

  3. On the next page, click Desktop and, in the Application file text box, type notepad.exe.

  4. Click Finish.

  5. TestComplete will create the project and display it in the Project Explorer panel.

  6. Expand the Script project item and right-click the Unit1 item. This will invoke the item’s context menu.
  7. Select the Rename command and rename the unit from Unit1 to MainUnit.
  8. Double-click the unit to open it in the Code Editor.
  9. Copy the source code from the snippet provided below and paste it instead of default code of the unit. Make sure you have chosen the code snippet that corresponds to the scripting language.
    Tip: You can copy the whole text of a code snippet by clicking Copy Code in the snippet header.

    JavaScript

    var pNotepad, wMain;

    function Main()
    {
      try
      {
        Log.Message("Main");

        // Terminate all running Notepad instances
        while (Sys.WaitProcess("Notepad").Exists)
          Sys.Process("Notepad").Terminate();

        // Launch Notepad
        WshShell.Run("notepad.exe", SW_NORMAL);
        
        // Run the test
        ODT.Data.Run();
      }
      catch(exception)
      {
         Log.Error('Exception', exception.message);
      }

      // Close Notepad
      ODT.Classes.ClsNotepadTest.Close();
    }


    // This method of the ClsNotepadTest class obtains a reference to Notepad and its main window
    function ClsNotepadTest_Init()
    {
      Log.Message("ClsTest_Init");

      pNotepad = Sys.Process("Notepad");
      wMain = pNotepad.Window("Notepad", "*");
    }


    // This method of the ClsNotepadTest class closes Notepad
    function ClsNotepadTest_Close()
    {
      if (!strictEqual(pNotepad, null) && !strictEqual(wMain, null))
      {
        pNotepad.Close();

        if (pNotepad.Exists)
        {
          let w = pNotepad.WaitWindow("#32770", "Notepad", -1, 1000);

          if (!strictEqual(w, null) && w.Exists)
          {
            if (equal(Sys.OSInfo.Name, "WinVista") || equal(Sys.OSInfo.Name, "Win2008") || equal(Sys.OSInfo.Name, "Win7"))
              w.FindChild("WndCaption", "Do&n't Save", 2).Click();
            else
              w.Window("Button", "&No").Click();
          }
        }
      }
    }


    // This method of the ClsFile class opens a file in Notepad
    function ClsFile_Load()
    {
      Log.Message("ClsFile_Load");

      // A menu reference is encapsulated in the ClsOperation class
      let o = ODT.Classes.New("ClsOperation");
      o.MenuPath = "File|Open...";
      o.Execute();

      let w = pNotepad.Window("#32770", "Open").Window("ComboBoxEx32").Window("ComboBox").Window("Edit");
      w.wText = "";
      w.Keys(ProjectSuite.Path + This.Path + "[Enter]");
    }

    // This method of the ClsFile class checks whether the file has been loaded successfully
    function ClsFile_Check()
    {
      // You can add some code here that will perform additional checking

      Log.Message("ClsFile_Check");
    }


    // This method of the ClsOperation class selects the menu item specified in the MenuPath field
    function ClsOperation_Execute()
    {
      Log.Message("ClsOperation_Execute");

      if (!strictEqual(wMain, null))
        wMain.MainMenu.Click(This.MenuPath);
    }


    // This method of the ClsFindData class calls the Find dialog
    function ClsFindData_Find()
    {
      Log.Message("ClsFindData_Find");

      // Call the Execute method of a parent ClsOperation class
      This.Owner.Owner.Execute();

      let w = pNotepad.Window("#32770", "Find");
      w.Window("Edit").wText = "";
      w.Window("Edit").Keys(This.FindText);
      w.Window("Button", "&Find Next").Click();
      w.Window("Button", "Cancel").Click();
    }


    // This method of the ClsFindData class checks whether the operation was completed successfully
    function ClsFindData_Check()
    {
      // You can add some code here that will perform additional checking

      Log.Message("ClsFind_Check");
    }

    // This method of the ClsReplaceData class brings up the Replace dialog
    function ClsReplaceData_Replace()
    {
      Log.Message("ClsReplaceData_FindReplace");

      // Calls the Execute method of a parent ClsOperation class
      This.Owner.Owner.Execute();

      let w = pNotepad.Window("#32770", "Replace");
      w.Window("Edit", "", 1).wText = "";
      w.Window("Edit", "", 1).Keys(This.FindText);
      w.Window("Edit", "", 2).wText = "";
      w.Window("Edit", "", 2).Keys(This.ReplaceWithText);
      w.Window("Button", "&Find Next").Click();
      w.Window("Button", "&Replace").Click();
      w.Window("Button", "Cancel").Click();
    }


    // This method of the ClsReplaceData class checks whether the operation was completed successfully
    function ClsReplaceData_Check()
    {
      // You can add some code here that will perform additional checking

      Log.Message("ClsReplaceData_Check");
    }


    // This method of the TimeDate class checks whether the operation was completed successfully
    function ClsOperationTimeDate_Check()
    {
      // You can add some code here that will perform additional checking

      Log.Message("ClsOperationTimeDate_Check");
    }

    JScript

    var pNotepad, wMain;

    function Main()
    {
      try
      {
        Log.Message("Main");

        // Terminate all running Notepad instances
        while (Sys.WaitProcess("Notepad").Exists)
          Sys.Process("Notepad").Terminate();

        // Launch Notepad
        WshShell.Run("notepad.exe", SW_NORMAL);
        
        // Run the test
        ODT.Data.Run();
      }
      catch(exception)
      {
         Log.Error('Exception', exception.description);
      }

      // Close Notepad
      ODT.Classes.ClsNotepadTest.Close();
    }


    // This method of the ClsNotepadTest class obtains a reference to Notepad and its main window
    function ClsNotepadTest_Init()
    {
      Log.Message("ClsTest_Init");

      pNotepad = Sys.Process("Notepad");
      wMain = pNotepad.Window("Notepad", "*");
    }


    // This method of the ClsNotepadTest class closes Notepad
    function ClsNotepadTest_Close()
    {
      if (pNotepad != null && wMain != null)
      {
        pNotepad.Close();

        if (pNotepad.Exists)
        {
          var w = pNotepad.WaitWindow("#32770", "Notepad", -1, 1000);

          if (w != null && w.Exists)
          {
            if ((Sys.OSInfo.Name == "WinVista") || (Sys.OSInfo.Name == "Win2008") || (Sys.OSInfo.Name == "Win7"))
              w.FindChild("WndCaption", "Do&n't Save", 2).Click();
            else
              w.Window("Button", "&No").Click();
          }
        }
      }
    }


    // This method of the ClsFile class opens a file in Notepad
    function ClsFile_Load()
    {
      Log.Message("ClsFile_Load");

      // A menu reference is encapsulated in the ClsOperation class
      var o = ODT.Classes.New("ClsOperation");
      o.MenuPath = "File|Open...";
      o.Execute();

      var w = pNotepad.Window("#32770", "Open").Window("ComboBoxEx32").Window("ComboBox").Window("Edit");
      w.wText = "";
      w.Keys(ProjectSuite.Path + This.Path + "[Enter]");
    }

    // This method of the ClsFile class checks whether the file has been loaded successfully
    function ClsFile_Check()
    {
      // You can add some code here that will perform additional checking

      Log.Message("ClsFile_Check");
    }


    // This method of the ClsOperation class selects the menu item specified in the MenuPath field
    function ClsOperation_Execute()
    {
      Log.Message("ClsOperation_Execute");

      if (wMain != null)
        wMain.MainMenu.Click(This.MenuPath);
    }


    // This method of the ClsFindData class calls the Find dialog
    function ClsFindData_Find()
    {
      Log.Message("ClsFindData_Find");

      // Call the Execute method of a parent ClsOperation class
      This.Owner.Owner.Execute();

      var w = pNotepad.Window("#32770", "Find");
      w.Window("Edit").wText = "";
      w.Window("Edit").Keys(This.FindText);
      w.Window("Button", "&Find Next").Click();
      w.Window("Button", "Cancel").Click();
    }


    // This method of the ClsFindData class checks whether the operation was completed successfully
    function ClsFindData_Check()
    {
      // You can add some code here that will perform additional checking

      Log.Message("ClsFind_Check");
    }

    // This method of the ClsReplaceData class brings up the Replace dialog
    function ClsReplaceData_Replace()
    {
      Log.Message("ClsReplaceData_FindReplace");

      // Calls the Execute method of a parent ClsOperation class
      This.Owner.Owner.Execute();

      var w = pNotepad.Window("#32770", "Replace");
      w.Window("Edit", "", 1).wText = "";
      w.Window("Edit", "", 1).Keys(This.FindText);
      w.Window("Edit", "", 2).wText = "";
      w.Window("Edit", "", 2).Keys(This.ReplaceWithText);
      w.Window("Button", "&Find Next").Click();
      w.Window("Button", "&Replace").Click();
      w.Window("Button", "Cancel").Click();
    }


    // This method of the ClsReplaceData class checks whether the operation was completed successfully
    function ClsReplaceData_Check()
    {
      // You can add some code here that will perform additional checking

      Log.Message("ClsReplaceData_Check");
    }


    // This method of the TimeDate class checks whether the operation was completed successfully
    function ClsOperationTimeDate_Check()
    {
      // You can add some code here that will perform additional checking

      Log.Message("ClsOperationTimeDate_Check");
    }

    VBScript

    Dim pNotepad, wMain

    Sub Main
      On Error Resume Next
      
      Log.Message("Main")

      ' Terminate all running Notepad instances
      While Sys.WaitProcess("Notepad").Exists
        Sys.Process("Notepad").Terminate
      Wend

      ' Launch Notepad
      Call WshShell.Run("notepad.exe", SW_NORMAL)
        
      ' Run the test
      ODT.Data.Run

      If Err.Number <> 0 Then
        Call Log.Error("Error", Err.Description)
        Err.Clear
      End If

      ' Close Notepad
      ODT.Classes.ClsNotepadTest.Close
    End Sub


    ' This method of the ClsNotepadTest class obtains a reference to Notepad and its main window
    Sub ClsNotepadTest_Init
      Log.Message("ClsTest_Init")

      Set pNotepad = Sys.Process("Notepad")
      Set wMain = pNotepad.Window("Notepad", "*")
    End Sub


    ' This method of the ClsNotepadTest class closes Notepad
    Sub ClsNotepadTest_Close
      Dim w

      If (Not pNotepad Is Nothing) And (Not wMain Is Nothing) Then
        pNotepad.Close

        If pNotepad.Exists Then
          Set w = pNotepad.WaitWindow("#32770", "Notepad", -1, 1000)

          If (Not w Is Nothing) And w.Exists Then
            If (Sys.OSInfo.Name = "WinVista") Or (Sys.OSInfo.Name = "Win2008") Or (Sys.OSInfo.Name = "Win7") Then
              w.FindChild("WndCaption", "Do&n't Save", 2).Click
            Else
              w.Window("Button", "&No").Click
            End If
          End If
        End If
      End If
    End Sub


    ' This method of the ClsFile class opens a file in Notepad
    Sub ClsFile_Load
      Dim o, w

      Log.Message("ClsFile_Load")

      ' A menu reference is encapsulated in the ClsOperation class
      Set o = ODT.Classes.New("ClsOperation")
      o.MenuPath = "File|Open..."
      o.Execute

      Set w = pNotepad.Window("#32770", "Open").Window("ComboBoxEx32").Window("ComboBox").Window("Edit")
      w.wText = ""
      w.Keys(ProjectSuite.Path & This.Path & "[Enter]")
    End Sub


    ' This method of the ClsFile class checks whether the file has been loaded successfully
    Sub ClsFile_Check
      ' You can add some code here that will perform additional checking

      Log.Message("ClsFile_Check")
    End Sub


    ' This method of the ClsOperation class selects the menu item specified in the MenuPath field
    Sub ClsOperation_Execute
      Log.Message("ClsOperation_Execute")

      If Not wMain Is Nothing Then
        wMain.MainMenu.Click(This.MenuPath)
      End If
    End Sub


    ' This method of the ClsFindData class calls the Find dialog
    Sub ClsFindData_Find
      Dim w

      Log.Message("ClsFindData_Find")

      ' Call the Execute method of a parent ClsOperation class
      This.Owner.Owner.Execute

      Set w = pNotepad.Window("#32770", "Find")
      w.Window("Edit").wText = ""
      w.Window("Edit").Keys(This.FindText)
      w.Window("Button", "&Find Next").Click
      w.Window("Button", "Cancel").Click
    End Sub


    ' This method of the ClsFindData class checks whether the operation was completed successfully
    Sub ClsFindData_Check
      ' You can add some code here that will perform additional checking

      Log.Message("ClsFind_Check")
    End Sub

    ' This method of the ClsReplaceData class brings up the Replace dialog
    Sub ClsReplaceData_Replace
      Dim w

      Log.Message("ClsReplaceData_FindReplace")

      ' Calls the Execute method of a parent ClsOperation class
      This.Owner.Owner.Execute

      Set w = pNotepad.Window("#32770", "Replace")
      w.Window("Edit", "", 1).wText = ""
      w.Window("Edit", "", 1).Keys(This.FindText)
      w.Window("Edit", "", 2).wText = ""
      w.Window("Edit", "", 2).Keys(This.ReplaceWithText)
      w.Window("Button", "&Find Next").Click
      w.Window("Button", "&Replace").Click
      w.Window("Button", "Cancel").Click
    End Sub


    ' This method of the ClsReplaceData class checks whether the operation was completed successfully
    Sub ClsReplaceData_Check
      ' You can add some code here that will perform additional checking

      Log.Message("ClsReplaceData_Check")
    End Sub


    ' This method of the TimeDate class checks whether the operation was completed successfully
    Sub ClsOperationTimeDate_Check
      ' You can add some code here that will perform additional checking

      Log.Message("ClsOperationTimeDate_Check")
    End Sub

    DelphiScript

    var pNotepad, wMain;

    procedure Main;
    begin
      try
        Log.Message('Main');

        // Terminate all running Notepad instances
        while (Sys.WaitProcess('Notepad').Exists) do
          Sys.Process('Notepad').Terminate;

        // Launch Notepad
        WshShell.Run('notepad.exe', SW_NORMAL);
        
        // Run the test
        ODT.Data.Run;
      except
         Log.Error('Exception', ExceptionMessage);
      end;

      // Close Notepad
      ODT.Classes.ClsNotepadTest.Close;
    end;


    // This method of the ClsNotepadTest class obtains a reference to Notepad and its main window
    procedure ClsNotepadTest_Init;
    begin
      Log.Message('ClsTest_Init');

      pNotepad := Sys.Process('Notepad');
      wMain := pNotepad.Window('Notepad', '*');
    end;


    // This method of the ClsNotepadTest class closes Notepad
    procedure ClsNotepadTest_Close;
    var w;
    begin
      if (pNotepad <> nil) and (wMain <> nil) then
      begin
        pNotepad.Close;

        if pNotepad.Exists then
        begin
          w := pNotepad.WaitWindow('#32770', 'Notepad', -1, 1000);

          if (w <> nil) and w.Exists then
          begin
            if (Sys.OSInfo.Name = 'WinVista') or (Sys.OSInfo.Name = 'Win2008') or (Sys.OSInfo.Name = 'Win7') then
              w.FindChild('WndCaption', 'Do&n''t Save', 2).Click
            else
              w.Window('Button', '&No').Click;
          end;
        end;
      end;
    end;


    // This method of the ClsFile class opens a file in Notepad
    procedure ClsFile_Load;
    var o, w;
    begin
      Log.Message('ClsFile_Load');

      // A menu reference is encapsulated in the ClsOperation class
      o := ODT.Classes.New('ClsOperation');
      o.MenuPath := 'File|Open...';
      o.Execute;

      w := pNotepad.Window('#32770', 'Open').Window('ComboBoxEx32').Window('ComboBox').Window('Edit');
      w.wText := '';
      w.Keys(ProjectSuite.Path + Self.Path + '[Enter]');
    end;


    // This method of the ClsFile class checks whether the file has been loaded successfully
    procedure ClsFile_Check;
    begin
      // You can add some code here that will perform additional checking

      Log.Message('ClsFile_Check');
    end;


    // This method of the ClsOperation class selects the menu item specified in the MenuPath field
    procedure ClsOperation_Execute;
    begin
      Log.Message('ClsOperation_Execute');

      if wMain <> nil then
        wMain.MainMenu.Click(Self.MenuPath);
    end;


    // This method of the ClsFindData class calls the Find dialog
    procedure ClsFindData_Find;
    var w;
    begin
      Log.Message('ClsFindData_Find');

      // Call the Execute method of a parent ClsOperation class
      Self.Owner.Owner.Execute;

      w := pNotepad.Window('#32770', 'Find');
      w.Window('Edit').wText := '';
      w.Window('Edit').Keys(Self.FindText);
      w.Window('Button', '&Find Next').Click;
      w.Window('Button', 'Cancel').Click;
    end;


    // This method of the ClsFindData class checks whether the operation was completed successfully
    procedure ClsFindData_Check;
    begin
      // You can add some code here that will perform additional checking

      Log.Message('ClsFind_Check');
    end;

    // This method of the ClsReplaceData class brings up the Replace dialog
    procedure ClsReplaceData_Replace;
    var w;
    begin
      Log.Message('ClsReplaceData_FindReplace');

      // Calls the Execute method of a parent ClsOperation class
      Self.Owner.Owner.Execute;

      w := pNotepad.Window('#32770', 'Replace');
      w.Window('Edit', '', 1).wText := '';
      w.Window('Edit', '', 1).Keys(Self.FindText);
      w.Window('Edit', '', 2).wText := '';
      w.Window('Edit', '', 2).Keys(Self.ReplaceWithText);
      w.Window('Button', '&Find Next').Click;
      w.Window('Button', '&Replace').Click;
      w.Window('Button', 'Cancel').Click;
    end;


    // This method of the ClsReplaceData class checks whether the operation was completed successfully
    procedure ClsReplaceData_Check;
    begin
      // You can add some code here that will perform additional checking

      Log.Message('ClsReplaceData_Check');
    end;


    // This method of the TimeDate class checks whether the operation was completed successfully
    procedure ClsOperationTimeDate_Check;
    begin
      // You can add some code here that will perform additional checking

      Log.Message('ClsOperationTimeDate_Check');
    end;

    C++Script, C#Script

    var pNotepad, wMain;

    function Main()
    {
      try
      {
        Log["Message"]("Main");

        // Terminate all running Notepad instances
        while (Sys["WaitProcess"]("Notepad")["Exists"])
          Sys["Process"]("Notepad")["Terminate"]();

        // Launch Notepad
        WshShell["Run"]("notepad.exe", SW_NORMAL);
        
        // Run the test
        ODT["Data"]["Run"]();
      }
      catch(exception)
      {
         Log["Error"]("Exception", exception["description"]);
      }

      // Close Notepad
      ODT["Classes"]["ClsNotepadTest"]["Close"]();
    }


    // This method of the ClsNotepadTest class obtains a reference to Notepad and its main window
    function ClsNotepadTest_Init()
    {
      Log["Message"]("ClsTest_Init");

      pNotepad = Sys["Process"]("Notepad");
      wMain = pNotepad["Window"]("Notepad", "*");
    }


    // This method of the ClsNotepadTest class closes Notepad
    function ClsNotepadTest_Close()
    {
      if (pNotepad != null && wMain != null)
      {
        pNotepad["Close"]();

        if (pNotepad["Exists"])
        {
          var w = pNotepad["WaitWindow"]("#32770", "Notepad", -1, 1000);

          if (w != null && w["Exists"])
          {
            if ((Sys["OSInfo"]["Name"] == "WinVista") || (Sys["OSInfo"]["Name"] == "Win2008") || (Sys["OSInfo"]["Name"] == "Win7"))
              w["FindChild"]("WndCaption", "Do&n't Save", 2)["Click"]();
            else
              w["Window"]("Button", "&No")["Click"]();
          }
        }
      }
    }


    // This method of the ClsFile class opens a file in Notepad
    function ClsFile_Load()
    {
      Log["Message"]("ClsFile_Load");

      // A menu reference is encapsulated in the ClsOperation class
      var o = ODT["Classes"].New("ClsOperation");
      o["MenuPath"] = "File|Open...";
      o["Execute"]();

      var w = pNotepad["Window"]("#32770", "Open")["Window"]("ComboBoxEx32")["Window"]("ComboBox")["Window"]("Edit");
      w["wText"] = "";
      w["Keys"]( ProjectSuite["Path"] + This["Path"] + "[Enter]" );
    }


    // This method of the ClsFile class checks whether the file has been loaded successfully
    function ClsFile_Check()
    {
      // You can add some code here that will perform additional checking

      Log["Message"]("ClsFile_Check");
    }


    // This method of the ClsOperation class selects the menu item specified in the MenuPath field
    function ClsOperation_Execute()
    {
      Log["Message"]("ClsOperation_Execute");

      if (wMain != null)
        wMain["MainMenu"]["Click"]( This["MenuPath"] );
    }


    // This method of the ClsFindData class calls the Find dialog
    function ClsFindData_Find()
    {
      Log["Message"]("ClsFindData_Find");

      // Call the Execute method of a parent ClsOperation class
      This["Owner"]["Owner"]["Execute"]();

      var w = pNotepad["Window"]("#32770", "Find");
      w["Window"]("Edit")["wText"] = "";
      w["Window"]("Edit")["Keys"]( This["FindText"] );
      w["Window"]("Button", "&Find Next")["Click"]();
      w["Window"]("Button", "Cancel")["Click"]();
    }


    // This method of the ClsFindData class checks whether the operation was completed successfully
    function ClsFindData_Check()
    {
      // You can add some code here that will perform additional checking

      Log["Message"]("ClsFind_Check");
    }

    // This method of the ClsReplaceData class brings up the Replace dialog
    function ClsReplaceData_Replace()
    {
      Log["Message"]("ClsReplaceData_FindReplace");

      // Calls the Execute method of a parent ClsOperation class
      This["Owner"]["Owner"]["Execute"]();

      var w = pNotepad["Window"]("#32770", "Replace");
      w["Window"]("Edit", "", 1)["wText"] = "";
      w["Window"]("Edit", "", 1)["Keys"]( This["FindText"] );
      w["Window"]("Edit", "", 2)["wText"] = "";
      w["Window"]("Edit", "", 2)["Keys"]( This["ReplaceWithText"] );
      w["Window"]("Button", "&Find Next")["Click"]();
      w["Window"]("Button", "&Replace")["Click"]();
      w["Window"]("Button", "Cancel")["Click"]();
    }


    // This method of the ClsReplaceData class checks whether the operation was completed successfully
    function ClsReplaceData_Check()
    {
      // You can add some code here that will perform additional checking

      Log["Message"]("ClsReplaceData_Check");
    }


    // This method of the TimeDate class checks whether the operation was completed successfully
    function ClsOperationTimeDate_Check()
    {
      // You can add some code here that will perform additional checking

      Log["Message"]("ClsOperationTimeDate_Check");
    }

Prev     Next

See Also

Visual Creation of Custom Objects

Highlight search results