Thursday 20 February 2014

How to find the column number for given column name in webtable in QTP?

Below QTP example shows how we can find out the column position/number for the given column name in the webtable using QTP.

Suppose you know the column name as "Quantity", then you can get it's position using below line of code.
Below function will return the position of the given column in the table. If the column is not found in the table, -1 is returned.

print GetColumnNumber(ObjParent.Webtable("mytable"), "Quantity")


Function GetColumnNumber(byval table, byval columnName)


Result = false

intColumnCount = table.ColumnCount(1)
print "row count " & intColumnCount

For j = 1 to intColumnCount
curColumn =table.GetCellData (1, j)
print curColumn

'It is important to remove the extra spaces using trim function to match the column name properly  
If ucase(trim(columnName)) = ucase(trim(curColumn)) Then
GetColumnNumber=j
Exit Function
        end if
next

GetColumnNumber = -1 

End Function


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to create random strings in QTP?

Sometimes we need to create the test data randomly  in the form of strings (numerical, alphanumerical and alphabetical) at run time. I have created one function that will give you random strings.

'to get only alphabetical strings, pass 1 as input
print getRandomString(1)

'to get only alphanumerical strings , pass 2 as input
print getRandomString(2)

'to get only numerical strings , pass 3 as input
print getRandomString(3)

Function getRandomString(byval intType)

stamp = replace(replace(formatdatetime(now),":","-"),"/","")
stamp = replace(stamp,"-","")
stamp = replace(stamp," ","")

If intType = 1 Then
'only alphbetical
For i=1 to 8
Randomize 
x = x & chr( Int((90-65+1)*Rnd+65))
Next

elseIf intType = 2 Then
' alphanumeric
x = "AN" + stamp

elseIf intType = 3 Then
' only digits
x  = stamp

End If
        getRandomString = x

End Function

All the strings will be unique as they are based on time stamp.
Some of the testing scenarios where you will need such function are given below.

  1. Some times users are allowed to save the profile settings which can be loaded later on. In this case, we need to create such profile names that are unique. 
  2. Some times we need to create the entity names which are unique. For example - creating the group Ids, User Ids etc.


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to close all processes with given name in QTP?

While testing the applications with automation, we need to close existing applications and then start new one to avoid a clash.

Below example demonstrates how we can close all processes with given name.

Suppose if you want to close all excel processes, then you can use below code.
Call closeProcessByName("excel.exe")

Suppose if you want to close all internet explorer browser processes, then you can use below code.
Call closeProcessByName("iexplore.exe")

In similar way you can close any process given that you know the name of process

Sub closeProcessByName(byval process_name)

sComp = "."

'Get the WMI object
Set WMI = GetObject("winmgmts:\\" &  sComp &  "\root\cimv2")

'Get collection of processes with process_name like excel.exe, iexplore.exe

 Set allp = WMI.ExecQuery("Select * from Win32_Process Where Name = '" & process_name & "'")

'terminate each process
For Each p  in allp
p.Terminate()
Next 

End Sub


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to find the total number of rows in the webtable in QTP?

We can find the count of rows in the table in QTP using 3 ways as mentioned below.

  1. using rowcount property of QTP webtable object
  2. using GetROProperty of QTP
  3. using HTML DOM + QTP

using rowcount property of QTP webtable object
print  objParent.webtable("html id:=myTable").RowCount

using GetROProperty of QTP
print  objParent.webtable("html id:=myTable").GetROProperty("rows")

using html DOM + QTP
print  objParent.webtable("html id:=myTable").Object.rows.length

This is how you can get the total number of rows in the given table in QTP.

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to find all items from the weblist in QTP?

Below example illustrates how We can get the all items from the drop down in QTP.

arrWeblistItems = split(objParent.weblist("name:=listType").GetROProperty("all items"),";")

'print total number of items in the listbox
print arrWeblistItems.count

For itemCounter =0 to ubound(arrWeblistItems)
'print the value in the dropdown
print arrWeblistItems(itemCounter )
Next                                                

This is how we can print the all items in the list box in QTP.

We can also get all items in the combo box using HTML DOM + QTP as shown in below code.

Set col = ObjParent.weblist("name:=tranType").object.getElementsByTagName("option")
For i=0 to col.length-1
              'print the value in each option inside combobox
             print col(i).innerText
Next


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to verify if the edit box is enabled or disabled in QTP?

We can easily check if the given web object like edit box, listbox, weblist, webbutton, webcheckbox is enabled or disabled using 2 ways in QTP 

1. Example - Using getROProperty method of QTP
Below example uses the GetROProperty method to find the checkbox status

status = Browser("ABC").Page("Mypage").WebEditBox("myedit").GetROProperty("Disabled")

If the webeditbox is disabled then status will have a value - 1

If the webeditbox is enabled then status will have a value - 0

This is how we can validate if the editbox is enabled or not using QTP.

2. Example - Using HTML DOM (applies to only web applications)

status = Browser("ABC").Page("Mypage").WebEditBox("myedit").object.disabled

If the editbox is disabled then status will have a value - true

If the editbox is disabled then status will have a value - false

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to verify if the webcheckbox is selected or not in QTP?

Verifying if the web checkbox is selected or not is very easy in QTP.

Example - Using getROProperty method of QTP
Below example uses the GetROProperty method to find the checkbox status

status = Browser("ABC").Page("Mypage").WebCheckBox("mycheck").GetROProperty("Value")

If the checkbox is selected then status will have a value - ON

If the checkbox is not selected then status will have a value - OFF

This is how we can validate if the checkbox is selected or not using QTP.

Example - Using HTML DOM (applies to only web applications)

status = Browser("ABC").Page("Mypage").WebCheckBox("mycheck").object.checked

If the checkbox is selected then status will have a value - true

If the checkbox is not selected then status will have a value - false


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to Select an item from the weblist in QTP?

Selecting an item from the weblist is tricky in QTP. We have a method - select to select a specific item from the weblist.

But the item value is case sensitive. That means if you have 2 items in the list say Buy and Sell , then below code will throw an error saying can not identify the specified item of type - listType.

objParent.weblist("name:=listType").Select "BUY"  'and not Buy

But if you use below code, it will work like a charm.

objParent.weblist("name:=listType").Select "Buy"  ' and not BUY

To make the code more reliable we can use more improved code as shown below


itemToSelect = "BUY"

arrWeblistItems = split(objParent.weblist("name:=listType").GetROProperty("all items"),";")

For itemCounter =0 to ubound(arrWeblistItems)

If trim(ucase(arrWeblistItems(itemCounter))) = ucase((trim(itemToSelect))) Then
objParent.weblist("name:=listType").Select arrWeblistItems(itemCounter)
Exit for
End If

Next                                                

This is how we can select the item from the combo box in QTP regardless of case-sensitivity.

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to schedule QTP script execution at night?

Well - Many a times you need to schedule the test execution with QTP at night.

I am going to explain how you can write a vbscript to run QTP test at specific period of time. You can copy below code and store it in sample vbscript file say qtptask.vbs Please do not forget to change the path of the test to execute at the first line in below code.

Now go to windows scheduler through control panel and create a new task. Give the path of the vbs file you created and set the specific time at which you want to execute the QTP scripts.

That's it. Now you can go home and relax. Your QTP scripts will run automatically at the time you specified in task.

Example to launch QTP automatically

testpath = "C:\sample-scheduled-test"

'Create the QTP Application object
Set qtApp = CreateObject("QuickTest.Application") 
qtApp.Launch 

'Make the qtp application visible
qtApp.Visible = True

'open the test you want to run 
qtApp.Open testpath , True 

'start executing the test
qtApp.Test.Run

'close the test
qtApp.Test.Close

'quit the QTP application
qtApp.quit

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts.

How to find all check boxes on the webpage in QTP?

In web applications, we can find the web checkbox objects in 2 ways.
  1. using QTP childobjects method
  2. using html DOM
using QTP childobjects method

Set oDesc = Description.Create()
oDesc("micclass").Value = "Webcheckbox"

'identify objects inside parent page having micclass as Webcheckbox
set col = ObjParent.childobjects(oDesc)

'print the count of checkboxes on the page.
msgbox col.count


using HTML DOM

set col = Browser("index:=0").page("index:=0").object.getElementsByTagName("input")
msgbox col.length

'Above code will give all objects starting with input tag. You can filter those having type = checkbox using below code.


For i=0 to col.length-1
If  col(i).getAttribute("type")  = "checkbox" Then
cnt = cnt +1
End If
Next


print cnt


'above code print the type attribute value. If it is checkbox , that means given control is web checkbox.

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to find all web list objects on the web page in QTP?

In web applications, we can find the weblist objects in 2 ways.
  1. using QTP childobjects method
  2. using html DOM
using QTP childobjects method

Set oDesc = Description.Create()
oDesc("micclass").Value = "WebList"

'identify objects inside parent page having micclass as Weblist
set col = ObjParent.childobjects(oDesc)

'print the count of web list boxes on the page.
msgbox col.count


using HTML DOM

set col = Browser("index:=0").page("index:=0").object.getElementsByTagName("Select")
msgbox col.length


'Above code will give all objects starting with select tag - list boxes ( Drop downs) . 


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com



Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

Tuesday 18 February 2014

How to find all webedit objects on the webpage in QTP?

In web applications, we can find the webedit objects in 2 ways.

  1. using QTP childobjects method
  2. using html DOM


using QTP childobjects method

Set oDesc = Description.Create()
oDesc("micclass").Value = "WebEdit"

'identify objects inside parent page having micclass as Webedit
set col = ObjParent.childobjects(oDesc)

'print the count of edit boxes on the page.
msgbox col.count


using HTML DOM

set col = Browser("index:=0").page("index:=0").object.getElementsByTagName("input")
msgbox col.length

'Above code will give all objects starting with input tag. You can filter those having type = text using below code.


For i=0 to col.length-1
If  col(i).getAttribute("type") = "text" Then
cnt = cnt +1
End If
Next


print cnt


'above code print the type attribute value. If it is text, that means given control is edit box.


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to select the radio button in the WebRadioGroup in QTP

In web applications, radio buttons are usually part of the webradiogroup.  Each webradiogroup contains specific items. We can select one item at a time.

In QTP, we can select the particular radio button using below syntax.

Browser("abc").page("xyz").WebRadioGroup("sex").Select "male"

In above code, we have a WebRadioGroup - sex (with 2 items - male and female).
Above code will select the male.

To select female radiobutton, we can use below line
Browser("abc").page("xyz").WebRadioGroup("sex").Select "female"


Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to select the web checkbox in QTP?

In QTP, you can select the web checkbox using below syntax.

'Below code will select the checkbox with name - mycheck
Browser("ABC").Page("XYZ").WebCheckBox("mycheck").set "ON"

'Below code will deselect the checkbox with name - mycheck
Browser("ABC").Page("XYZ").WebCheckBox("mycheck").set "OFF"

Please note that using same method, we can select or deselect the checkbox in other applications/environments like JAVA, .Net, Delphi, Oracle, Peoplesoft, Siebel, Visual Basic.

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

How to find the title of all open browsers in QTP?

Below example shows how we can handle multiple open browsers in QTP.


'Create the browser object using description.create
Set browserObject= Description.Create()
browserObject("micclass").Value= "Browser"

'using Desktop.ChildObjects find the collection of all open browsers
Set collection= Desktop.ChildObjects(browserObject)

'print the total number of open browsers in the system
print  "Total number of browsers open in the system are " & collection.Count()

Once we get total number of browsers open in the system. We can print the titles using 2 ways.


 For i =0 To (collection.Count()-1)

print  "Title of the browser no : " & ( i+1) &  " is -> " &  collection(i).GetROProperty("title")

Next

Just the another way to print the title of browsers.

 For i =0 To (collection.Count()-1)

print  "Title of the browser no : " & ( i+1) &  " is -> " &  Browser("index:=" & i).getROProperty("title")

Next

We can also print other details of  each browser like
  1. hasmenubar - returns true if browser has menu bar
  2. hasstatusbar - returns true if browser has statusbar
  3. hastoolbar - returns true if browser has toolbar
  4. hwnd - returns the handle of the browser window
  5. number of tabs - returns the count of tabs
  6. openedbytestingtool - returns true if the browser is opened by QTP
  7. opentitle - returns the open title 
  8. openurl -  returns the url
 Above code was tested on QTP 10 and Internet Explorer 10.

Please give your inputs, suggestions, feedback to Us about above QTP topic. We value your thoughts. You can write to me at reply2sagar@gmail.com

Best QTP Books

Everything About QTP

Hello Friends,
You can find QTP study material, Multiple choice questions (mcq), QTP question bank, QTP question papers, QTP notes, QTP questionnaire, scenario based QTP interview questions, QTP tutorial and QTP training on this site.

If you are a fresher or experienced QTP professional with (1/2/3/4) years of experience, this blog is just for you.