Tuesday, December 6, 2011

How "On Error Resume Next" can be a problem in QTP?

How "On Error Resume Next" can be a problem in QTP?
'"On Error Resume Next" is used to handle errors in vbscript/QTP and usually very helpful. But many times, it might give you unexpected outcomes. Let's see how..
Example 1:
Function Example(var1 , var2)
On Error Resume Next
Example = var1/var2
On Error GOTO 0
End Function
msgbox Example(2, 0)
Run this code and see the result.


Result - Blank value is returned because of 'On Error Resume Next', which is incorrect! If 'On Error Resume Next' is not there, it must return an error code - 11, as division by zero.
Consider the situation where you are passing the above output as an input is some other function! What'll happen? Whole your test will do something unexpected and you..... :)
You'll just keep looking in the code for What went wrong!!



Example 2:
Suppose there is a bug in application, i.e. Login button is not displayed.
On Error Resume Next
Browser("mybrowser").Page("mypage").WebEdit("loginname").Set "abhikansh"
Browser("mybrowser").Page("mypage").WebEdit("password").SetSecure "mypass"
Browser("mybrowser").Page("mypage").WebButton("login").click
On Error GOTO 0
Result : Even though the Login button is not displayed on the page, there will be no runtime error and because steps inside the 'On error resume next' are not optional, it leads to incorrect result.
So what's the lesson? Shall I avoid to use 'On error resume next'?
No. Not at all! But be very careful while using.
Remember.. Its useful when you intentionally don't want to let errors occur i.e. any temporary browser specific error or printer error which is not about AUT (application under test).

Monday, October 10, 2011

Search for Particular value in Excel

Set myxl = createobject("excel.application")
myxl.Application.Visible = true
myxl.Workbooks.Open "C:\Documents and Settings\pavann\Desktop\qtp15"
'This is the name of Sheet in Excel file "qtp15.xls" where data needs to be entered
set mysheet = myxl.ActiveWorkbook.Worksheets("Sheet1")
'Select the used range in particular sheet
With mysheet.UsedRange
' Data "PAVAN" to search
' Loop through the used range
For each search_data in mysheet.UsedRange
' compare with the expected data
If search_data="PAVAN" then
'make the cell with color if it finds the data search_data.Interior.ColorIndex = 10
End If
next
End With
myxl.ActiveWorkbook.Save
myxl.ActiveWorkbook.Close
myxl.Application.Quit
Set mysheet =nothing
Set myxl = nothing

-- Pavankumar Nandagiri

Read the data from Excel File

Set xlapp3 = createobject("excel.application")
xlapp3.Visible = true
Set xlbook3 = xlapp3.Workbooks.Open("C:\Documents and Settings\pavann\Desktop\qtp15")Set xlsheet3= xlbook3.Worksheets("sheet1")
xlrwcnt = xlsheet3.usedrange.rows.count
'msgbox xlrwcnt
xlcolcnt = xlsheet3.usedrange.columns.count
'msgbox xlcolcnt
For m=1 to xlrwcnt
For n=1 to xlcolcnt
val = xlsheet3.cells(m,n).value
msgbox val
Next
Next

-- Pavankumar Nandagiri

Wednesday, October 5, 2011

Creating a new Excel sheet, Opening the exsisting sheet & entering data and saving it

'Creating a new Excel sheet
Set xlapp = createobject("excel.application")
xlapp.Visible = true
xlapp.Workbooks.Add
xlapp.ActiveWorkbook.SaveAs("C:\Documents and Settings\pavann\Desktop\qtp12")xlapp.Application.Quit
Set xlapp=nothing

'Opening the exsisting sheet & entering data and saving it
Set xlapp1 = createobject("Excel.Application")
xlapp1.Visible = true
Set xlbook1 = xlapp1.Workbooks.Open("C:\Documents and Settings\pavann\Desktop\qtp12")Set xlsheet1 = xlbook1.Worksheets("sheet1")
For i = 1 to 10
xlsheet1.cells(i,1).value = "pavan"
For k = 1 to 10
If k>1 Then
xlsheet1.cells(i,k).value = "soumya"
End If
Next
Next
set objrange = xlsheet1.usedrange
For each cell in objrange
cell.value = ucase(cell.value)
Next
xlbook1.Save
xlapp1.Application.Quit
Set xlapp1=nothing

-- Pavankumar Nandagiri

Tuesday, October 4, 2011

Adding values into excel sheet and changing the cell values to Uppercase

Set xlapp = createobject("Excel.Application")
xlapp.Visible = true
set xlbook = xlapp.Workbooks.Open("C:\Documents and Settings\pavann\Desktop\test")
set xlsheet = xlbook.Worksheets("sheet1")
For i = 1 to 10
For j= 1 to 10
xlsheet.cells(i,1) = "pavan"
If j>1 Then
xlsheet.cells(i,j) = "soumya"
End If
Next
Next
set objrange = xlsheet.usedrange
For each objcell in objrange
objcell.value = Ucase(objcell.value)
Next

-- Pavankumar Nandagiri

Google Search

Google