site
stats
By Anish Pillai Anish Pillai Posted under Advanced Concepts | Not So Common Stuff

Find Broken Links using QTP without directly opening the links

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

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.

Automation Repository Home Page


The standard approach to find out the broken links in this case is depicted in the below flowchart.

Checking Broken Links - The Normal Way


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.

Identify Broken Links with WinHTTPRequest


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.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
  • Veena

    Hi Anish
    I tried to run the above code , got the following error

    The URL does not use a Recognized protocol

    • Anish10110

      Hi Veena,

      On which line are you getting this error?

  • Madhu

    correct the above script .. Line(2)
    variable declaration

    oLink("micclass").value="Link"

    .value should be mentioned.

    • Anish10110

      Hi Madhu,

      Thank you for pointing out the mistake. I have corrected it now. Thanks.. 🙂

  • harish

    i tried the above code. but getting only 400 as the server response even though URL is working.

    • Anish10110

      Hi Harish,

      Can you share the URL that you were testing with this logic?

  • Maruti

    Hey Anish, I got the same error 'The URL does not use a Recognized protocol ".

    URL: http://MyCompanyENVironment/default/Level1NavWindow?mode=view&action=e&windowstate=normal&location=%2FWEB-INF%2Fjsp%2Fnavigation%2Flevel1Tabs.jsp&webwork.portlet.action=renderDirect&webwork.portlet.mode=view

    Please advice. Thanks in advance.

    Can you elaborate below as well.

    objWinHTTP.Open "POST", sURL, TRUE
    objWinHTTP.SetRequestHeader "User-agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"

    • Anish10110

      Not really sure why this error came.. but i will try to check from my end if I can find any info regarding this..

  • Fred S

    Hi Anish, Great stuff btw… So the errors that are received above are probably because some of the links are postbacks… here's one example of a URL it's trying to verify but gives error: javascript:__doPostBack('ctl00$lnkEmeTop','')
    So, how can we add code to take the postbacks as a part of the URL check?
    thanks much
    Fred

    • Anish10110

      Not sure if the errors are all related to the postback URLs.. I would try to check and see if I get some info about it..

  • Tejas

    Hi Anish i'm also getting the same error The URL does not use a Recognized protocol what is the solution for this?

    • Anish10110

      Is it possible for you to provide the link which you were checking. If you want you can email me the link.

  • Suresh

    Hi, can you guys help me code on how to check if each of the links opens in a new window or not? Greatly appreciate it.

    • Anish10110

      Hi Suresh,

      Each link has a attribute called 'target'. If its value is "_blank" that means it opens in a new page. Try with this and see if you can get the code. Let us know if you face any issues.

  • Sadek

    Hi Anish
    I would like to know how to implement this broken link test code for Firefox user agent.

  • Sourabh

    Hi

    I am getting errors when i try to run this code, My application has Javascript embedded links, its not working for those links and throwing error. Any Solution for this.

  • san

    Hi Anish,

    Thanks for code, great work. I have some links that open on new window, when i run the code, its say broken link 404, but the links work fine. how do i tackle this. i don't understand how to use the atttribute.

    thanks
    Shan

  • soumya

    Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    what is the meaning of it? means winhttprequest.5.1

  • devesh bhatt

    Hi Anish,
    Thanks for code, i have some problem as i am not getting a url which one is call by JavaScript. when i m running this on QTP then i get some error and these are following

    The URL does not use a recognized protocol

    Line (28): "objWinHTTP.Open "GET", sURL,False".

    This method cannot be called until the Open method has been called

    Line (30): "objWinHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"".

    This method cannot be called until the Open method has been called

    Line (33): "objWinHTTP.Send"

    This method cannot be called until the Send method has been called

    Line (34): "iReturnVal = objWinHTTP.Status".

  • crpusany

    Hi Anish,

    What is the line objWinHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)" doing? Do i have to modify it according to Internet Explorer? I changed it to "objWinHTTP.SetRequestHeader "User-Agent", "Internet Explorer 8.0"" is it correct? When i executed the script i got 401 status i.e "HTTP_STATUS_DENIED"

    1) What I need to do check all the links are opening?

    2)If I get HTTP_STATUS_DENIED status that means the page is active? can i consider?

  • Pawan

    This may be because the url string which we pass do not contain http:// before them.

  • Gautam kumar

    hi, I want to clck on a link and after that drop down is there so again i want to click on the link …..how we can do this ….when i spy the object that is link but its behabiour is click …

  • saba ustad

    Hi Anish,

    I am getting operation timed out when i try to run the code for the step Line(33): "objWinHTTP.Send"

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×