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.

Sunday, July 17, 2011

Work with Shared Object Repository

Shared OR are more advantages than local OR.
1. A OR file which can be associated to any action and any test.
2. If the size of OR grows up, we can split that file to multiple files and associate to action\test.
3. Can be handled very easily i.e we can add local objects in OR to shared, merging of local to shared, merging two shared ORs, we can compare two Shared ORs etc...
4. QTP has so size limit for shared OR, but it recommends the file size is upto 1.5MB for performance reasons.
5. We can handle share ORs easily using OR Manager.

Disadvantages:
1. In a team environment, one team member updates\deletes an object, then all other team members who are working on that object get effected.
2. If we use a single file to hold all shared ORs then, then performance will go down.

Through the tool we can associate the share OR in multiple ways:
1. Edit -> Action -> Action properties -> Associated Repositories -> Browse ur shared OR
2. Resources -> Associate Repositories


We can programmatically add shared ORs in following ways:
Method 1:
Set App=CreateObject("QuickTest.Application")
Set ORS=App.test.Actions("Action1").ObjectRepositories
ORS.Add("c:\sharedOR.tsr")
ORS.Count 'returns the number of shared ORs associated
ORS.Find 'finds the shared OR
ORS.Remove 'removes the associated shared OR

Method 2:
SORpath = "c:\sharedOR.tsr"
RepositoriesCollection.Add(SORpath)
Same above methods can be used with RepositoriesCollection(Add, Count, Find, Remove etc..)

Method 3:
Set RepositoryObj = CreateObject("Mercury.ObjectRepositoryUtil")
RepositoryObj.Load "c:\sharedOR.tsr"
"RepositoryObj" util have more methods than the above.

Saturday, July 16, 2011

How to call a function in one action from another action

We can do this QTP, by passing the function name with parameters as Input parameters

Following is the script in my "Action1"
RunAction "LibraryAction",0,"displayMsg(3)"
FuncReturnVal = RunAction ("LibraryAction", oneIteration, "sum 5,6,7,8")
msgbox FuncReturnVal

Create a New action called "LibraryAction", can created an input parameter call "function" of type string
Following is the script in my second action "LibraryAction"

func = Parameter("function")
Execute func
Public function displayMsg(str)
msgbox(str)
End Function

Function sum(a,b,c,d)
temp=a+b+c+d
exitAction(temp)
End Function

Working with cookies in QTP

Cookies are temporary internet files which were created by Web Server in users Harddisk. When we make a request to a website, the Web Server looks for the

cookie in our Harddisk, and uses the stored information(if it finds), or creates a new cookie file in our system.
Only the webserver can understand the cookie information properly, we cannot understand the content of the cookie properly.
Cookies can be long-term, short term.
Long term cookies, can be saved as per the configured number of days(may be months or year)
Short term cookies get deleted once you visited the site and close the browser.
We can access the cookies information by Start->Run ->cookies and enter

We can test these cookies using QTP.

In QTP there is utility called "WebUtil" which was supported completely upto QTP 6.0. Later it was not supported fully. It is there only for backward

compatability.
We have couple of methods related to cookies like:
WebUtil.Addcookie
WebUtil.Deletecookie
WebUtil.Deletecookies 'deletes all cookies in the system
WebUtil.Getcookies

There is otherway around to delete cookies using Windows shell object:
SysUserName=Environment.Value("UserName")
Set ShellObj=CreateObject("WScript.shell")
ShellObj.run("cmd /C cd C:\Documents and Settings\" & SysUserName & "\Cookies & del *.txt")
Set ShellObj=nothing

The above two methods works with IE not with other browsers.

Wednesday, July 13, 2011

Work with command prompt using QTP(vbscript)

We can use Windows Shell object to start a command prompt and execute the command.

In the following i want to list all files\folders in C drive:
Set wscriptObj=createobject("WScript.Shell")
wscriptObj.Run "cmd /K cd c:\ & dir"

When i execute the above statement, it opens the command prompt and execute dir command. You are free to use any command you want as per your needs.

How to work with Windows registry using QTP(vbscript)

We can work with Windows registry using WScript.Shell object.

If you want to read a specific Key from registry
Set wscriptObj=createobject("WScript.Shell")
objProperties=wscriptObj.RegRead("HKLM\SOFTWARE\Mercury Interactive\QuickTest Professional\MicTest\Test Objects\Browser\CommonUse\")
msgbox objProperties
The above will give you the "Default" property value

If you want to read a specific property name in the registry:
objProperties=wscriptObj.RegRead("HKLM\SOFTWARE\Mercury Interactive\QuickTest Professional\MicTest\Test Objects\Browser\CommonUse\Sync")

If you want to update a specific property value in the registry:
wscriptObj.RegWrite "HKLM\SOFTWARE\Mercury Interactive\QuickTest Professional\MicTest\Test Objects\Browser\CommonUse\Sync",0,"REG_DWORD"

If you want to delete a specific property from the registry:
wscriptObj.RegDelete "HKLM\SOFTWARE\Mercury Interactive\QuickTest Professional\MicTest\Test Objects\Browser\CommonUse\Sync"


You can also use DotNetFactory to retrieve any retrieve any value
registryPath1="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\IEXPLORE.EXE"
'Here i am creating an instance of DotNet Factory computer object
Set objDFComputer=DotNetFactory("Microsoft.VisualBasic.Devices.Computer","Microsoft.VisualBasic")
'Here i am creating an instance of Registry Object
Set objRegistry=objDFComputer.Registry
'The below will retrieve the "Default" Property
print objRegistry.GetValue(registryPath1,"","")

'The below retrieves the property of the regsitry
print objRegistry.GetValue(registryPath1,"Path","")
Set objRegistry=nothing
Set objDFComputer=nothing

Thursday, July 7, 2011

Different ways to invoke a IE browser

'commonly used method
systemutil.Run "iexplore.exe"
wait(10)
Browser("name:=.*").Navigate "www.yahoo.com"

'For QTP backward compatability
invokeapplication "C:\Program Files\Internet Explorer\iexplore.exe http://www.yahoo.com"

Set WShelObj=Createobject("WScript.Shell")
WShelObj.Run Chr(34) & "C:\Program Files\Internet Explorer\iexplore.exe" & Chr(34)&" "&"www.yahoo.com"
or
Set WShelObj=Createobject("WScript.Shell")
WShelObj.Run Chr(34) & "C:\Program Files\Internet Explorer\iexplore.exe" & Chr(34)
Browser("name:=.*").Navigate "www.yahoo.com"
Char(34) is used to append double quotes to the IE application path.

Sunday, July 3, 2011

What test cases to be automated

Following are to name a few common test cases which needs to automated:
1. Test Cases that belongs to core functionality(which is less frequent or no change in functionality) sometimes called regression test cases.
2. Test Cases which needs to executed, but taking long times to execute manually.
3. Identify reusable actions which are used by most of the test cases.
4. The same test case which needs to verify with different values(data driven). The same test case with different environments(compatibility).

Importance of WaitProperty method

We generally use WaitProperty with below syntax like
Object.WaitProperty "visible",1,10000

But assume i have a EditBox, and any data can be populated in this field. QTP should wait until it populated with data, and then proceed further after it populated with data. To handle these situations use below syntax:
Dim vEmpty 'Here vEmpty is uninitilized with any value, so it have empty value.
Object.WaitProperty "text",micNotEqual(vEmpty)

Common comparisons we use here are:
micGreaterThan
micLessThan
micGreaterThanOrEqual
micLessThanOrEqual
micNotEqual
micRegExpMatch

VB Script to find the number of occurences of a char in a string

Method 1:
str="Hello world"
Set regex=new RegExp
regex.pattern="l"
regex.global=true
Set matches=regex.execute(str)
msgbox matches.count

Method 2:
arr=split(str,"l")
msgbox ubound(arr)

Method 3:
counter=0
For i=1 to len(str)
char=mid(str,i,1)
If char="l" Then
counter=counter+1
End If
Next

msgbox counter