Quantcast
Channel: Test Automation
Viewing all articles
Browse latest Browse all 19

QTP Run As Example

$
0
0
The following code demonstrates how to use a Run As command to open applications within QTP.

An example of calling the function would be:

Call RunProcessAs("dave@domain","password","C:\Program Files (x86)\Internet Explorer\iexplore.exe")

The key thing to note is that the user and domain are passed in as one parameter (you could modify this so that they are not).

Here is the function:

'Function Name:  RunProcessAs
'Function Purpose: Runs a process using the "RunAs" command.
'Input Parameters: strUserAtDomain - the user and domain to use. e.g. Test UserName@DOMAIN
' strPassword - Password for the username. e.g. Password
' strApplicationLaunchParams - Launch details of the program as if it were being launched in a cmd window.
' e.g. C:\Program Files (x86)\Internet Explorer\iexplore.exe www.google.com
'Creation Date: July 7th 2012 by David Hartley
'Modification Date:

Function RunProcessAs(strUserAtDomain,strPassword,strApplicationLaunchParams)
Dim strThisFunctionName : strThisFunctionName = "[Function RunProcessAs] "
Dim strRunAs
Dim objFSO
Dim strBatFilePath
Dim objBatFile
Dim objDesc,objDescCol
Dim n
Dim strBatWindow


'An example of the desired launch string would be:
' runas /user:"test UserName@DOMAIN""C:\Program Files (x86)\Internet Explorer\iexplore.exe www.google.com"

'Construct the RunAs launch string:

strRunAs = "runas /user:"& chr(34) & strUserAtDomain & chr(34) & ""& chr(34) & strApplicationLaunchParams & chr(34)

'Now create a bat file to launch this from - the bat file will allow us to create our own cmd window for this process.
Set objFSO = CreateObject("Scripting.FileSystemObject")
strBatFilePath = objFSO.GetSpecialFolder(2) & "\QTPRunAs.Bat"

'delete the file if it exists
If objFSO.fileexists(strBatFilePath) Then objFSO.DeleteFile(strBatFilePath)

'now create the bat file
Set objBatFile = objFSO.OpenTextFile(strBatFilePath, 8, True)
Call objBatFile.WriteLine ("title QTP RunAs")
Call objBatFile.WriteLine (strRunAs)
Call objBatFile.WriteLine ("timeout 10")
objBatFile.Close

'before we run the bat file, just check there are no already opened instances
Set objDesc = Description.Create
objDesc("micclass").value = "Window"
objDesc("regexpwndtitle").value = "QTP RunAs"
Set objDescCol = Desktop.ChildObjects(objDesc)
If objDescCol.count >0 then
For n = 0 to (objDescCol.count -1)
objDescCol(n).close
Next
end if

'Now run the .bat file
systemutil.Run strBatFilePath
wait(1)
strBatWindow = "regexpwndtitle:=QTP RunAs"
Window(strBatWindow).Activate
Window(strBatWindow).Type strPassword
Window(strBatWindow).Type micReturn
wait(2)

End Function

Viewing all articles
Browse latest Browse all 19

Trending Articles