SilkTest: running programs and opening files

@ Fr, 30 November 2007, 15:45

There are several ways of the opening different files in SilkTest. In this blog post we'll describe 3 ways and when it is better to use them.

 

  1. SYS_Execute() function is the easiest method. It allows to run executable files (*.com, *.exe, *.bat). If you decided to use it, you should realize that script execution will be postponed until SYS_Execute function ends.

    Here is an example of using SYS_Execute:

    LIST OF STRING lsOut
    Print (SYS_Execute ("start C:\test_file.bat", lsOut))
    Print (lsOut)

    Beside this, you can open any associated file using this function. For example,

    Print (SYS_Execute ("start C:\temp.txt"))

    Here, file C:\temp.txt will be opened in Notepad.
    Please notice, that in this case script will continue execution after the file is opened. However, this will not work for executables. For example,

    Print (SYS_Execute ("start C:\test_file.bat"))

    Script execution will be stopped until the test_file.bat stops.

  2. Method Start(). As a matter of fact, this method is declared for main windows (MainWin) and should be used for running application. When using it, the main window which method Start() was used must open. Like this,   

    window MainWin wCalculator
     tag "Calculator"

    testcase TestStartMethod () appstate none
     wCalculator.Start ("C:\WINDOWS\system32\calc.exe")

    This example will work without mistakes only if Calculator window opens. Otherwise, we will get an error message in the log

    *** Error: Window '[MainWin]Calculator1' was not found

    However, we can use it to create a function which will ignore the error message. In the example below we will use Start() method to open Display Properties dialog, which doesn't have main window.

    window DialogBox DisplayProperties
     tag "Display Properties"

     void MySYS_Execute(STRING sFileName)
     withoptions
      BindAgentOption (OPT_WINDOW_TIMEOUT, 0)
      MainWin("!!!!!!").Start(sFileName)

     testcase TestStartMethod () appstate none
     MySYS_Execute ("control.exe desk.cpl")

    Certainly, this function can (and should) be improved (for instance, it should check whether the file exists), but this is not a subject of this topic. 

  3. Using Win API function ShellExecute(). This is the most convenient way for opening most files and run executables.

    Here is an example of its usage,

    use "msw32.inc"
    dll "shell32.dll"
     HINSTANCE ShellExecute (in HWND hwnd,in STRING lpOperation,in STRING lpFile,in STRING lpParameters,in STRING lpDirectory, in INT nShowCmd) alias "ShellExecuteA"

    testcase TestStartMethod () appstate none
     ShellExecute (0, "open", "C:\temp.txt", "", "", 1)

    For SilkTest International and SilkTest v.8.0 and higher, another declaration of ShellExecute() is needed 

    HINSTANCE ShellExecute (in HWND hwnd,in STRING lpOperation,in STRING lpFile,in STRING lpParameters,in STRING lpDirectory, in INT nShowCmd) alias "ShellExecuteW"

    You can learn more about Shellexecute() function here
    http://msdn2.microsoft.com/en-us/library/ms647732.aspx

Of course, it depends on the certain task what exactly way to choose. For instance, if you need to run a batch file, wait until it terminates and then check the data in the output, it is better to use SYS_Execute() function.

If you need to run only executable files and want to have compatibility between different SilkTest versions, method Start() is the most suitable.

And if you want to open files of different formats, or to run executable file and continue executing your script without waiting the executable file to finish working, it would be better to use ShellExecute() function.

You must be logged in in order to post comments