Error handling in UFT - hp-uft

Please suggest me how to handle the scenario. Problem is supposed I got an error
at statement 2 during execution of the first iteration for Row 1 from excel then how to skip remaining statement and start the execution of statement 1 with excel row 2.
Browser("ABC").Page("ABC").WebEdit("ABC").Set "123"
Dim i
Dim iRow
iRow = datatable.GetRowCount
For i = 1 to iRow
Statement 1- Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT", dtGlobalSheet)
Statement 2- Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT", dtGlobalSheet)
Statement 3- Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT", dtGlobalSheet)
Statement 4- Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT", dtGlobalSheet)
Statement 5- Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT", dtGlobalSheet)
datatable.SetNextRow
Next
Excel sheet
Row 1 Row 2 Row 3

step1) create a new Action with Name "Action2" and put the code from inside for loop
step2) import data to Action2 local data table
step3) create a recovery scenario enable check on every line with recovery function as
function RecoveryFunction()
ExitActionIteration
End function
step4) from Action1 call Action2 with allIterations

Firstly, if your test is set to Run on all rows in File->Settings->Run then you do not require the loop you have in the code - UFT knows it needs to iterate for each row in your data table.
Secondly, all you need is the statement followed by a check on its success and any error handling. IN the event of the error, just exit the test iteration, and UFT will move onto the next one.
My reworked version of your code:
Browser("ABC").Page("ABC").WebEdit("ABC").Set "123" ' sets an initial value
Browser("ABC").Page("").WebEdit("ABC").Set DataTable("DT1", dtGlobalSheet)
If Browser("ABC").Page("").WebEdit("ABC").GetROProperty("value") <> DataTable("DT1", dtGlobalSheet) Then
' Handle error popup if it exists
Browser("ABCError").Page("").WebButton("OK").Click
ExitTestIteration
End If
Browser("ABC").Page("").WebEdit("CDE").Set DataTable("DT2", dtGlobalSheet)
If Browser("ABC").Page("").WebEdit("CDE").GetROProperty("value") <> DataTable("DT2", dtGlobalSheet) Then
' Handle error popup if it exists
Browser("ABCError").Page("").WebButton("OK").Click
ExitTestIteration
End If
Browser("ABC").Page("").WebEdit("FGH").Set DataTable("DT3", dtGlobalSheet)
If Browser("ABC").Page("").WebEdit("FGH").GetROProperty("value") <> DataTable("DT3", dtGlobalSheet) Then
' Handle error popup if it exists
Browser("ABCError").Page("").WebButton("OK").Click
ExitTestIteration
End If
Browser("ABC").Page("").WebEdit("JKL").Set DataTable("DT4", dtGlobalSheet)
If Browser("ABC").Page("").WebEdit("JKL").GetROProperty("value") <> DataTable("DT4", dtGlobalSheet) Then
' Handle error popup if it exists
Browser("ABCError").Page("").WebButton("OK").Click
ExitTestIteration
End If
Browser("ABC").Page("").WebEdit("MNO").Set DataTable("DT5", dtGlobalSheet)
If Browser("ABC").Page("").WebEdit("MNO").GetROProperty("value") <> DataTable("DT5", dtGlobalSheet) Then
' Handle error popup if it exists
Browser("ABCError").Page("").WebButton("OK").Click
ExitTestIteration
End If
I have assumed that your repeated setting of Statement 1 etc in the question is supposed to emulate setting a number of different WebEdit text boxes and coded 5 sets as an example. If, as the iteration progresses, any of the text boxes is not set to the specified data table column value (DT1 to DT5 in my example) then the If condition will be satisfied, causing the attempt to handle an error popup (you'd need to amend my guessed code to suit your requirements) followed by the exiting of the test iteration. Exiting the iteration will trigger a new one to be started, so just be sure you handle any error popup before you exit.
If all WebEdits are completed successfully, then UFT will just repeat for all iterations defined in your data table.

You need to decide one thing: are you going to use the UFT way of Data-Driven testing or you would do the whole controlling yourself
Option 1 - Do the UFT Way
Tell Uft to interpret the DataTable properly - meaning execute the Test Case as many times as many rows there are in the Global Sheet: File->Settings->Run-> Run on all rows should be checked
Tell UFT that we are in a Datat-Driven mode. At the same page set the Setting; When error occurs during run session to proceed to Next Action Iteration - uft will start the whole thing with the beginning but next line
Add the five statements in Action 1 (That's all, UFT Takes care of the rest)
Option 2 - Do it in your way
Seems like you are already doing. You implement the Loop and iterate over all the lines of the Table
Extract the 5 Statements into a function (and Function Library)
Create a function that calls the previous one in a supervised manner (and Function Library)
PSEUDO-CODE
ACTION1
ForEach row In DataTable.GlobalSheet
ExecuteStatementsSupervised row
Next
FUNCTION LIBRARY
Function ExecuteStatementsSupervised(row)
On Error Resume Next
ExecuteStatements row
If Err.Number <> 0 Then
Reporter.ReportEvent micFail, "OOPS Failed"
End If
On Error GoTo 0
End Function
SubExecuteStatements(row)
Statement1
Statement2
....
Statement5
End Sub
So in Action1 we iterate over the DataTable, in the Supervisor function we catch the error if anything happens and make sure that the iterations can go on. The real function per se, just executes the steps

Related

UFT script using a data table runs only the first row repeatedly; doesnt consider the rest of the rows

I am working on a UFT script that reads the values from a global data sheet and inputs the value to a textbox and then performs a verification. But the script reads only the first value and not the rest of the values in the column (and runs the same value 6 times). I know I am missing something basic but can't put my finger on it; can you please help?
length (data table)
-100
200
100.01
0
100
25
Here is the code:
<opens the number dialog>
data_length = DataTable.GetRowCount 'returns 6
for i=1 To data_length
swfWindow("main_client").SwfWindow("tallyDialog").WinEdit("Current Value: -000.00).Set DataTable.Value("length") 'expecting it to read and input first value.
swfWindow("main_client").SwfWindow("tallyDialog").ActiveX("Enter") 'click enter
avg_length = swfWindow("main_client").SwfWindow("tallyDialog").Check (Checkpoint("Value must be from 0 to 100))
If avg_length then
reporter.reportEvent micPass, "test passed"
Else
reporter.reportEvent micFail, "test failed"
End if
Next
I was expecting it to do this iteration for all 6 values in the length table but it is doing this iteration 6 times for the first value (-100) What am I missing?
changing "run on all rows" to "run only one iteration" in the UFT settings also did not work.
There are a couple of issues to look at here. Here's some code that will do what you need, followed by an explanation.
Firstly, set your test to run only one iteration, since you are looping through the data table in this one iteration. If you were to avoid the loop in the code, then you could set it on "Run all rows" and it would iterate through the data table.
<opens the number dialog>
data_length = DataTable.GetRowCount 'returns 6
for i=1 To data_length
DataTable.SetCurrentRow(i)
swfWindow("main_client").SwfWindow("tallyDialog").WinEdit("Current Value: -000.00).Set DataTable.Value("length") 'expecting it to read and input first value.
swfWindow("main_client").SwfWindow("tallyDialog").ActiveX("Enter") 'click enter
avg_length = swfWindow("main_client").SwfWindow("tallyDialog").Check (Checkpoint("Value must be from 0 to 100))
If avg_length then
reporter.reportEvent micPass, "test passed"
Else
reporter.reportEvent micFail, "test failed"
End if
Next
Note the DataTable.SetCurrentRow command at the start of the loop, which sets the datatable row to the loop index i. This ensures that each time around the loop, the datatable picks up the correct data item for the iteration of the loop.
Give this a try and if you are still having issues, post a comment with your problem and I'll try to help further.

Subscript out of range in MSFlexgrid

I am using msflexgrid in VB6. How can I remove or resolve the following error:
Subscript out of range.
With flxData(0)
For i = 1 To .Rows - 1
Do While cboselect <> .TextMatrix(i, 1)
.RemoveItem (i)
Loop
Next i
End with
Just realized that the Do While loop inside of the For loop will cause the error to display - once a row doesn't have the desired cboselect value, it's going to call RemoveItem for that and all the remaining rows, until it's deleted all of them, then it'll display the message when it tries to delete a (now) non-existent row.
I'm guessing that you want to be removing just the rows that don't match the cboselect value, so that would call for an If statement. You also do need to run the For loop backwards. Try this:
With flxData(0)
For i = .Rows - 1 to 1 Step -1
If cboselect <> .TextMatrix(i, 1) Then
.RemoveItem (i)
End If
Next i
End With

VBA code with multiple For loops running very slow

my code is running very slow, is it the 2 For loops causing it?
Thanks
For x = LBound(dataArray) To UBound(dataArray) 'define start and end of array, lower bound to upper bound
For Each rngcell In Range("A:B") 'lookup each cell in row 1
If dataArray(x) = rngcell.Value Then ' if cells in header row match with values in array
rngcell.EntireColumn.Copy ' then copy whole column of data for that parameter
Sheets(3).Select ' select sheet to paste data
Range("A:B").End(xlUp).Offset(rowOffset:=0, columnOffset:=x).Select 'select area to paste, paste in next column - no. x
Selection.PasteSpecial xlPasteValues ' paste
End If
Next rngcell ' next header row cell
Next x
End Sub
Just a few suggestions:
Doing .Select causes Excel to update the UI, which is expensive. Try to calculate the target cell/range ONCE and use that to call PasteSpecial and not Selection.
Selecting the Sheet(3) could be done before the loop, as it doesnt change.
IF (!) max. ONE dataArray Element matches ONE rngcell.Value, you could abort the rest of the inner loop by using Exit For before the End If, saving the useless rest of the loop.
You're using Range(A:B) which is definitely slowing your code down.
Excel will basically read every cell in that range according to your code.
That's 2 million cells.
Try to limit the range on the B by using something like Replace(Range("B").End(Xldown).Address,"$B$","").

syntax error, expected end statement on QTP

Set obju=description.Create()
obju("name").value="user"
obju("html tag").value="input"
Arow=Browser("creationtime:=0").page("title:=.*").webedit(obju).set DataTable.Value("user",Global)
Arow=datatable.GetRowCount
For i = 1 To Arow Step 1
datatable.SetCurrentRow (i)
Next
its giving me error "The test run cannot continue due to a syntax error.
Expected end of statement"
anyone know whats going on?
In the line
Arow=Browser("creationtime:=0").page("title:=.*").webedit(obju).set DataTable.Value("user",Global)
you're assigning the return value from the webedit(obju).set procedure to the variable Arow. If you're doing this in VBScript then the arguments for the procedure need to be put in parentheses.
Following the logic of your code I'm not sure if you're intending to save a return value from the set procedure, in which case just take out assigning to the variable:
Browser("creationtime:=0").page("title:=.*").webedit(obju).set DataTable.Value("user",Global)
Similarly, in the for loop, the datatable.SetCurrentRow (i) procedure should be called as
datatable.SetCurrentRow i

Search WebList for particular item count from Excel sheet

I need a help in VBScript or either in QTP for the below case.
For example:
I have nearly 40 items in the weblist. I have only one item in the Excel sheet that is one among the 40 in the weblist. If I run the script, the one in the Excel should be select in the weblist. How do I perform this? I tried many scenarios, but couldn't get it to work.
Below are some of the sample pieces of code I tried in QTP:
ocount=Browser("name:=brw").Page("title:=brw").WebList("htmlid:=tabContainerBrandSite_123&rtyoh").GetROProperty("items count")
msgbox ocount
var7=mySheet2.Cells(2,"C")
For k=2 to ocount
ocount2=Browser("name:=brw").Page("title:=brw").WebList("html id:=tabContainerBrandSite_123&rtyoh").GetItem(k)
msgbox ocount2
merchantName = DataTable("Merchant_Name","Global") 'an example if value is saved in global sheet
items_count = Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").GetROProperty("Items Count") 'This will get all the items from your weblist.
i = 1
Do
webListName = Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").GetItem(i)
'this will get first value from the web list
If merchantName = webListName Then 'comparing first value from your value from global sheet
Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").Select(i) 'selects that value
Exit do 'because it has found your only value from the local sheet, it exits
else
i = i + 1
End If
Loop While i <= items_count

Resources