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

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.