5.4.14

Invoke a Web Service via VbScript using the WinHTTPRequest Object


As part of our functional tests, there might be a need to invoke a web service directly. We can do that by using the Web Service Add-in of the QTP, or by a simple VbScript code [which would be much faster] as shown below.

Sample Code:

'''----------------------------------------------------------------------------------------------------------------------
'The URL of the site or of a webservice which we need to access
sURL = "http://intranetportal.anycompany.com/"


'Create the WinHTTPRequest COM Object
Set oWinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

'Create an HTTP request
oWinHttpReq.Open "POST", sURL, False

'Send the HTTP request
oWinHttpReq.Send()

'Wait 4 sec for the Response to come
oWinHttpReq.WaitForResponse(4)

'Retrieve the Status
MsgBox oWinHttpReq.StatusText

'Retrieve the response text
MsgBox oWinHttpReq.ResponseText

'''----------------------------------------------------------------------------------------------------------------------
''These can be added later if needed
''sContentType = "text/XML"
''oWinHttpReq.setRequestHeader "Content-Type", sContentType
''oWinHttpReq.setRequestHeader "Accept", sContentType
''oWinHttpReq.setRequestHeader "Connection", "keep-alive"
''oWinHttpReq.SetTimeouts 5,5,5,5
''oWinHttpReq.Option WinHttpRequestOption_URL
'''----------------------------------------------------------------------------------------------------------------------




Notes:
  • HTTP Verb: The HTTP verb (or HTTP method) is an instruction sent in a request message that notifies an HTTP server of the action to perform on the specified resource. For example, "GET" specifies that a resource is being retrieved from the server. Common verbs include "GET", "POST", and "HEAD". For more information and a complete list of standard HTTP verbs, see the HTTP/1.1 specification.
  • The Open method does not establish a connection to the resource as the name might imply. Rather, it initializes the internal data structures that maintain information about the session, connection, and request. The HTTP verb "GET" obtains data from the URL. False implies that the transaction occurs synchronously
  • The Send method assembles the request headers and sends the request. When called in synchronous mode, the Send method also waits for a response before allowing the application to continue. Sometimes the operations may get timed out but its not because of some error in the code but because it may be blocked by the company firewall. To overcome this, try using the URL of an Internal Site

Connect to ALM-QC via VbS or QTP


You can use the following code to connect to ALM\QC via pure VbScript, without using QTP, even though this code would work from QTP as well.

Prerequisite:
  • ALM\QC should be installed

Code -

'''----------------------------------------------------------------------------------------------------------------------
'' Function Name:            funcGetALMConnectionObj
'' Description:                   This will connect to the ALM\QC server via either pure VbS or QTP
'' Input Parameters:        sQCServer
''                                         sQCUsername
''                                         sQCPassword
''                                         sQCDomain
''                                         sQCProject
'' Output Parameters:     oALMConnObj [The ALM Connection Object]
'' Author:                          Ashish Jaiswal
'''----------------------------------------------------------------------------------------------------------------------
Function funcGetALMConnectionObj (sQCServer, sQCUsername, sQCPassword, sQCDomain, sQCProject)

    ''Defining the Parent QC OTA Object
    Set oALMConnObj = CreateObject("TDAPIOLE80.TDConnection")

    ''Initiating the connection to the QC Server
    oALMConnObj.InitConnectionEx sQCServer

    ''Logging in
    oALMConnObj.Login sQCUsername, sQCPassword

    ''Connecting to the required project and domain
    oALMConnObj.Connect sQCDomain, sQCProject

    ''Returning the object
    Set funcGetALMConnectionObj = oALMConnObj

End Function
'''----------------------------------------------------------------------------------------------------------------------

Usage:

''Defining the connection parameters for QC
sQCServer = "http://ealm11.anyorg.net/qcbin/"
sQCUsername = "username"
sQCPassword = "password"
sQCDomain = "domain"
sQCProject = "projectname"

Set oALMConnObj = funcGetALMConnectionObj (sQCServer, sQCUsername, sQCPassword, sQCDomain, sQCProject)

''Proceeding ahead only if connected
If oALMConnObj.Connected Then

    MsgBox "Connected to QC!"
    MsgBox oALMConnObj.ProjectName

Else

    MsgBox "Not connected to QC"

End If
'''----------------------------------------------------------------------------------------------------------------------

Common troubleshooting measures -

  • Register the OTAClient.dl
    • For this put the OTAClient.dll in the "C:\Windows\System32" drive, if its not already there
    • Run the following command to [re]register the dll from the Run window
                      RegSvr32 "C:\Windows\System32\OTAClient.dll"

  • If you are getting an error something like 'ActiveX can't create object - TDConnection' then try running the connection code by putting in a .vbs file, from the Run window as follows, via the WScript.exe -
           C:\Windows\SysWOW64\wscript.exe "C:\Automation\QC-Connection.vbs"