Friday, June 8, 2012

QTP - Find Broken Links

Broken links can be found in couple of ways with the help of QTP:
Automatic Page Checkpoint, Manual Page Checkpoint, Script to find broken links and more.

You can use Page Checkpoint in QTP to find broken links on a web page.

Page checkpoint can be created automatically as well as manually.

Record the below script (open QTP, open IE and go to, record, type "ash" in search and hit enter), run it and see the results. As expected, no checkpoint information in the results.

Now let’s add Automatic Page Checkpoints

Go to Tools (menu)> Options > Web > Advanced and check the two checkboxes "Create a checkpoint for each page while recording" and "Broken Links".

Now QTP will automatically include Page Checkpoint while recording:

Record the same test as above, run it and see the results. QTP added checkpoint automatically and also shows Link Name, Url and its Status, whether passed or not (see below two screenshots).

We can also create Page Checkpoint manually.

Start Recording.
Go to Insert (menu) > Checkpoint > Standard Checkpoint (or press F12).
Click anywhere on your web page. It will show Object Selection – Checkpoint Properties dialog as below. Select Page and click ok.

It will show Page Checkpoint Properties dialog.

Check Broken links checkbox and click ok. The checkpoint is added, manually. You can check the results, as above it will show the Broken Links Results.

You can run the below script to find the broken links:

This below code (line 1 to 14) verifies only one link you enter in the 3rd line. You can modify this code accordingly. Like for example you can identify all links firstly by using below 5 lines:

Set ilink = Description.Create
ilink("micclass").Value = "Link"
Set all_links = Browser("Browser").Page("HomePage").ChildObjects(ilink)
'count of links
total_links = all_links.Count

and then using a loop, try each link in the third line.

1. Dim var
2. Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
3. objWinHTTP.Open "GET", "", False
4. objWinHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"
5. objWinHTTP.Send
6. iVal = objWinHTTP.Status
7. var=iVal
8. Select Case var
9. Case 404
10. msgbox ("Link is broken")
11. Case else
12. msgbox ("Link is NOT broken")
13. End Select
14. Set objWinHTTP = Nothing

Explanation of above code according to line number:

2. WinHttpRequest object is being created here.
3. Open method of WinHttpRequest object is being used here. Open opens an HTTP connection to an HTTP resource.
4. SetRequestHeader method of WinHttpRequest object is being used here. SetRequestHeader adds, changes, or deletes an HTTP request header.
5. Send Request to server and get response
6. Status property of WinHttpRequest object is being used. Status property Retrieves the HTTP status code from the last response.
9. Find out if the Link exists or is broken
The web site hosting server will typically generate "404 - Not Found" web page, when users attempts to follow a broken or dead link [Wikipedia]
List_of_HTTP_status_codes .....200 means request was fulfilled

How to check all links on a Web page?