Monday 24 March 2014

How to get current browser URL in QTP

There are two ways:
 1.  Using GetROProperty("URL") method (run-time object property)
       2. Using "URL" property of "Object" (IE DOM Object)
Using GetROProperty("URL") method (run-time object property)

GetRoProperty function reads properties of a run-time object in an application.

So, for example:

  • To check whether a link is visible or not, use:
    Browser("bname").Page("pname").Link("lname").GetROProperty("visible")
  • To get a page's title, use:
    Browser("bname").Page("pname").GetROProperty("title")
  • To check whether a window is maximized, use:
    Window("wname").GetROProperty("maximized")
In our case, to get URL of the current WebPage, we will use:
Browser("bname").Page("pname").GetRoProperty("URL")

"URL" property of "Object" (IE DOM Object)

The sample syntax is:

Browser("bname").Page("pname").Object.URL


Tuesday 18 March 2014

Actions vs Functions in QTP

There are few pros and cons in both approaches but I prefer functions over actions.
Some key points when using Actions are:

·    Action parameters can’t accept complex values like arrays or object.
·          Actions allow us to have optional parameters.
·         A blank Action with no object and no code occupies 150+KB of spaces, so using too many Actions increases the size of scripts.
·         Re-usable Actions, when called in test are read only. So if, there is an issue in running the test and the Action’s code need to be updated for fixing it, we would need to close the test and re-open it containing the re-usable action. After making changes to the Action code we need to open the test again for execution and in case it fails we must repeat the procedure for fixing the code.
·         Actions create a separate section in the test results summary making it easier to differentiate functionality.
      Compared to Actions, Functions have few advantages:
·         Functions don’t take much space since they don’t have associated Local OR or DataTable.
·         Functions can be edited easily as compared to external actions. This make maintenance much easier.
·         Functions parameters can take any possible value supported by VB Script.
·         Functions can easily be overridden by redefining them.
So, I prefer Functions because maintenance becomes huge when we have too many Actions.

Monday 10 March 2014

How to split string without using split function

Dim str,strRes,strchar
str="i am busy with work"
dim strarray()
Redim preserve strarray(0)

for i = 1 to len(str)

    strchar=mid(str,i,1)
    if strchar<>" " then
        strRes=strRes & strchar
    End if
       
        If strchar=" " Then
                  strsize=ubound(strarray)
                  Redim preserve strarray(strsize+1)
                 strarray(strsize+1)=strRes
                 strRes=""
        End If

        If len(str)=i Then
                strsize=ubound(strarray)
                  Redim preserve strarray(strsize+1)
                 strarray(strsize+1)=strRes
        End If
 
Next

for i=1 to ubound(strarray)
msgbox strarray(i)

next

Thursday 6 March 2014

Dynamic Synchronization in QTP

Dynamic synchronization allows waiting till the object property is changed or till time out.
Sample example:

Window("WinFlight").ActiveX("ThreedPanelControl").WaitProperty "text", "Insert Done...", 10000

The waitproperty can be inserted using Insert Synchronization point or we can manually add the waitproperty method by typing  typing it.
Apart from this we can also add synchronization point using following code also

Sample code: 1
Function ManualSyncPoint (Object,propertyname,propertyval,timeout)
Do
If Object.GetRoProperty(propertyname)=propertyval Then
            'if propertyname and propertyval match, come out of the loop and execute next step
           
                        Exit Do
           
            Else
                        Wait(1)
            End If
Loop while(i<=timeout)

End Function

In the preceding method we have used the static wait; we can implement it without using wait statement as well.

Sample code:2

Function ManualSyncPoint2(Object ,propertyname, propertyval, timeout )
sttimer = Timer
'Timer allows to get the number of seconds elapsed since midnight
(12:00 AM)
Do
If Object.GetROProperty(propertyname) = propertyval then
Exit Do
else
end if
endtimer = Timer
'The difference between the two timer objects provides the number of seconds we need to determine for timeout
Loop While ( int (endtimer-sttimer) <= -timeout)
End Function

Function that allows to wait till object exists
Sample code:3

Function WaitTillExists(Object, timeout )
loadtimer = Timer
Do
isObjectExists = Object.Exist
If isObjectExists = true Then
Exit Do
else
End If
completetimer = Timer
Loop While(completetimer-loadtimer <= timeout)
End Function

           
                       

            

Wednesday 5 March 2014

Different ways to add Test Object to Object Repository

There are multiple ways to add test object to Object Repository in QTP.

1.    Record a test step.
2.    Add object to local.
3.    Create a test object by using define new test objects.
4.    Creating an Object Description.
5.    OR Manager Navigate and Learn or Add object option.
6.    Adding test objects to local object repository from Active Screen.

Recording a test step

Test objects are created automatically and added to the local OR when a user records
a step. Add object to local.

Adding a test object to OR

Add a test object to the local OR through the following steps:
1. Navigate to Resources | Object Repository | Add object to local.
2. Click on the button Add Object to local.
3. Click on the hand pointer to the object we want to add from AUT
4. Click on OK.
Create a test object by using define new test objects

To define a new object, we should know its class and its identification properties.
Perform the following steps:
1. Navigate to Resources | Object Repository | Define New Test Object.
2. Filter Environment from the Define New Test Object window.
3. Select the class from the Class drop-down box.
4. Enter a logical name.
5. Provide the value for the Identification property (1 to n).
6. If required, add some identification properties.
7. Click on the Add button to add a test object to the OR.

Creating an Object Description

The test object can be created without recording, by providing an object description.

We can use the object description in the test steps directly, as shown in the following
code:
Window("regexpwndtitle:=FlghtReservation").WinComboBox("attached
text:=Fly From:").Select "London"


OR Manager Navigate and Learn or Add object option

The Navigate and Learn toolbar

This toolbar allows you to add multiple test objects to a shared object repository
while you navigate through your application. Following are the steps to learn to
navigate the object in the object repository manager:

1. Navigate to Resources | Object Repository Manager | Object | Navigate
and Learn or press F6.


2. Select a window to learn. The selected window and its descendant objects
are added to the active shared object repository according to a predefined
object filter.

Adding a test object using the OR Manager Add Object option

1. Navigate to Resources | Object Repository Manager | Objects | Add
Object. Click on the hand pointer and select the window.

2. The object filter is used for the Navigate and Learn option as well as the Add
Objects option.

Adding test objects to the local object repository from Active Screen

Select the required object from Active Screen and add it to the object repository. In
order to add test objects to the object repository using Active Screen, Active Screen
should have the object. Perform the following steps:

1. Go to the Active Screen pane.
2. Select an object and right-click on it.
3. Click on View / Add Object.
4. Click on the Add to Repository button.

Tuesday 4 March 2014

Function to Create Log file

When we have developed automation framework, we also keeping execution log in it. Here I have given sample function for creating log file.

Function CreateLogFile
            Dim Curtime, LogFilePath, FilePath ‘Declaration part
            Curtime=Now() ‘This will give result like 1/1/2015 12:00:00 PM
            Curtime=Replace(Curtime,”/”,”-“)
            Curtime=Replace(Curtime,”:”,”_”)
Environment(“FilePath”)=”C:\QTP  ‘Creating Environment variable
Environment(“FilePath”)=FilePath
LogFilePath=FilePath & “Results\” & Curtime & “.log”
Environment(“LogFilePath”)=LogFilePath
Set Fso=CreateObject(“Scripting.FileSystemObject”)
Fso.CreateTextFile LogFilePath

End Function