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

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.

Related

AddField function does not work as expected

I have the following block of code that iterates through the fields of each table and adds the fields of the current table respectively in order to create a number of tableboxes.
'iterate through every table
For i=1 To arrTCount
'the arrFF array holds the names of the fields of each table
arrFF = Split(arrFields(i), ", ")
arrFFCount = UBound(arrFF)
'create a tablebox
Set TB = ActiveDocument.Sheets("Main").CreateTableBox
'iterate through the fields of the array
For j=0 to (arrFFCount - 1)
'add the field to the tablebox
TB.AddField arrFF(j)
'Msgbox(arrFF(j))
Next
Set tboxprop = TB.GetProperties
tboxprop.Layout.Frame.ObjectId = "TB" + CStr(i)
TB.SetProperties tboxprop
Next
The above code creates the tableboxes, but with one field less every time (the last one is missing). If I change the For loop from For j=0 To (arrFFCount - 1) to For j=0 To (arrFFCount) it creates empty tableboxes and seems to execute forever. Regarding this change, I tested the field names with the Msgbox(arrFF(j)) command and it shows me the correct field names as I want them to be in the tableboxes in the UI of QlikView.
Does anybody have an idea of what seems to be the problem here? Can I do this in a different way?
To clarify the situation here and what I have tested so far, I have 11 tables to make tableboxes of and I have tried with just one of them or some of them. The result I am seeing with the code is on the left and what I am expecting to see is on the right of the following image. Please note that the number of fields vary for each table and the image has just one of them as an example.

Error handling in 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

Applescript Error "can't get end of"

I am trying my hand at Applescript and can't see anything wrong with this.
The Error I get is
error "Can’t get end of {button returned:\"OK\", text returned:\"3\"}." number -1728 from last insertion point of {button returned:"OK", text returned:"3"}
This is my code:
beep
set counter to 0
set tempX to 0
set temp to 0
set counting to 0
set stored to {0}
set input to "How many grades do you wish to enter?" as string
set str to display dialog input buttons {"NEXT"} default button "NEXT" default answer ""
repeat text returned of str times
counting = counting + 1
set grades to display dialog "GRADES: " default answer ""
set stored to grades
end repeat
set rep to the length of stored
repeat rep times
counter = counter + 1
set tempX to the ((end of stored) - counter) as number
set temp to temp + tempX
end repeat
set ln to the length of grades
set average to temp / ln
if text returned of str is 1 then
say "The Average of your grade is " & average using "Zarvox"
else
say "The Average of your grades is " & average using "Zarvox"
end if
get "AVERAGE: " & average
So, before I begin: I'd strongly recommend that you teach yourself how to use the Javascript interface to Apple Events, rather than the Applescript language itself. Applescript is a really weird language, and its quirks are largely unique; learning it is going to be frustrating, and won't help you learn other languages.
That being said, let's dive into your code:
set stored to {0}
This would start you out with one grade that's always present and set to zero. You probably want to just initialize this to an empty list:
set stored to {}
Next:
set grades to display dialog "GRADES: " default answer ""
This sets grades to a result object, not just the answer. What you probably want here is actually the text returned of the result:
set grades to text returned of (display dialog "GRADES: " default answer "")
(This is what's creating the really weird-looking object in your error message.)
Next, you overwrite stored with this result object:
set stored to grades
What you probably want here is to insert this element into the list. Because Applescript is a strange and obnoxious language, this is somewhat more cumbersome than you're thinking:
set stored to stored & {grades}
Finally, there's some logical issues with your averaging; you're adding the end of stored (that is, the last grade input) to the temp variable each time. A much simpler approach would be:
set temp to 0
repeat with n in stored
set temp to temp + n
end repeat
set average to sum / (count of stored)
With these changes all made, your script should work correctly.

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$","").

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