About Me

Gaurav Seth is a a Senior Automation Test Analyst with experience in QTP, Selenium and Ranorex tools and currently working on Automating in C# for a FX trading Client

VB Scripting with QTP Part 2

Below Snippet of code will let you know if a particular Windows Process is running in the Background or not. From windows Task manager just put the Process name in the If Condition in Upper Case

Dim AllProcess
Dim Process
Dim strFoundProcess
strFoundProcess = False
Set AllProcess = getobject("winmgmts:") 'create object
For Each Process In AllProcess.InstancesOf("Win32_process") 'Get all the processes running in your PC
If (Instr (Ucase(Process.Name),"EXCEL.EXE") = 1) Then 'Made all uppercase to remove ambiguity. Replace TASKMGR.EXE with your application name in CAPS.
msgbox "Application is already running!" 'You can replace this with Reporter.ReportEvent
strFoundProcess = True
Exit for
End If
Next
If strFoundProcess = False Then
msgbox "Go ahead!Application is not running" 'You can replace this with Reporter.ReportEvent
End If
Set AllProcess = nothing

Using Description.Create() Object to Activate a Window. This will activate a VB Window with title having SIREN title. After that Counting the No of buttons on this window using Description.Create()

Set FrmActivate=Description.Create()
FrmActivate("micclass").Value="vBWindow"
FrmActivate("Text").Value="SIREN"
FrmActivate.Activate

Set btn=Description.Create()
btn(“micclass”).Value=”VbButton”
btn(“Text”).Value=”OK”

Set cnt=VBWindow(FrmActivate).ChildObjects(btn)
Msgbox cnt.Count

or Msgbox vbWindow(FrmActivate).ChildObjects(bnt).Count

To See the name of all the buttons present in the VB WIndow use the Following code

Set FrmActivate=Description.Create()
FrmActivate("micclass").Value="VbWindow"
FrmActivate("Text").Value="SIREN"

Set btn=Description.Create()
btn("micclass").Value="VbButton"

Set cnt=VbWindow(FrmActivate).ChildObjects(btn). Count
Set val=VBWindow(FrmActivate).ChildObjects(btn)
For i=0 to cnt-1
value=val(i).GetRoProperty(“Text”)
msgbox value
Next


How to open a URL in the Maximize State
url="www.gmail.com"
Set IEInstance=CreateObject("InternetExplorer.Application")
IEInstance.Visible=True
IEInstance.Navigate(url)
Window("hwnd:=" &IEInstance.HWND).Maximize

Below piece of snippet code will demonstrate how to count no and item name of drop down items from the WebList

SystemUtil.Run "http://newtours.demoaut.com/"
Browser("title:= Welcome: Mercury Tours" ).Sync
Browser("title:= Welcome: Mercury Tours" ).Page("title:= Welcome: Mercury Tours").WebEdit("index:=0").Set "mercury"
Browser("title:= Welcome: Mercury Tours" ).Page("title:= Welcome: Mercury Tours").WebEdit("index:=1").Set "mercury"
Browser("title:= Welcome: Mercury Tours" ).Page("title:= Welcome: Mercury Tours").Image("file name:=btn_signin.gif").Click
Browser("title:=Find a Flight: Mercury Tours:").Sync
'Browser("title:= Find a Flight: Mercury Tours:" ).Page("title:= Find a Flight: Mercury Tours:").WinButton("x:=455,y:=463, text:=View Calendar, index:=1, hwnd:=8196244").Click
Set webl=Description.Create()
webl("micclass").Value="WebList"
webl("name").Value="fromPort"
Dim wl
Set wlc=Browser("title:= Find a Flight: Mercury Tours:" ).Page("title:= Find a Flight: Mercury Tours:").ChildObjects(webl)
wl= Browser("title:= Find a Flight: Mercury Tours:" ).Page("title:= Find a Flight: Mercury Tours:").WebList(webl).GetROProperty("items count")
witems=Browser("title:= Find a Flight: Mercury Tours:" ).Page("title:= Find a Flight: Mercury Tours:").WebList(webl).GetROProperty("all items")
msgbox witems
t=Split(witems, ";")
For i=0 to ubound(t)
msgbox t(i)
Next
Browser("title:= Find a Flight: Mercury Tours:" ).Page("title:= Find a Flight: Mercury Tours:").Image("file name:=continue.gif").Click


Following Snippet of Code will tell you how you
1. How to Use Index property to uniquely identify the Items such as WebList(drop down items), WebCheckBox(CheckBox), WebEdit
2. How to take a screenshot of the Application

SystemUtil.Run "http://newtours.demoaut.com/"
Browser("title:= Welcome: Mercury Tours" ).Sync
Browser("title:= Welcome: Mercury Tours" ).Page("title:= Welcome: Mercury Tours").WebEdit("index:=0").Set "mercury"
Browser("title:= Welcome: Mercury Tours" ).Page("title:= Welcome: Mercury Tours").WebEdit("index:=1").Set "mercury"
Browser("title:= Welcome: Mercury Tours" ).Page("title:= Welcome: Mercury Tours").Image("file name:=btn_signin.gif").Click
Browser("title:=Find a Flight: Mercury Tours:").Sync
Browser("title:= Find a Flight: Mercury Tours:" ).Page("title:= Find a Flight: Mercury Tours:").Image("file name:=continue.gif").Click
Browser("title:= Select a Flight: Mercury Tours" ).Page("title:= Select a Flight: Mercury Tours").Image("file name:=continue.gif").Click
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebList("index:=0").Select "Bland"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebCheckBox("index:=0").Set "OFF"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebCheckBox("index:=1").Set "ON"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=0").Set "Gaurav"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=1").Set "Seth"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=2").Set "987698689"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=3").Set "Gaurav"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=4").Set "Kumar"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=5").Set "Seth"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=6").Set "16 Dashmesh Avenue"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=8").Set "Amritsar"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=9").Set "Punjab"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=10").Set "143104"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=11").Set "Sheetla Mata Road"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=13").Set "Gurgaon"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=14").Set "Haryana"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").WebEdit("index:=15").Set "145003"
Desktop.CaptureBitmap "C:\QTP\Screenshots\Bookaflight.PNG"
Browser("title:= Book a Flight: Mercury Tours" ).Page("title:= Book a Flight: Mercury Tours").Image("file name:=purchase.gif").Click


Print Statement

Displays information in the QuickTest Print Log window during the run session. The QuickTest Print Log window remains open while the run session continues, until you close it.

Set webl=Description.Create()
webl("micclass").Value="WebList"
webl("name").Value="fromPort"
Dim wl
Set wlc=Browser("title:= Find a Flight: Mercury Tours:" ).Page("title:= Find a Flight: Mercury Tours:").ChildObjects(webl)
wl= Browser("title:= Find a Flight: Mercury Tours:" ).Page("title:= Find a Flight: Mercury Tours:").WebList(webl).GetROProperty("items count")
Print "The total no of items are "&wl
witems=Browser("title:= Find a Flight: Mercury Tours:" ).Page("title:= Find a Flight: Mercury Tours:").WebList(webl).GetROProperty("all items")
Print " The Items are "&"\n"& witems
t=Split(witems, ";")
For i=0 to ubound(t)
Print "The drop down item is " &t(i)



To count no of Tables in a web Page, get the cell data of the Tables and cofirming the existence of particular text in them

Set mypage=Browser("title:= Flight Confirmation: Mercury Tours" ).Page("title:= Flight Confirmation: Mercury Tours")

Set table_desc=Description.Create()
table_desc("html tag").Value="TABLE"

Set all_tables=mypage.ChildObjects(table_desc)
table_count=cint(all_tables.count)

msgbox table_count
Print "We have" &table_count &"tables opened in the current Browser"

For icount=0 to table_count-1
Print "Table No" &icount+1
For irowcount=1 to all_tables(icount).RowCount
For icolumnCount=1 to all_tables(icount).ColumnCount(irowcount)
cell_value=all_tables(icount).getcelldata(irowcount,icolumnCount)
Print "Cell Value is " & cell_value

If instr(1,cell_value,"Flight Confirmation" )Then
Print "Sucess!!!!!!!!!!!!!!!!!!!!!!!"
End If

Next
irowcount=irowcount+1
Next
Next

DataTable Importsheet and Exportsheet Methods

DataTable.ImportSheet "C:\QTP\SystmOne_Config_Files\1.xls",1,1

DataTable.ExportSheet "C:\QTP\SystmOne_Config_Files\3.xls",1

DataTable Count Rows and Columns

DataTable.ImportSheet "C:\QTP\SystmOne_Config_Files\2.xls" ,1 ,1

a=DataTable.GetSheet(1).GetRowCount()
b=DataTable.GetSheet(1).GetParametercount()


Regular Expressions used to find a pattern of Text. For Example time in the format
aa:bb:cc in a particular page



Dim ObjRegExp, IEinstance
Set IEinstance =CreateObject("InternetExplorer.Application")
IEinstance.Visible=True
Wait(2)
IEinstance.Navigate "http://www.timeanddate.com/worldclock/"
Wait(2)

lvVal = IEinstance.Document.body.innertext

Dim Searchkeys(4)
Searchkeys(0)="[0-9]{2}\:[0-9]{2}\:[0-9]{2}"
cnt=0


If SearchKeys(0) <> "" Then

Set ObjRegExp = new regexp
ObjRegExp.pattern = SearchKeys(0)
ObjRegExp.Global = True
ObjRegExp.Ignorecase = True
Set matches = ObjRegExp.Execute(lvVal)
For each objmatch in matches
cnt = cnt+1
tmpVar = objmatch.value
Print objmatch.value

Next
Else
End If

Search a string pattern from web page.
Use the Above code just modify the below

If you use Searchkeys(0) as "[a-z]{2} [a-z]{9} [a-z]{12}" and use IEinstance.Navigate "http://hpqtpautomation.blogspot.com/"
then this will extract the String " HP QuickTest Professional" from this web page.

3 comments:

  1. Hi Gaurav,

    Could you please let us know how to kill an Excel Process that is running in the Background.

    Thanks
    RK

    ReplyDelete
  2. Hi RK,

    Use Process.Terminate()after the Message box to terminate that process that is found matching. Below is the Snippet


    Dim AllProcess
    Dim Process
    Dim strFoundProcess
    strFoundProcess = False
    Set AllProcess = getobject("winmgmts:") 'create object
    For Each Process In AllProcess.InstancesOf("Win32_process")

    If (Instr (Ucase(Process.Name),"EXCEL.EXE") = 1) Then 'Made all uppercase to remove ambiguity.

    msgbox "Application is already running!"
    Process.Terimate

    strFoundProcess = True
    Exit for
    End If
    Next

    If strFoundProcess = False Then
    msgbox "Go ahead!Application is not running" 'You can replace this with Reporter.ReportEvent
    End If

    Set AllProcess = nothing

    ReplyDelete
  3. Below Piece of code ( Function ) will kill any Process you want ( Word, Excel, etc. Just Pass "winword.exe", or "excel.exe" to the Kill

    Function Kill(a)

    'Option Explicit
    Dim strComputer, strProcessToKill, objWMIService, colProcess, objProcess,Processn

    strComputer = "."

    strProcessToKill = a
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer _
    & "\root\cimv2")
    Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strProcessToKill & "'")
    For Each objProcess in colProcess
    objProcess.Terminate()
    Next
    End Function


    Kill ("winword.exe")

    ReplyDelete