Forum Discussion

salley's avatar
salley
Frequent Contributor
7 years ago

Checking for Valid or broken links

Hello,

I've a  large number of links present in a page, and i'm trying to verify if they're valid or broken. My accessiblity check point doesn't work, Test Complete Freezes not sure why. i've tried the below script that found in here, but it doesn't work not sure why ,,, doesn't get out of the while loop. can you please check the below code. Thanks

Sub Test

Dim page, expectedObjectData, links, linksCount, link, URL, i

URL = "www.smartbear.com"
Browsers.Item(btIExplorer).Run(URL)
Set page = Sys.Browser("*").Page("*")

expectedObjectData = ""

' Obtains the links
Set links = page.contentDocument.links
linksCount = links.length

If linksCount > 0 Then

' Searches for broken links
For i = 0 To UBound(linksCount - 1)

Set link = links.item(i)

URL = link.href
Log.Message("The " & URL & " verification results: " & VerifyWebObject(URL, expectedObjectData))

Next
End If

End Sub


' Checks whether the specified URL is valid
Function VerifyWebObject(link, expectedObjectData)
on error resume Next
Dim httpObj

Set httpObj = Sys.OleObject("MSXML2.XMLHTTP")
Call httpObj.open("GET", link, True)
Call httpObj.send
i=0
While (httpObj.readyState <>4 )
aqutils.Delay(1000)

WEnd
Select Case httpObj.status
Case 200
If (httpObj.responseText <> expectedObjectData) Then
Call Log.Message("The " + link + " link is valid", httpObj.responseText)
VerifyWebObject = False
Exit Function
Else
VerifyWebObject = True
End If

Case 302
If (httpObj.responseText <> expectedObjectData) Then
Call Log.Message("The " + link + " link is valid", httpObj.responseText)
VerifyWebObject = False
Exit Function
Else
VerifyWebObject = True
End If

Case Else
Call Log.Message("The " & link & " link was not found," &" the returned status: " & httpObj.status, httpObj.responseText)
VerifyWebObject = False
End Select
End Function

  • I would use TestComplete in-built option called aqHttpRequest 

    Sample code:

     

    function checkLink(URL)
    {
      var address = URL;
    
      var aqHttpRequest = aqHttp.CreateGetRequest(address);
    
      // Send the request, get an aqHttpResponse object
      var aqHttpResponse = aqHttpRequest.Send(); 
      
      // Read the response data
      Log.Message(aqHttpResponse.AllHeaders); // All headers
      Log.Message(aqHttpResponse.GetHeader("Content-Type")); // A specific header
      Log.Message(aqHttpResponse.StatusCode); // A status code 
      Log.Message(aqHttpResponse.StatusText); // A status text 
      Log.Message(aqHttpResponse.Text); // A response body     
    }

     

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    I think TestComplete interprets "ReadyState" as a string, not as an integer.  Change the while loop to check for "complete" and see if that works.

    • salley's avatar
      salley
      Frequent Contributor

      Thanks for responding,,, yeah i've checked just no luck,,,,,,,,,