Ask a Question

Checking for Valid or broken links

SOLVED
salley
Frequent Contributor

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

7 REPLIES 7
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.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
salley
Frequent Contributor

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

tristaanogre
Esteemed Contributor

Hrm... you might want to try and use onreadystatechange.  I'm not sure 'readyState' is refreshed in such a way that the while loop will detect it.

Alternatively, in your "open" call, try setting to synchronous (false) and completely remove the while loop.  This way the "send" won't return until complete and the while-loop becomes unnecessary.

 

Check out https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
salley
Frequent Contributor

no Luck,, we are still trying the readystate method,,,,, 

Untitled.png

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     
}

 


Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”

Extension Available

salley
Frequent Contributor

Thanks a lot, this is great. learned something new. i'm getting some resopose " The URL does not use a recognize protocol" what does that mean.

thanks

 

Below link might help you

https://support.symantec.com/en_US/article.tech106475.html


Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”

Extension Available

cancel
Showing results for 
Search instead for 
Did you mean: