Run TestSet using REST API (Lab Management)

If you have configured your ALM to use the Lab Management feature you can create an instantaneous timeslot, that execute the TestSet, through REST API.

 

This example use vbscript as Scripting Language.

 

Let's see the steps you have to perform to do such thing:

  1. Authentication + Session Initialization
  2. Set the request to send via POST
  3. Send the request and save the response

 

We could create a generic engine that recive several parameter such as:

  • ALM SERVER
  • ALM DOMAIN
  • ALM PROJECT
  • ALM USER
  • ALM PWD
  • TESTSET ID

 

The script must be call from the "cscript.exe" engine under c:\windows\System32 [or SysWOW64] and will be something similar to this one:

 

'****************************************************************************
'CONSTANTS to manage Session and Authentication
'****************************************************************************
Const QCSESSION_CK = "QCSession"
Const AUTH_CALL    = "/authentication-point/alm-authenticate"
Const SESSION      = "/rest/site-session"
Const LOGOUT_CALL   = "/authentication-point/logout"
Const TIME_OUT     = 10
Const CLIENT_TYPE  = "REST Client"
'****************************************************************************
'****************************************************************************
'CONSTANTS for Script Exit Code
'****************************************************************************
Const RC_OK     = 0
Const RC_ERR    = 3
'****************************************************************************

'****************************************************************************
'Variable Declarations
'****************************************************************************
Dim StdOut   : set StdOut    = WScript.StdOut    'Standard Output
Dim ScriptExCode : ScriptExCode      = RC_ERR      'Exit Code for the Script

 

'*****************************************************************

'*****************************************************************

'                                      MAIN PROGRAM

'*****************************************************************

'*****************************************************************

'*****************************************************************

'Verify the args number

'*****************************************************************
if wscript.arguments.count <>  6 then
 WriteLine date & " - " & time & ": Error on the number of input arguments: "
 WriteLine date & " - " & time & ": Please Verify your input. expected parameters: "
 WriteLine date & " - " & time & ": ALM SERVER ADDRESS (in the form of http://<ALM_DNS_Address|IP_Address>/qcbin) "
 WriteLine date & " - " & time & ": ALM DOMAIN "
 WriteLine date & " - " & time & ": ALM PROJECT "
 WriteLine date & " - " & time & ": ALM USER "
 WriteLine date & " - " & time & ": ALM PASSWORD "
 WriteLine date & " - " & time & ": TESTSET ID "
 WriteLine date & " - " & time & " "
 WriteLine date & " - " & time & ": Parameter you passed: "
 for i = 0 to Ubound(wscript.arguments)
  WriteLine date & " - " & time & ": Parameter n." & i+1 & " :" & wscript.arguments(i)
 next
    WriteLine date & " - " & time & ""
 WriteLine date & " - " & time & ": Please, retry to submit this program with the expected parameters. Thank you!"  
 set StdOut = Nothing
 wscript.quit ScriptExCode 
end if

'*****************************************************************

'If I'm here it means that the arguments number is OK


Dim ALM_SERVER   : ALM_SERVER  = wscript.arguments(0)
Dim ALM_DOMAIN   : ALM_DOMAIN  = wscript.arguments(1)
Dim ALM_PROJECT  : ALM_PROJECT  = wscript.arguments(2)
Dim ALM_USER   : ALM_USER   = wscript.arguments(3)
Dim ALM_PASSWORD : ALM_PASSWORD  = wscript.arguments(4)
Dim TESTSET_ID   : TESTSET_ID  = wscript.arguments(5)

'*****************************************************************
Dim objSrvHTTP, objXMLDoc       'http and xmldoc object to manage the request and xml document

Dim restDomPrj      : restDomPrj = "/rest/domains/" & ALM_DOMAIN & "/projects/" & ALM_PROJECT
Dim QR_TSET_RUN   = "/test-sets/" & TESTSET_ID & "/startruntestset"
Dim theCookie  : theCookie = ""
Dim AllCookies  : AllCookies = ""


'HTTP object creation to send the request
set objSrvHTTP = CreateObject("Msxml2.ServerXMLHTTP.6.0")

 

'Authentication

Dim bolRes : bolRes = ExecAuthentication("POST", ALM_SERVER & AUTH_CALL, false, ALM_USER, ALM_PASSWORD)

if not(bolRes) then
 WriteLine date & " - " & time & ": Connection Error!!!" 
 set objSrvHTTP = Nothing
 wscript.quit ScriptExCode
end if

 

'Initialize the Session
Dim theSession : theSession = initSession("POST", ALM_SERVER & SESSION, false)

if theSession = "" then
 WriteLine date & " - " & time & ": Error during the session's creation!!!"  
 set objSrvHTTP = Nothing
 wscript.quit ScriptExCode
end if

 

'Perform the creation of the TimeSlot

Dim dataXML   'XML to pass to the request to create the instantaneous timeslot
dataXML = "<Entity><Fields><Field Name=""duration""><Value>30</Value></Field><Field Name=""vudsMode""><Value>false</Value></Field><Field Name=""reservationId""><Value>-1</Value></Field></Fields></Entity>"
objSrvHTTP.open "POST", ALM_SERVER & restDomPrj & QR_TSET_RUN, false
objSrvHTTP.setRequestHeader "Content-Type", "application/xml"
objSrvHTTP.setRequestHeader "Connection", "keep-alive"
objSrvHTTP.setRequestHeader "PtAL", "1"

'Send the request with data
objSrvHTTP.Send dataXML

if objSrvHTTP.Status = 200 Or _
   objSrvHTTP.Status = 201 then
   WriteLine date & " - " & time & ": The Run of the TestSet " & TESTSET_ID & " has been performed!!!"
   set objXMLDoc=CreateObject("Msxml2.DOMDocument")
   objXMLDoc.async = false
   'XML response is saved into Run.xml file
   objXMLDoc.loadXML(objSrvHTTP.responseText) 
   objXMLDoc.Save("Run.xml")
   set objXMLDoc = nothing  
  else 
  WriteLine date & " - " & time & ": The Run of the TestSet " & TESTSET_ID & " has NOT been performed!!! Called Status: " & objSrvHTTP.Status  
end if

 

'Logout

ExecLogout "GET", ALM_SERVER & LOGOUT_CALL, false

set objSrvHTTP = nothing

'*****************************************************************

'*****************************************************************

'                                      END OF MAIN PROGRAM

'*****************************************************************

'*****************************************************************

 

'*****************************************************************

'   FUNCTIONS AND SUBS

'*****************************************************************
Function ExecAuthentication(mothod, urlToCall, AsyncCall, us, pw)
Dim res     'boolean variable that rappresent the result of the call
Dim strHDRS, arrHdrs
Dim DataToSend 
 
 res = false
 'xml structur with user e password
 DataToSend = "<alm-authentication> " & _
                "<user>" & us & "</user>" & _
       "<password>" & pw & "</password>" & _
      "</alm-authentication>"

 'Open execution
 objSrvHTTP.open mothod, urlToCall, AsyncCall
 'contenty type setting to application/xml
 objSrvHTTP.setRequestHeader "Content-Type", "application/xml"     
     
 'send the HTTP request
 objSrvHTTP.send DataToSend

 'verify the status
 if objSrvHTTP.status = 200 then    
  res = true
  AllCookies = objSrvHTTP.getAllResponseHeaders  
  theCookie = readCookie(AllCookies, "Set-Cookie")  
 end if
 
ExecAuthentication = res
End Function

 

Function initSession(mothod, urlToCall, AsyncCall)
Dim res : res = ""

Dim DataToSend

 DataToSend = "<session-parameters> " & _
                "<client-type>" & CLIENT_TYPE & "</client-type>" & _
       "<time-out>" & TIME_OUT & "</time-out>" & _
      "</session-parameters>"

 'Open execution
 objSrvHTTP.open mothod, urlToCall, AsyncCall  
 objSrvHTTP.Send
 
 'verify the status
 if objSrvHTTP.status = 201 then  
  strHDRS = objSrvHTTP.getAllResponseHeaders   
  arrHdrs = split(strHDRS, vbCrLF)
  for i=0 to Ubound(arrHdrs)   
   if instr(arrHdrs(i),"Set-Cookie") > 0 then
    if instr(arrHdrs(i), QCSESSION_CK) then 
     res = right(split(arrHdrs(i), ":")(1), len(split(arrHdrs(i), ":")(1)) - (len(QCSESSION_CK) + 2))  '2 because there is a space after the equal char (= )
    end if    
   end if
  next
  else
  WriteLine date & " - " & time & ": Session's initialization error!!!!"
 end if

initSession = res
End Function

 

Sub ExecLogout(mothod, q, AsyncCall)
objSrvHTTP.open mothod, q, AsyncCall
objSrvHTTP.send
if objSrvHTTP.status = 200 then
 WriteLine date & " - " & time & ": Logout Performed "  
end if
End Sub

 

Sub WriteLine(text)          
    StdOut.WriteLine(text) 'Scrivo l'output
End Sub

 

Function readCookie(listCookies, whichCookie)
Dim strRes, arrList
strRes = ""

arrList = split(listCookies, vbCrLF)

for i=0 to Ubound(arrList)
 if instr(arrList(i), whichCookie) > 0 then
  strRes = arrList(i)
  exit for  
 end if
next

readCookie = strRes
End Function

'*****************************************************************

'     END FUNCTIONS AND SUBS

'*****************************************************************

 

 

Pag: <<   <   >   >>