Find Broken Links using QTP without directly opening the links
Consider this scenario. You open the Home page of the web application that you are testing. In your Home page, you’ll have many links pointing to various sections of the website. Your task is to check that there should be no broken links (or dead links) in the Home page. i.e. All the links should open successfully without throwing ‘Page Not Found’ error.
The standard approach to find out the broken links in this case is depicted in the below flowchart.
Rather than using this method, we would try using a different way to check if the links available in a given page work properly or are broken. In this method, the code uses each of the links to create an HTTP Request. This request is then sent to the server and depending upon the response (e.g. 200 – Page Found, 404 – Page Not Found), you will come to know if the link is working or broken. With this method you can check if the page exists or not without opening the page directly.
Let’s see the code which you can use to find out broken links in your application.
'This is the main page Browser("Browser").Page("HomePage").Sync 'Find out all the links in the page using ChildObjects Set oLink = Description.Create oLink("micclass").Value = "Link" Set oAllLinks = Browser("Browser").Page("HomePage").ChildObjects(oLink) 'Find out the count of links iTotalLinks = oAllLinks.Count 'Loop through all the links to find if the link is broken or not For i=0 to iTotalLinks - 1 'Find out the url for the link sURL = oAllLinks(i).GetROProperty("url") 'Create a WinHTTP Request using the link's URL Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") objWinHTTP.Open "GET", sURL, False objWinHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)" 'Send the Request to the Server and capture the response objWinHTTP.Send iReturnVal = objWinHTTP.Status 'Find out if the Link exists or is broken If iReturnVal = 200 Then msgbox "Link - " & sURL & " Exists" ElseIf iReturnVal = 404 Then msgbox "Link - " & sURL & " is Broken" Else msgbox "Code" - iReturnVal End If Set objWinHTTP = Nothing Next
The above example uses only 200 and 404 HTTP status codes. Below is the list of all the HTTP status codes that you can use to refine this code as per your requirement.
Code | Status | Description |
100 | HTTP_STATUS_CONTINUE | The request can be continued. |
101 | HTTP_STATUS_SWITCH_PROTOCOLS | The server has switched protocols. |
200 | HTTP_STATUS_OK | The request is completed successfully. |
201 | HTTP_STATUS_CREATED | The request has been fulfilled and resulted in the creation of a new resource. |
202 | HTTP_STATUS_CREATED | The request has been accepted for processing, but the processing has not been completed. |
203 | HTTP_STATUS_PARTIAL | The returned meta information in the entity-header is not the definitive set available from the originating server. |
204 | HTTP_STATUS_NO_CONTENT | The server has fulfilled the request, but there is no new information to be sent back. |
205 | HTTP_STATUS_RESET_CONTENT | The request has been completed, and the client program should reset the document view that caused the request to be sent to allow the user to easily initiate another input action. |
206 | HTTP_STATUS_PARTIAL_CONTENT | The server has fulfilled the partial GET request for the resource. |
207 | HTTP_STATUS_WEBDAV_MULTI_STATUS | During a World Wide Web Distributed Authoring and Versioning (WebDAV) operation, this indicates multiple status codes for a single response. The response body contains Extensible Markup Language (XML) that describes the status codes. |
300 | HTTP_STATUS_AMBIGUOUS | The requested resource is available at one or more locations. |
301 | HTTP_STATUS_MOVED | The requested resource has been assigned to a new permanent Uniform Resource Identifier (URI), and any future references to this resource should be done using one of the returned URIs. |
302 | HTTP_STATUS_REDIRECT | The requested resource resides temporarily under a different URI. |
303 | HTTP_STATUS_REDIRECT_METHOD | The response to the request can be found under a different URI and should be retrieved using a GET HTTP verb on that resource. |
304 | HTTP_STATUS_NOT_MODIFIED | The requested resource has not been modified. |
305 | HTTP_STATUS_USE_PROXY | The requested resource must be accessed through the proxy given by the location field. |
307 | HTTP_STATUS_REDIRECT_KEEP_VERB | The redirected request keeps the same HTTP verb. HTTP/1.1 behavior. |
400 | HTTP_STATUS_BAD_REQUEST | The request could not be processed by the server due to invalid syntax. |
401 | HTTP_STATUS_DENIED | The requested resource requires user authentication. |
402 | HTTP_STATUS_PAYMENT_REQ | Not implemented in the HTTP protocol. |
403 | HTTP_STATUS_FORBIDDEN | The server understood the request, but cannot fulfill it. |
404 | HTTP_STATUS_NOT_FOUND | The server has not found anything that matches the requested URI. |
405 | HTTP_STATUS_BAD_METHOD | The HTTP verb used is not allowed. |
406 | HTTP_STATUS_NONE_ACCEPTABLE | No responses acceptable to the client were found. |
407 | HTTP_STATUS_PROXY_AUTH_REQ | Proxy authentication required. |
408 | HTTP_STATUS_REQUEST_TIMEOUT | The server timed out waiting for the request. |
409 | HTTP_STATUS_CONFLICT | The request could not be completed due to a conflict with the current state of the resource. The user should resubmit with more information. |
410 | HTTP_STATUS_GONE | The requested resource is no longer available at the server, and no forwarding address is known. |
411 | HTTP_STATUS_LENGTH_REQUIRED | The server cannot accept the request without a defined content length. |
412 | HTTP_STATUS_PRECOND_FAILED | The precondition given in one or more of the request header fields evaluated to false when it was tested on the server. |
413 | HTTP_STATUS_REQUEST_TOO_LARGE | The server cannot process the request because the request entity is larger than the server is able to process. |
414 | HTTP_STATUS_URI_TOO_LONG | The server cannot service the request because the request URI is longer than the server can interpret. |
415 | HTTP_STATUS_UNSUPPORTED_MEDIA | The server cannot service the request because the entity of the request is in a format not supported by the requested resource for the requested method. |
449 | HTTP_STATUS_RETRY_WITH | The request should be retried after doing the appropriate action. |
500 | HTTP_STATUS_SERVER_ERROR | The server encountered an unexpected condition that prevented it from fulfilling the request. |
501 | HTTP_STATUS_NOT_SUPPORTED | The server does not support the functionality required to fulfill the request. |
502 | HTTP_STATUS_BAD_GATEWAY | The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request. |
503 | HTTP_STATUS_SERVICE_UNAVAIL | The service is temporarily overloaded. |
504 | HTTP_STATUS_GATEWAY_TIMEOUT | The request was timed out waiting for a gateway. |
505 | HTTP_STATUS_VERSION_NOT_SUP | The server does not support the HTTP protocol version that was used in the request message. |
Use the comments section to let us know if this was useful for you. Happy Reading.. :–)
If you enjoyed this article, you can join our blog to get new articles delivered directly in your inbox.
To check out QTP tutorials, visit our QTP Tutorials page. You can also check the Archives page to view the list of all our articles.