1

Please help me with VBScript code to identify the broken links, images on the page through automation (I am using HP UFT tool). When I searched in the internet and found few namespaces like "WinHttp.WinHttpRequest.5.1", MSXML2.ServerXMLHTTP but those are always returning me the status as 200 for every link

Below is the code I am running:

Set LinkDesObj = Description.Create
LinkDesObj("micclass").value="Link"
LinkDesObj("url").value = "https://testCRM.azuresites.com" 
Set LinkCollection = Browser("TestCRM").Page("TestCRM").ChildObjects(LinkDesObj)

For i = 0 To LinkCollection.count-1 Step 1

    If LinkCollection(i).GetROProperty("visible")=True Then
        LinkCollection(i).highlight
        URL = LinkCollection(i).GetROProperty("url")
        status = VerifyTheURL(URL)
    End If

Next


Function VerifyTheURL(URL)

Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objWinHTTP.Option(WinHttpRequestOption_EnableRedirects)=False
objWinHTTP.Open "GET", URL, 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
objWinHTTP.WaitForResponse(10)
iReturnVal = objWinHTTP.Status

'Find out if the Link exists or is broken
If iReturnVal = 200 Then
    msgbox "Link - " & URL & " Exists"
ElseIf iReturnVal = 404 Then
    msgbox "Link - " & URL & " is Broken"
Else
    msgbox "Code" - iReturnVal
End If

Set objWinHTTP = Nothing

End Function

Please let me know that is the above code is correct for a HTTPS URL?

Teamothy
  • 2,000
  • 3
  • 16
  • 26
  • `200` means success. 100s mean it is still continuing. 200s mean full or partial success. 300s are mostly about info. 400s are the web server saying it can't do whatever you asked. 500s are server errors like it crashed.. – CatCat Dec 01 '18 at 20:49

1 Answers1

1

200 means success. 100s mean it is still continuing. 200s mean full or partial success. 300s are mostly about info. 400s are the web server saying it can't do whatever you asked. 500s are server errors like it crashed.

For Windows' error numbers add 12,000 to them. IE 12404 is Inet status 404

Also consider user Head instead of Get

The HEAD Method

HEAD is almost identical to GET, but without the response body.

In other words, if GET /users returns a list of users, then HEAD /users will make the same request but will not return the list of users.

HEAD requests are useful for checking what a GET request will return before actually making a GET request - like before downloading a large file or response body.

This is from WinInet.h

// HTTP Response Status Codes:
//

#define HTTP_STATUS_CONTINUE            100 // OK to continue with request
#define HTTP_STATUS_SWITCH_PROTOCOLS    101 // server has switched protocols in upgrade header

#define HTTP_STATUS_OK                  200 // request completed
#define HTTP_STATUS_CREATED             201 // object created, reason = new URI
#define HTTP_STATUS_ACCEPTED            202 // async completion (TBS)
#define HTTP_STATUS_PARTIAL             203 // partial completion
#define HTTP_STATUS_NO_CONTENT          204 // no info to return
#define HTTP_STATUS_RESET_CONTENT       205 // request completed, but clear form
#define HTTP_STATUS_PARTIAL_CONTENT     206 // partial GET furfilled

#define HTTP_STATUS_AMBIGUOUS           300 // server couldn't decide what to return
#define HTTP_STATUS_MOVED               301 // object permanently moved
#define HTTP_STATUS_REDIRECT            302 // object temporarily moved
#define HTTP_STATUS_REDIRECT_METHOD     303 // redirection w/ new access method
#define HTTP_STATUS_NOT_MODIFIED        304 // if-modified-since was not modified
#define HTTP_STATUS_USE_PROXY           305 // redirection to proxy, location header specifies proxy to use
#define HTTP_STATUS_REDIRECT_KEEP_VERB  307 // HTTP/1.1: keep same verb

#define HTTP_STATUS_BAD_REQUEST         400 // invalid syntax
#define HTTP_STATUS_DENIED              401 // access denied
#define HTTP_STATUS_PAYMENT_REQ         402 // payment required
#define HTTP_STATUS_FORBIDDEN           403 // request forbidden
#define HTTP_STATUS_NOT_FOUND           404 // object not found
#define HTTP_STATUS_BAD_METHOD          405 // method is not allowed
#define HTTP_STATUS_NONE_ACCEPTABLE     406 // no response acceptable to client found
#define HTTP_STATUS_PROXY_AUTH_REQ      407 // proxy authentication required
#define HTTP_STATUS_REQUEST_TIMEOUT     408 // server timed out waiting for request
#define HTTP_STATUS_CONFLICT            409 // user should resubmit with more info
#define HTTP_STATUS_GONE                410 // the resource is no longer available
#define HTTP_STATUS_LENGTH_REQUIRED     411 // the server refused to accept request w/o a length
#define HTTP_STATUS_PRECOND_FAILED      412 // precondition given in request failed
#define HTTP_STATUS_REQUEST_TOO_LARGE   413 // request entity was too large
#define HTTP_STATUS_URI_TOO_LONG        414 // request URI too long
#define HTTP_STATUS_UNSUPPORTED_MEDIA   415 // unsupported media type
#define HTTP_STATUS_RETRY_WITH          449 // retry after doing the appropriate action.

#define HTTP_STATUS_SERVER_ERROR        500 // internal server error
#define HTTP_STATUS_NOT_SUPPORTED       501 // required not supported
#define HTTP_STATUS_BAD_GATEWAY         502 // error response received from gateway
#define HTTP_STATUS_SERVICE_UNAVAIL     503 // temporarily overloaded
#define HTTP_STATUS_GATEWAY_TIMEOUT     504 // timed out waiting for gateway
#define HTTP_STATUS_VERSION_NOT_SUP     505 // HTTP version not supported
CatCat
  • 483
  • 4
  • 5