Sunday, March 10, 2013

Size of multi-dimensional array

How to find size of a multi-dimensional array. Use following code to find size of a multi-dimensional array.


'Define 3 dimensional array, with size of the first dimension as 4, with size of the second dimension as 6 and with size of the third dimension as 8

Dim myArray(4,6,8)

'size of the first dimension
MsgBox lbound(myArray,1)        'Returns 0
MsgBox ubound(myArray,1)        'Returns 4

'size of the second dimension
MsgBox lbound(myArray,2)        'Returns 0
MsgBox ubound(myArray,2)        'Returns 6

'size of the third dimension
MsgBox lbound(myArray,3)        'Returns 0
MsgBox ubound(myArray,3)        'Returns 8

Thursday, February 23, 2012

Inserting picture in excel sheet using VBScript/ QTP

Following code can be used to insert pictures in to the excel file. All you need to give is the path of the excel file in which the picture to be inserted & the path of the picture file to be inserted.





Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objworkbook  = objExcel.WorkBooks.Open("C:\exceltest.xls")
Set objworksheet = objworkbook.Worksheets(1)
objExcel.ActiveCell.Worksheet.Pictures.Insert("C:\my_picture.JPG")
objworkbook.Save
objExcel.Quit
Set objExcel = Nothing
Set objworkbook  = Nothing
Set objworksheet = Nothing
Set objusedrange = Nothing

Wednesday, February 22, 2012

Sorting an array using VBScript/ QTP

Following code can be used to sort an array using QTP. Let sortme is name of the array which is to be sorted. I have used "Bubble Sort" principle of sorting.





A) Descending Sort
For a = UBound(sortme) - 1 To 0 Step -1
  For j= 0 to a
If sortme(j+1) > sortme(j) Then
temp = sortme(j)
sortme(j) = sortme(j+1)
sortme(j+1) = temp
End If
Next
Next




B) Ascending Sort
For a = UBound(sortme) - 1 To 0 Step -1
  For j= 0 to a
If sortme(j+1) < sortme(j) Then
temp = sortme(j)
sortme(j) = sortme(j+1)
sortme(j+1) = temp
End If
Next
Next

Wednesday, February 15, 2012

Running Database Procedure using qtp

Following code can be used to execute the database procedure. Before proceeding you must know the connection-string to connect the database & the name of the procedure. If you dont know the connection-string of your database refer http://www.connectionstrings.com/ . This website provides an easy reference to connection-strings for various databases.








Set conn = CreateObject("ADODB.Connection")
Set adocommand = CreateObject("ADODB.Command")

conn.ConnectionString = "Connection-string of the running database"
conn.Open


Set adocommand.ActiveConnection = conn
adocommand.CommandText = "Name of the Stored Procedure"
adocommand.CommandTimeout = 30
adocommand.CommandType = 4

adocommand.Execute

conn.Close


Set adocommand = Nothing
Set conn = Nothing

Thursday, February 9, 2012

Clicking at any point(x & y corodinates) on screen using qtp

Sometimes QTP do not identifies complex objects due to various reasons, in such case if you want to click on the unidentifiable object you can use following method. However you want to know the (x,y) position of that object in the browser/application.


Set obj=CreateObject("Mercury.DeviceReplay")
obj.MouseClick x,y,0

You can get the x & y coordinates of that object using the following code or you can use the approximation. This is also useful when QTP do not identify the methods associated/supported by any particular object.
e.g. There might be a webelement object which needs a click to activate, but QTP might not identify or support click method for that particular object then use following code to get the x & y coordinates of that object. Also note that the coordinates must be converted to integer subtype before passing into MouseClick method.


x=Browser("micclass:=Browser").Page("micclass:=Page").WebElement("innertext:=property").GetROProperty("abs_x") 
y=Browser("micclass:=Browser").Page("micclass:=Page").WebElement("innertext:=property").GetROProperty("abs_y") 
x= cint(x)
y= cint(y)



Set obj=CreateObject("Mercury.DeviceReplay")
obj.MouseClick x,y,0



Wednesday, February 8, 2012

Object Repository & Descriptive Programming together

Sometimes we need to use both QTP's own Object Repository and Descriptive Programming while defining complex objects. But this scenario do not works always.

The following care must be taken while designing scripts when you are using both Object Repository & descriptive Programming.

Scenario 1 : Parent object as Object Repository with child object as Descriptive Programming.
Browser("My Website").Page("micclass:=Page").WebEdit("micclass:=My Edit').Set "QuickTest"
'This code works properly

Scenario 2 : Parent object as  Descriptive Programming with child object as  Object Repository.

Browser("micclass:=Browser").Page("My Page").WebEdit("My Edit').Set "QuickTest"
'This code will generate error

Hence whenever you are using Object Repository & Descriptive Programming together take care that parent object should be from Object Repository & not from Descriptive Programming.


Maximize the Browser

If you are using QTP 9.2 or 9.5 or 11.0 version with Internet Explorer then following code should help to maximize/minimize or restore the browser window.

hwnd = object.Object.HWND
Set oWin = Window("hwnd:=" & hwnd)
'To maximize the application
oWin.Maximize

'To restore the application
oWin.Restore

'To minimize the application
oWin.Minimize