Monday, 2 November 2015

REST API Webservices Testing

Steps for testing webservices (REST API):

  • Copy below given code and paste it in your action.
  • Give path to your TestPlan.xlsx, SampleTemplate.txt and ResponseXML.xml

Set oExcel=createobject("excel.application")
Set oExcelWorkbook=oExcel.Workbooks.Open("Path for Excel File- Data Sheet - Test Plan")
Set OExcelSheet=oExcelWorkbook.Worksheets("DataSheet")
oExcelCount=OExcelSheet.usedrange.rows.count

For i=3 to oExcelCount
  FromCity= trim(OExcelSheet.cells(i,3))
  FromState= trim(OExcelSheet.cells(i,4))
  FromCountry= trim(OExcelSheet.cells(i,5))
  
  ToCity= trim(OExcelSheet.cells(i,6))
  ToState= trim(OExcelSheet.cells(i,7))
  ToCountry= trim(OExcelSheet.cells(i,8))
  
  strFileText=templateLoad()
  
  strFileText=Replace(strFileText,"citys",FromCity)
  strFileText=Replace(strFileText,"states",FromState)
  strFileText=Replace(strFileText,"countrys",FromCountry)
  
  strFileText=Replace(strFileText,"cityd",ToCity)
  strFileText=Replace(strFileText,"stated",ToState)
  strFileText=Replace(strFileText,"countryd",ToCountry)
  
  url=strFileText
  responseText=getResponse(url)
  
  writeXML(responseText)
  
  Set getresult=getvalueXML()

  OExcelSheet.cells(i,11)=getresult(0)
  OExcelSheet.cells(i,12)=getresult(1)
  
  expectedDuration=trim(OExcelSheet.cells(i,9))
  expectedDistance=trim(OExcelSheet.cells(i,10))
  
  print expectedDuration
  print expectedDistance
  print getresult(0)
  print getresult(1)
  
  If (strcomp(trim(expectedDuration),trim(getresult(0)))<>0 )  Then
   OExcelSheet.cells(i,13)="Fail"
  Else
   If (strcomp(trim(expectedDistance),trim(getresult(1)))<>0) Then
    OExcelSheet.cells(i,13)="Fail"
   Else
    OExcelSheet.cells(i,13)="Pass"
   End If
   
  End If
  
Next

oExcelWorkbook.save
oExcelWorkbook.Close
oExcel.quit

'##########################################
'#########COMMON FUNCTIONS#################
'##########################################

Function writeXML(responseText)

 Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("Path for Response XML",2,true)
 objFileToWrite.WriteLine(responseText)
 objFileToWrite.Close
 Set objFileToWrite = Nothing

End Function

Function templateLoad()
 
 Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("Path for Sample Template-URL",1)
    strFileText = objFileToRead.ReadAll()
    objFileToRead.Close
    Set objFileToRead = Nothing
    templateLoad=strFileText
 
End Function

Function getResponse(url)
 
 Set oReq = CreateObject("Microsoft.XMLHTTP")
   oReq.open "POST", url, False
  oReq.send
   'Response
   responseText = oReq.responseText
   getResponse=responseText
 
End Function

Function getvalueXML()
 
 Set xmlDoc = CreateObject("Microsoft.XMLDOM")
   xmlDoc.Async = False
   xmlDoc.Load("Path for Response XML")
  
   Set nodes = xmlDoc.SelectNodes("/DistanceMatrixResponse/row/element")
  
   Set duration = xmlDoc.SelectNodes("/DistanceMatrixResponse/row/element/duration/text/text()")
   Set distance = xmlDoc.SelectNodes("/DistanceMatrixResponse/row/element/distance/text/text()")
 
   Set userArrayList = CreateObject( "System.Collections.ArrayList" )
   userArrayList.add duration(0).NodeValue
   userArrayList.add distance(0).NodeValue
   Set getvalueXML=userArrayList
 
End Function

exittest


Screenshot of Sample Template-URL:



Screenshot of Response XML:



Screenshot of Excel File- Data Sheet - Test Plan:


No comments:

Post a Comment