Friday, September 30, 2011

Working with Dictionary Object

Set dictObj=Createobject("Scripting.Dictionary")
dictObj.Add "1","One"
dictObj.Add "2","Two"
dictObj.Add 3,"Three" 'Observe here i am passing key as a integer meaning we can pass different types as Key

msgbox dictObj.Count 'This method returns the number of keys

If dictObj.Exists(3) Then 'This method looks for the Key
msgbox "Key exists"
else
msgbox "Key does not exist"
End If

a=dictObj.Keys 'This method assigns all the keys to an array
For i=1 to dictObj.Count
msgbox dictObj.Item(a(i-1)) 'This method retrieves the value of the key
Next

Sunday, July 24, 2011

How to select a check box\click a link in a web table with text

This is one of the most common interview question.
The first column of a web table contains checkboxs, and in the second column have some text.
If any row in the second column contains a text say "uday", then the corresponding checkbox in the first column should be selected.
How can we do that?

It is pretty simple: :)
rowNo=B().P().WT().getrowwithcelltext("uday",2) 'Here "uday" is my search string and 2 the column number
set childObj=B().P().WT().ChileItem(rowNo,1,"webcheckbox")
childObj.set "ON"

Friday, July 22, 2011

Different ways of synchronizing in QTP

In QTP, we can do synchronizing between QTP tool and application in many ways:

1. The best one is, use synchronization point, which waits until the object meets the criteria. The general syntax is:
Object.waitproperty "your criteria"
Ex: B().P().WB().Waitproperty "enabled",1,10000
B().P().WE().WaitProperty "text",micNotEmpty(var)
(Here the tool waits until the object meets the criteria).

2. Wait(least preferred): It is like hard coding the time. The syntax is:
Wait(x) ' waits x seconds irrespective of the object state. For Ex. If we set the x as 60 Seconds and the object is ready by 2 seconds, even then it waits for 60 seconds.
If the object is not ready by 60 seconds, it wont bother and it moves to the next step(may causes script fail).

3. We can use Wait and Exists in some cases

4. And also we can use Wait and CheckProperty, which almost works like synchronization point.

5. You can also configure the default settings in the tool.
Browser navigation timeout : File -> Settings -> Web ->Browser navigation timeout
Object synchronization timeout : File -> Settings ->Run -> Object synchronization timeout.

6. We can also write custom script for QTP like below to wait till the application is loaded.

Set oPage=B().P().Object
While oPage.readystate=”Completed”
Wend

While oPage.Busy
Wend

Tuesday, July 19, 2011

Some common QTP Tips & Tricks:

Maximum no. of actions can be created:
From QTP 9.0(or later) max of 120 actions can be created. We can extend this by installing private patch.
From QTP 8.2(or below) max of 255 actions can be created.

How to find which version of vbscript you are using?
Search for vbscript.dll in c:\windows\system32. In properties -> Version tab
Programatically we can retrieve this by:
msgbox ScriptEngineMajorVersion &"."& ScriptEngineMinorVersion

Associating function library Vs executefile
Use to associate functional libraries to a test rather than using execute file.
By associating functional library, all actions in the test can access those functions.
By calling executefile, all functions are loaded in the calling action only.

Recovery Scenario Vs On Error Resume Next
Recovery scenarios can be used when we dont know where an error arises.
We can use On Error Resume Next when we know the occurance of an error and we dont want to halt the execution because of that error.

Loading function library at run-time
From QTP 11 onwards we can use "LoadFunctionLibrary" statement to load Function library at run-time.
We can load multiple functional libraries in a single call like
LoadFunctionLibrary "c:\flb1","c:\flb2" etc...

Generating Random number
Generating a random number between min and max.
Syntax is: Int((max-min+1)*Rnd +min)
The below example generates a random number between 10 to 50 in each iteration
For i=1 to 5
msgbox Int((50-10+1)*Rnd +10)
Next

Arrays with redim
Small example with arrays and redim preserve
a=Array(5,2,50,33,21)
For i=0 to ubound(a)
msgbox a(i)
Next
ReDim preserve a(7)
a(5)=10
a(6)=20
For i=0 to ubound(a)-1
msgbox a(i)
Next

Monday, July 18, 2011

Shared Object Repository Vs Dynamic Programming

When to use shared repository(SOR) vs descriptive programming(DP).
1. QTP can perform faster with SOR, because while replying a script if it encounters an object, it will create a temporary test object with the description, and it uses this temporary test object for the rest of the test execution rather than building this test object for each occurrence.
Where as in "Static DP" that is not the case, each time it builds the objects and uses while execution.
If properly written\coded, "Dynamic DP" have the same performance as Shared OR.

2. Object identification with OR is more handy, because we can configure properties for mandatory & assistive properties and ordinal identifiers and also we can enable smart identification mechanism for object identification.
Where as in DP, all the properties used are treated as mandatory properties.

3. We can associate a single SOR and we can access in any action\test.
Where as in static DP, we build objects in the action\test, which cannot be accessible in other actions\tests.
Where as using dynamic DP, we can write driver script which loads all objects(say in dictionary object) and can be used.

4. QTP automatically creates the SOR while recording or manually adding Objects to OR.
Where as in DP, we need to identify unique properties in the application, and use those properties which is a time consuming task.

5. With SOR we can export\import objects to XML files. No such functionality with DP.

6. DP is more useful when working with dynamic objects.
Ex: want to verify all links in a webpage. Here we just create an single link object description and use it to verify all the links. That is not the case with SOR, we have to store and each and every link(which is not always possible, because the links are dynamic).

7. Regular expressions can be used in both SOR or DP.

8. Parametrization can be done in both SOR or DP.