Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

How can I interact with the console (cmd) window in 4Test?

How can I interact with the console (cmd) window in 4Test?

Console.GetContents() returns a List Of String. Normally you would used ListFind to identify an item in a List Of String but the data returned by some DOS commands is columnular in format e.g. dir /w. This presents a problem in that the column data will vary by console size and item numbers. In this type of case it is necessary to separately parse each line of data listed in the GetContents() call. The following 4Test testcase returns the console window to a known state (the root of drive C:), then calls a directory listing. The code checks to see if the  Program Files folder is available and if it is, a directory listing is then called for Program Files.

The cmdRead function requires just the string that you want to find.

[ ] // ------------------------------------
[ ] // Compare the data in the console window against the required data.
[ ] // We can't use ListFind because the data may be columnular in format
[ ] // (e.g. a directory listing)
[ ] // ------------------------------------
[-] boolean cmdRead(string sCmdFind)
    [ ] string sString
    [ ] boolean bFound = FALSE  
    [-] for each sString in Console.GetContents()
        [-] if (MatchStr("*{sCmdFind}*",sString))
            [ ] bFound = TRUE
            [ ] break
    [ ] return bFound
[ ]
[ ]
[ ] // ------------------------------------
[ ] // Find the Program Files folder and do a directory listing
[ ] // ------------------------------------
[-] testcase useCMDWindow() appstate none
    [ ] list of string lsConsoleContents
    [ ] boolean bPass=false
    [ ] string crlf=chr(13)+chr(10)
    [ ]
    [ ] // Set the console to a known state i.e. the root of drive C:
    [ ] Console.TypeKeys("cd\"+crlf)
    [ ] Console.TypeKeys("cls"+crlf)
    [ ] // Run a directory listing
    [ ] Console.TypeKeys("dir/w"+crlf)
    [ ] // Look for the Program Files folder
    [ ] bPass=cmdRead("[Program Files]")
    [ ] // If it exists change to it and list the contents
    [-] if bPass==true
        [ ] Console.TypeKeys("cd program files"+crlf)
        [ ] Console.TypeKeys("dir/w"+crlf)
    [-] else
        [ ] print("Program Files folder not found")

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-10-29 17:09
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.