Count Number of links in a Web Page

In this let us see a script to count number of links in a web page.This will help to understand the descriptive programing better and will help to solve many practical requirements like finding the broken links.

Let us count the number of links in google’s home page

Set mygooglesearch = Description.Create()

Then you declare the property name and value pair for that object,here since we are counting the link,we have to use micclass property  of type link.

mygooglesearch(”micclass”).value =”Link”

Now use childobjects to get the properties into link_count objects,this will give all the child objects of that page of type link.

Set link_count = Browser(“name:=google”).Page(“title:=google”).childobjects(mygooglesearch)

Now we get the object count using count method.

msgbox link_count.count

Advertisements

Connecting to DB through QTP

Methods Used: ADO Connection object

What is ADO connection?

Microsoft’s ActiveX Data Objects (ADO) is a set of Component Object Model (COM) objects for accessing data sources. It provides a layer between programming languages and OLE DB (a means of accessing data stores, whether they be databases or otherwise, in a uniform manner). ADO allows a developer to write programs that access data without knowing how the database is implemented. You must be aware of your database for connection only. No knowledge of SQL is required to access a database when using ADO, although one can use ADO to execute arbitrary SQL commands. The disadvantage of this (i.e. using SQL directly) is that it introduces a dependency upon the type of database used.

To learn more about ADO visit http://www.w3schools.com/ADO/ado_ref_connection.asp

Let us look how to connect to database(DB) in QTP, the steps involved are

  1. Create an object of type ADODB.Connection.
  2. Using the connection string establish a connection to the DB.
  3. Execute the query and store the query result to Recordset collection.

Let us see code snippet how to connect to the flights MS Access DB

STEP 1:

Set connect = CreateObject (“ADODB.Connection”)

STEP 2:

This can be done by many ways, if there is a system DSN created then you can use that directly to establish a connection.

connect.open “QT_Flight32” ‘here this is the QT_Flight32 is the system DSN name.

if there is no system DSN, then you can give the DB details directly as

Connect.open “Driver={Microsoft ODBC for Oracle}:Server=DBserver,UID=Sa,Password=sa”

Here you have to give the Driver type like oracle,sybase then server name, user id and password.

STEP 3 :

Set RS = connect.execute(“select flight_number from flights where flight_number = 2004” )

Now the RS object has the query result ,using loop we can get those data.

Count1 = RS.fields.count

Do

For  i = 0 To count1-1

MsgBox(RS.fields(i).name)

MsgBox(RS.fields(i).value)

Next

RS.MoveNext

Loop until RS.EOF

Close All Opened Browser

Methods Used : Descriptive programming,Regular Expression and Ordinal Identifier Properties.

Call Closebrowser()

Function Closebrowser

While Browser(“CreationTime:=.*”).Exist
Browser(“CreationTime:=.*”).Close
Wend
End Functio
n

The above code will close all the browser that are opened.

Here instead of creation time other ordinal identifier can also be used.

Object Repository and Descriptive Programming in QTP

Pre Requisites: Basic of OOPS

Object Repository (OR) and Descriptive Programming (DP) are the two ways by which QTP recognize the objects present in your application under test (AUT).

First let see about OR, this is one of the greatest features that QTP has. You can easily capture all the objects from AUT and store it into QTP’s OR. During the play back QTP use this OR to identify the object on which the action is to be performed. Here QTP take care of all the object and it properties, you have a very good UI to edit an existing OR.

Let us look at an example Google Search button present in the Google’s home page.

We recognize the search button as below.

The same is stored into QTP OR as shown below.

So QTP use the properties of the object to identify each objects uniquely present in AUT.

The Description properties namely type, name and html tag are the mandatory properties used to identify the objects. If more than one object has all the three properties same then it uses other properties (assistive properties) of the object to identify the correct object.

What if there are more than one identical objects in same web page or window?

QTP can use the following types of ordinal identifiers to identify such objects:

  • Index. Indicates the order in which the object appears in the application code relative to other objects with an otherwise identical description.
  • Location. Indicates the order in which the object appears within the parent window, frame, or dialog box relative to other objects with an otherwise identical description.
  • CreationTime. (Browser object only.) Indicates the order in which the browser was opened relative to other open browsers with an otherwise identical description.

The ordinal identifier assigns the object a numerical value(index starts with zero) that indicates its order relative to other objects with an otherwise identical description (objects that have the same values for all properties specified in the mandatory and assistive property lists). This ordered value enables QuickTest to create a unique description when the mandatory and assistive properties are not sufficient to do so.

Below screen shot shows the Ordinal identifier value for the browser object.

Now to click Google Search button, you start from the browser hierarchy.

Browser (“Google”).Page (“Google”).WebButton (“Google Search”).Click

Now we are able to perform actions on the objects present in OR,then why DP ?

DP is a way in which you describe the object by writing your own code or script,instead of storing the objects in OR.

QTP records the properties of the objects at the time of recording and stores in OR, during playback it compares the properties of those in OR with run time objects and perform action on them. But what if the object is a dynamically generated during run time?.Let me give a situation where OR will not help you to identify an object. If you want to close all opened browser automatically before you start executing your scripts, it is not possible to store all the opened browser objects into OR. So this can be solved by using OR and regular expression.

Here in DP you have option to define the properties of objects in the script itself. There are two ways by which you can define your objects.

In first method, you can describe an object directly in a statement by specifying property:=value pairs describing the object.

The same Google search button present in OR can be described by code as

Browser(“google”).page(“google”).webbutton(“name:=Google Search”,”type:= Submit”).click

In above statement Browser,Page are identified from OR webbutton is identified from DP,so the webbutton need not to be captured and stored into OR.So here we described an object with property and value pair, first property is name which has value as Google Search and another property is type which has value of Submit.

Second way to define a DP object is by using the Description object,You start by defining an object for example

Set mygooglesearch = Description.Create()

Then you declare the property name and value pair for that object

mygooglesearch(“name”).value =”Google Search”

mygooglesearch(“type”).value =”Submit”

Now the above object can be used in the script instead of the object present in OR as

Browser(“Google”).Page(“Google”).webbutton(mygooglesearch).click

Now to release the object and to free the memory,set that object to Nothing

Set mygooglesearch = Nothing

NOTE: When using programmatic descriptions from a specific point within a test object hierarchy, you must continue to use programmatic descriptions from that point onward within the same statement. For example

Browser(mygoogle).page(“google”).webbutton(“Google Search”).click is invalid because once you start using DP you can’t come back to OR for that statement.

Browser(mygoogle).page(mygoogle).webbutton(mygooglsearch).click is a valid statement

What are the other advantages of DP?

  1. Getting Object collections.
  2. If same object appears in different page/window then instead of capturing the object in every  page/window use one DP object.

Look at this example on how to use DP to close all browser.

Summary:

This post explains  how QTP identifies objects and how to handle dynamic object using DP. OR the way in which QTP automatically stores the object, on the other hand DP is the way how we store the objects. DP is same as declaring a variable and assigning a value to it,here instead of value we assign object properties to the declared object.