Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QTP Tips and Tricks
#1
Question 
Let us share some tips and tricks we come across when working with QTP.

If you want to share a New trick,add appropriate title in the "Post Subject" after clicking on the "New Reply" link.





Setting.Add trick to use run session values in next run sessions


Suppose you want to save some values manipulated in the run session somewhere so that you would like use them for next run session, the only ways(i think) are to save them to some external files like excel or output the values to global sheet.

I just went through the topic
"Setting Testing Options During the Run Session" which explains about
Setting.Add through which we can add some key value pairs more or less same like Environment variables

If you change the user defined Environment variables(pre-defined or dynamically created) in run time,they are all lost when the run session ends as they are reset to their original values and it can throw error if you try to use some dynamically created Environment variables of the last run !


So,the trick which i found is,
Just save your values in the form of 'key value pair' to Setting.Add "key","value"
which once created can be accessed in any run session thereafter.

Example of how to use it.
Code:
If Not Setting.Exists("IterNum") then
Setting.Add "IterNum", 1 'create "IterNum" and assign to 1
else
msgbox Setting.Item("IterNum") 'Use it or modify it as written below
Setting.Item("IterNum")=Setting.Item("IterNum")+1 ' modify "IterNum"
end if
Now run this and see, first time you run, it will create a new setting item and next time you run, the message box promt will show the value.

Isn't it cool?Shy

Note:These values will not be saved with the test after you close the test or open some other test(or create a new test)l!
Reply
#2


As you know, at a time you can open only one test and work with it.
Sometimes you may wish to see scipt of other tests along with current test to compare/modify them.
For this,in the file sytem, manually navigate to test folder.
Drag the "script.mts" present inside the action folder to QTP tool.
Now in QTP tool, you will see a new window to view/edit&save that script.

You can drag as many script files as you wish

Note:You can not run tests this way!you can only change the content of script.

Reply
#3
Nice initiative rajpes. Great to find that you are helping community members not only with your knowledge but also with such ideas.
Want to fast track your QTP/UFT Learning? Join our UFT Training Course
Reply
#4
If you feel, the execution of tests has slowed down or there is need to increase the speed ,just keep these few points in mind.

1. Do not load unwanted Add-ins !
Selecting an add-in(eg: web) just because it was selected by default can only reduce performance if you are working with windows applications.Load only required add-ins

2.Run mode=Fast!
Tools>options>Run mode>set it to fast! You will need to have installed Microsoft script debugger to change settings here.

3.Active Screen!
This actually consumes lot of disk space and usually we don't really need active screen which is displayed by default when we open QTP.If you have noticed,while editing scripts,it takes fraction of seconds or more to update Active screen window on each step.So if you are not in need of it, just toggle it (View>Active screen) or close the window(x).
When it comes to saving the tests, again if you don't need any active screens to be stored just clear the option 'save active screen files' in 'save test' dialog box.
If you really need active screen,check capture level in Tools>options>active screen>
minimum/partial would be sufficient

4.Images and movie capture!
Tools>options>Run
Uncheck 'save movie results' and 'save still images'
If you really want images set it 'for errors' instead of 'always'

5.Recovery Scenario!
This is very crucial as far as performance hit is concerned.Try not to use them.Try to run scripts in a separate machine where unwanted softwares are not installed (eg:printers,anti virus e.t.c).Some browser popups can be disabled manually by selecting "do not show this next time" before actually automating scripts.
Prefer 'Exist' method rather than blindly using recovery scenario if you are sure of what exceptional behavior could occur at what point of time.

If it is really needed, make sure in file settings>recovery scenario, you configure "activate recovery scenarios" appropriately. See if 'On error' would handle the job instead of 'on every step'.

6.Importing sheets!

When you import excel sheets using datable methods, make sure you are not dumping the sheet in every iteration!
Eg:If the first line of your script is Datatable.Import(filename) and you have enabled "run on all iterations", you are simply importing the sheet every time unnecessarily.In such cases, set mode to "run one iteration only" and use a loop after import method

Datatable.Import(filename)
for i=1 to some_count
Datatable.SetCurrentRow i
'do some operation
next

or

If Environment("ActionIteration") = 1 Then
DataTable.ImportSheet "E:\......xls" ,1 ,"Global"
End If

7.Lengthy test script
Try to divide lengthy test scripts into appropriate actions though it is not really a big parameter in performance consideration.


If i find any more tips on this, will update this post.
Reply
#5
SystemUtil.Run "www.google.com"
'yes,it works!not a good practice though
Reply
#6
Just a puzzle here,You need print the pattern like below depending on no. of rows

rows=3,
**1**
*212*
32123

rows=4,
***1***
**212**
*32123*
4321234


Code:
r=inputbox ("enter no of rows")
c=(2*r)-1
m=(c-1)/2

ReDim a(r-1,c-1)
For i=0 to r-1
    For j=0 to c-1
        a(i,j)="*"
    Next
Next
'mid of all rows
For i=0 to r-1
  a(i,r-1)=1
Next


'rows manipulation
For i=1 to r-1
    For j=0 to m-1
                'If previous row has a value at next column position,increment it by 1
        If a(i-1,j+1)<>"*" Then
            val=a(i-1,j+1)+2 '2 instead of 1 because i am decrementing 'val' by 1 in next line
            For k=j to m-1
                 val=val-1
                 a(i,k)=val
                a(i,c-1-k)=val
            Next
        End If
    Next
Next

'print
For i=0 to r-1
    For j=0 to c-1
      txt= txt& a(i,j)
    Next
    txt=txt&vblf
Next
msgbox txt
If you have a better tested code, please post it
Reply
#7
If you need to check if an object like webedit has the focus on it as soon as the page is loaded,

If browser().page().webedit().GetROProperty("focus")=true then
msgbox "yes, focus is there"
end if

'Interestingly object spy doesnt show this property.So this is another cool trick!Wink
Reply
#8
Assume there is a weblist with "n" items in it and you need to select the items in it in a loop.
'To select the items always start index with 0
and to GetItem start index with 1

for i=0 to n-1
browser().page().weblist().select "#"&i
next

for i=1 to n
msgbox browser().page().weblist().GetItem (i)
next
Reply
#9
Code:
on error resume next
msgbox DataTable("col",dtGlobalSheet)
If err.number<>0 then
      msgbox "col doesnt exist"
else
   msgbox "col  exists"
end if
Reply
#10
Sometimes you may need to compare two datasheets in run time datatable.
The trick is to supply empty string in "filename" parameter of importsheet method.
Fallowing code copies global sheet's data into "NewSheet"
Code:
DataTable.AddSheet "NewSheet"
DataTable.ImportSheet  "", "Global", "NewSheet"
'Change values in global sheet , it doesnt affect newsheet's values!
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)