In the below article we will discuss the following topics related to the VBScripting in QTP.
- While .. Wend Looping In QTP.
- For ...Loop Handling In QTP VBScript.
- If ...End If Handling in VBScript in QTP.
- Usage of For...Each Loop in QTP Vbscripting.
- ChildObjects in QTP.
- LoadAndRunAction method.
ReDimUsage in Vbscripting QTP.
SystemUtil Usage in VbScripting QTP.
- ExecQuery
- IE.Terminate()
- Associated library :
- Action Code :
- The LoadAndRunAction method allows calling any Action for Local system or Quality center at run-time.
Regular Expression in VBScript in QTP :
Dim text
text = ""
Dim rgx
Set rgx = New RegExp
rgx.IgnoreCase = True
rgx.Global = True
rgx.Pattern = "([A-Za-z]{3,9})://([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((/[-\+~%/\.\w]+)?\??([-\+=&;%@\.\w]+)?#?([\w]+)?)?"
Dim match, matches
Set matches = rgx.Execute(text)
For Each match in matches
MsgBox match.Value, 0, "Found Match"
Next
text = "
Dim rgx
Set rgx = New RegExp
rgx.IgnoreCase = True
rgx.Global = True
rgx.Pattern = "([A-Za-z]{3,9})://([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((/[-\+~%/\.\w]+)?\??([-\+=&;%@\.\w]+)?#?([\w]+)?)?"
Dim match, matches
Set matches = rgx.Execute(text)
For Each match in matches
MsgBox match.Value, 0, "Found Match"
Next
'Launch a notepad window :
SystemUtil.Run "notepad.exe"
'Close the window just launched using the exact title :
SystemUtil.CloseProcessByWndTitle "Untitled - Notepad"
'Launch a notepad window
SystemUtil.Run "notepad.exe"
'Close the window just launched using a pattern string
SystemUtil.CloseProcessByWndTitle ".*Notepad", True
SystemUtil.CloseDescendentProcesses
CloseDescendentProcesses can be used to close any process launched by QTP. The code below illustrates the usage
'Launch explorer
SystemUtil.Run "iexplore.exe"
'Launch excel using COM
Set oXL = CreateObject("Excel.Application")
oXL.Visible = True
'Close processes launched by QTP. This will close
'the internet explorer and Excel as well
SystemUtil.CloseDescendentProcesses
While Browser("creationtime:=0").Exist(0)
'Close the browser
Browser("creationtime:=0").Close
Wend
'Create a description for browser
Set oBrowser = Description.Create
oBrowser("micclass").Value = "Browser"
Set oPage = Description.Create
oPage("micclass").Value = "Page"
'Get all browsers
Set allBrowser = Desktop.ChildObjects(oBrowser)
Dim i, iCount
iCount = allBrowser.Count - 1
For i = 0 To iCount
'Get the page object from the browser
Set oPg = allBrowser(i).ChildObjects(oPage)(0)
'Get the URL of the
If InStr(oPg.GetROProperty("title"), "Quality Center", vbTextCompare) = 0 Then
'Close the browser
allBrowser(i).Close
End If
Next
By now you must be wondering about the line
'Get the page object from the browser
Set oPg = allBrowser(i).ChildObjects(oPage)(0)
'Name/IP of the computer
sComp = "."
'Get the WMI object
Set WMI = GetObject("winmgmts:\\" & sComp & "\root\cimv2")
'Get collection of processes for with name iexplore.exe
Set allIE = WMI.ExecQuery("Select * from Win32_Process Where Name = 'iexplore.exe'")
'Loop through each process and terminate it
For Each IE in allIE
IE.Terminate()
Next
'Create the shell application object
Set shlApp = CreateObject("Shell.Application")
'Get all open windows collection
Set allWins = shlApp.Windows
'Loop through each window and close if IE
For Each window In allWins
If InStr(window.fullName, "iexplore.exe") Then
'Close the IE window
window.Quit
End If
Next
Dim windows2Close
'Initialize with negative upper bound
ReDim windows2Close( -1)
'Create the shell application object
Set shlApp = CreateObject("Shell.Application")
'Get all open windows collection
Set allWins = shlApp.Windows
'Loop through each window and close if IE
For Each window In allWins
'Close all IE windows but ignore Quality Center
If InStr(window.fullName, "iexplore.exe") And InStr(Window.LocationURL, "/qcbin/") = 0 Then
'Increase the array size by 1
ReDim Preserve windows2Close(UBound(windows2Close) + 1)
Set windows2Close(UBound(windows2Close)) = Window
End If
Next
'Loop through all array elements and close each window
For Each Window In windows2Close
Window.Quit
Next
WebUtil.DeleteCookies()
Function DeleteIECookies()
Set oWebUtil = CreateObject("Mercury.GUI_WebUtil")
oWebUtil.DeleteCookies()
End Function
Function DeleteFFCookies()
Dim FSO, cookiePath
Set FSO = CreateObject("Scripting.FileSystemObject")
cookiePath = GetFFCookiesPath()
If FSO.FileExists(cookiePath) Then FSO.DeleteFile cookiePath
End Function
Function GetFFCookiesPath()
Dim FSO, oShell
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
If InStr(Environment("OS"),"Vista") Then
Dim sUserProf
sUserProf = oShell.ExpandEnvironmentStrings("%USERPROFILE%") & "\AppData\Roaming\Mozilla\Firefox\Profiles"
GetFFCookiesPath = ""
If FSO.FolderExists(sUserProf) Then
Set oFolder = FSO.GetFolder(sUserProf)
If oFolder.SubFolders.Count> 0 Then
'oFolder.item(0) throws an error, so we use a workaround to find the first folder
For each oFolder in oFolder.SubFolders
Exit For
Next
'Set oFolder = oFolder.item(0)
Dim sPath
sPath = oFolder.Path
If FSO.FileExists(sPath & "\Cookies.txt") Then
GetFFCookiesPath = sPath & "\Cookies.txt"
End If
End If
End If
Else
Err.raise vbObjectError + 1, "GetFFCookiesPath", "This function has not yet been implemented for your OS - " & Environment("OS")
End if
End Function
Function Login(username, password)
oUserTxt.Set username
oUserPwd.Set Ucase(password)
oLogin.Click
End Function
ExecuteFile PathFinder.Locate("LoginUpdate.vbs")
Associated library :
Function NewMsgBox(Message)
Print "The user says – " & Message
End Function
Dim fnPtrNewMsgBox
Set fnPtrNewMsgBox = GetRef("NewMsgBox")
|
Action Code :
Dim MsgBox
Set MsgBox = fnPtrNewMsgBox
Msgbox "Tarun"
|
The advantage of this approach is that we can use the original MsgBox function as well in the NewMsgBox method which was not possible with the earlier approach
orkaround to that I decided to use a new ActualParams objects. So the Library was updated as
Dim ActualParams
'By default we will use the component parameters
Set ActualParams = Parameter
Function App_Login()
Browser("X").Page("X").WebEdit("UserName").Set ActualParams("UserName")
Browser("X").Page("X").WebEdit("Password").Set ActualParams("Password")
Browser("X").Page("X").WebButton("Login").Click
End Function
|
Now I can use the function as below in my component
'Call with component parameter
Call App_Login
Dim DummyParams
Set DummyParams = CreateObject("Scripting.Dictionary")
DummyParams("UserName") = "Tarun"
DummyParams("Password") = "Tarun" ' Do remember that password field should be first encrypted
'Set the Actual parameters be used in Function as DummyParams
Set ActualParams = DummyParams
Call App_Login
'Rest of the functions to use the component parameters
Set ActualParams = Parameter
|
The LoadAndRunAction method allows calling any Action for Local system or Quality center at run-time.
LoadAndRunAction(TestPath, ActionName, [Iteration], [Parameters])
'Get the Test type
TestType = DataTable.Value("TestType",dtGlobalSheet)
'Load and call the Action
LoadAndRunAction "C:\Framework\Tests\Actions", TestType
|
إرسال تعليق