Forum Discussion

GradyJr's avatar
GradyJr
Contributor
5 years ago

equivalent vb filedialog feature in testcomplete

for several situations i've been able to write a vb script and paste it into a testcomplete script and it works just fine.  My manager has requested that I look into how we can use the TestComplete File Compare to examine two files for differences but he stipulates "Ideally, when running this you would be able to select the 2 files for comparison and not hard code the file location/name into the test case". I know how to code that functionality into a VB script that allows the user to navigate to files through the Windows File Explorer using FileDialog. The following code snippet does just that (post continued below code)

 

Sub filepicker()
Dim fd As FileDialog
Dim baselinefile As String
Dim targetfile As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Navigate to and select the baseline file for comparison"
.AllowMultiSelect = False
If .Show = False Then Exit Sub
baselinefile = .SelectedItems(1)
End With
With fd
.Title = "Navigate to and select the target file for comparison"
.AllowMultiSelect = False
If .Show = False Then Exit Sub
targetfile = .SelectedItems(1)
End With
End Sub

 

This Sub however does not work in a Test Complete script. Is there anything in TestComplete that would accomplish the task of throwing up a File Explorer GUI and allow me to navigate to and select two files, save them in variables, and then perform the Files.Add(baselinefile, "Baseline") command followed by the Files.Baseline.Check(targetfile) command?

 

    • GradyJr's avatar
      GradyJr
      Contributor

      The answer to your question is simply that it is what i've been requested to do. In my initial post I shared only part of the request I received. Here is the entire text of the assignment:

       

      "TestComplete has the ability to compare 2 files to see if anything has changed.  Need to create a test case that can compare 2 files and report the changes.  Ideally, when running this you would be able to select the 2 files for comparison and not hard code the file location/name into the test case.  This is NOT something we would add to regression test at the start, but would be a manual run when needed."

       

      I believe the phrase "at the start" is an indication that, later on, we might be adding this functionality to the automation. Initially, we just want to see how it works running it manually and I would rather be able to navigate to the files and click on them rather than having to type in the file paths. Having said that, I will look through the links you provided and then let you know if anything works for my situation. Thanks!

      • GradyJr's avatar
        GradyJr
        Contributor

        Even though i still wish there was a way to accomplish this without involving a form and that it is somewhat awkward clicking on an Open button when i'm not acutally intending to open anything, Mr. Martin's solution works perfectly for my situation. My working code looks like this:

         

        Sub CompFiles
        set dialog = UserForms.FileDialog.OpenDialog1
        With dialog
        .title = "Navigate to and select the baseline file for comparison"
        If .execute Then
        baselinefile = .filename
        End If
        End With
        With dialog
        .title = "Navigate to and select the target file for comparison"
        If .execute Then
        targetfile = .filename
        End If
        End With
        If Files.Add(baselinefile, "Baseline") Then
        If Files.Baseline.Check(targetfile) Then
        Log.Message "Files Are Identical"
        Else
        Log.Message "Files Do not Match"
        End If
        ItemName = Files.NameByFileName(baselinefile)
        Call Files.Remove(ItemName)
        Else
        Log.Message (Files.LastError)
        End If
        End Sub

         

        Thank You!

    • GradyJr's avatar
      GradyJr
      Contributor

      Unfortunately, the Set openDialog = CreateObject("MSComDlg.CommonDialog line results in an "ActiveX component can't create object" error. Your suggested link, however, got me looking at some other sites and I  came pretty close to getting it. Close but no cigar. There's a function of the Shell.Application called BrowseForFolder which throws up an explorer dialog window and allows me to navigate through the directory structure. However, as the name implies, it seems to only allow me to get a specific folder or full path name and not a file name which is what i want. I've tested the following code attempting to select both a folder and a file.

       

      Set oShell = CreateObject("Shell.Application")
      Set oItem = oShell.BrowseForFolder(&H0, "Navigate to and select the baseline file for comparison", &H4000)
      If Not (oItem is Nothing) Then
      Log.Message oItem.Title
      Log.Message oItem.self.path
      Else
      Log.Message "oItem is nothing"
      End If

       

      When I select a folder the log contains two messages. The Title returns just the folder name that was selected. The self.path returns the whole pathname with the selected folder being the rightmost value. when i select a file within a folder, the log is empty. no log messages at all. Supposedly, according to the documentation i found online, putting the &H4000 value as the third parameter of BrowseForFolder is intended to include filenames:

       

      WINDOW_OPTIONS
      Const BIF_RETURNONLYFSDIRS = &H0001 (The default)
      Const BIF_DONTGOBELOWDOMAIN = &H0002
      Const BIF_STATUSTEXT = &H0004
      Const BIF_RETURNFSANCESTORS = &H0008
      Const BIF_EDITBOX = &H0010
      Const BIF_VALIDATE = &H0020
      Const BIF_NONEWFOLDER = &H0200
      Const BIF_BROWSEFORCOMPUTER = &H1000
      Const BIF_BROWSEFORPRINTER = &H2000
      Const BIF_BROWSEINCLUDEFILES = &H4000

       

      Apparently, it only displays the files in the filedialog but won't return it from the function. Why display them if you can't pick them? It doesn't look as though Shell.Application has  a BrowseForFiles function so I'm still out of luck. I haven't tried Martin's suggestion about the UserForm yet. I would really prefer something along this line of just code. If you have any idea why I got the error with the Set openDialog = CreateObject("MSComDlg.CommonDialog please let me know. that sounded like just what the doctor ordered.

       

      Thanks

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Keep in mind, the TestComplete environment, while it acts like an IDE, ISN'T one... so many of the things you might thing would work in a regular VB environment won't work in TestComplete... it's not VB, it's VB script and not all objects are available or fully supported in TestComplete.

         

        THIS is why the User Forms feature is going to be your best bet.  It is a built in feature of TestComplete to create forms to prompt for user interaction during the execution of an automated test.  Rather than attempting other methods, I'd highly suggest you go that route.