Chuan Chuan Law

DevOps | Software Automation | Continuous Integration

Tag: Webservice

How To Automate Web Services Testing

Testing web services is similar to testing API. Basically, all we need is a framework that:

  1. Calls the web services
  2. Checks the response
  3. Asset the value returned accordingly

The framework we choose is dependent on the framework of the web service itself, for example, if the web service is written in Java, we would want to use Java to test, if its written in .NET, we would use C# to test, etc.

In the example above, we:

  • Create a class extending WebClient

public class ExtendWebClient : WebClient

  •  Create a variable of class ExtendWebClient with overridden timeout

var client=new ExtendedClient(_TIMEOUT)

  • Set the header of the request

client.Headers.Add(HttpRequestHeader.ContentType,”text/xml”)

  • Set the XML data to be sent as string request

string request =”your XML here”

  • Calls the web service by passing the web services URL and XML (string)

 client.UploadString(new Uri(String.Format(“{0}/ADAuthenticate/ADAuthenticate.asmx”, _WEBSERVICEBASEURL)), request);

  • Web client will automatically return a success code (500) and error code otherwise

Code For Web Services Regression Tests

This is to replace my July post with the link to the source code in GitHub which I have remove for security reasons.

However, below is how the code looks like:

#!/usr/bin/ruby

class WebServices
def main_method
f=IO.popen(“curl –request POST –header ‘Content-type: text/xml’ –data @adauthenticate.xml -s -v -D – http://webservices.pageup.com.au/nas/ADAuthenticate/ADAuthenticate.asmx 2>/dev/null>result.xml”).readlines
evalresult(“authenticate”)

f=IO.popen(“curl –request POST –header ‘Content-type: text/xml’ –data @bulkData.xml -s -v -D – http://webservices.pageup.com.au/nas/BulkData/BulkData.asmx?op=GetAvailableImportItemTypes 2>/dev/null>result.xml”).readlines
evalresult(“bulkData”)

f=IO.popen(“curl –request POST –header ‘Content-type: text/xml’ –data @htmlToPdf.xml -s -v -D – http://webservices.pageup.com.au/nas/HTMLToPDF/HTMLToPDF.asmx?op=GetToken 2>/dev/null>result.xml”).readlines
evalresult(“HTMLToPDF”)

f=IO.popen(“curl –request POST –header ‘Content-type: text/xml’ –data @htmlToRtf.xml -s -v -D – http://webservices.pageup.com.au/nas/HTMLToRTF/HTMLToRTF.ASMX?op=URLToRTF 2>/dev/null>result.xml”).readlines
evalresult(“HTMLToRTF”)

f=IO.popen(“curl –request POST –header ‘Content-type: text/xml’ –data @jobSource.xml -s -v -D – http://webservices.pageup.com.au/nas/JobSourceWS/JobSourceWS.asmx?op=GetToken 2>/dev/null>result.xml”).readlines
evalresult(“jobSource”)

f=IO.popen(“curl –request POST –header ‘Content-type: text/xml’ –data @jobSource2.xml -s -v -D – http://webservices.pageup.com.au/nas/JobSourceWS/JobSourceWS.asmx?op=UpdateJobSource  2>/dev/null>result.xml”).readlines
evalresult(“updateJobSource”)

f=IO.popen(“curl –request POST –header ‘Content-type: text/xml’ –data @mediaOrder.xml -s -v -D – http://webservices.pageup.com.au/nas/MEDIAORDERWS/MediaOrderWS.asmx?op=GetToken 2>/dev/null>result.xml”).readlines
evalresult(“MediaOrder”)

f=IO.popen(“curl –request POST –header ‘Content-type: text/xml’ –data @pageUpPeople.xml -s -v -D – http://webservices.pageup.com.au/nas/PageUpPeopleWS//PageUpPeople.asmx?op=GetApplicant 2>/dev/null>result.xml”).readlines
evalresult(“PageUpPeople”)

f=IO.popen(“curl –request POST –header ‘Content-type: text/xml’ –data @pdfReportGenerator.xml -s -v -D – http://webservices.pageup.com.au/nas/PDFReportGenerator/PDFReportGenerator.asmx?op=Generate360ReportWS 2>/dev/null>result.xml”).readlines
evalresult(“PDFReportGenerator”)

f=IO.popen(“curl –request POST –header ‘Content-type: text/xml’ –data @pdfToHtml.xml -s -v -D – http://webservices.pageup.com.au/nas/PDFtoHTML/PDFtoHTML.asmx?op=PDFToHTML 2>/dev/null>result.xml”).readlines
evalresult(“PDFToHTML”)

f=IO.popen(“curl http://webservices.pageuppeople.com/nas/JobsRest/JobListing.svc/218/caw/en  2>/dev/null>result.xml”).readlines
evalresult(“JobRest”)

end

def evalresult(webserviceType)
f=File.new(“/Users/chuan/webservicesTest/result.xml”)
text = f.read

if (webserviceType==”authenticate”) then
print “Authenticate: ”
if (text =~/HTTP/1.1 200 OK/ && text =~/Success/) then
print  “Successn”
else
print  “Failn”
end
end

if (webserviceType==”bulkData”) then
print “Bulk Data: ”
if (text =~/HTTP/1.1 200 OK/ && text =~/OrgUnit/) then
print  “Successn”
else
print  “Failn”
end
end

if (webserviceType==”HTMLToPDF”) then
print “HTML To PDF: ”
if (text =~/HTTP/1.1 200 OK/) then
print  “Successn”
else
print  “Failn”
end
end

if (webserviceType==”jobSource”) then
print “Job Source: ”
if (text =~/HTTP/1.1 200 OK/) then
print  “Successn”
token=text.scan(/(.*)</GetTokenResult>/)
token_string=token.to_s
token_string.gsub!(/[^a-zA-Z0-9-]/,””)
File.open(‘jobSource2.xml’, ‘w’) do |f1|
f1.puts ”
f1.puts ‘  ‘
f1.puts ‘    ‘
f1.puts ”      #{token_string}”
f1.puts ‘     test’
f1.puts ‘
‘                    f1.puts   ‘
‘                    f1.puts ‘
‘                     end
else
print  “Failn”
end
end

if (webserviceType==”updateJobSource”) then
print “Job Source – Update Job Source: ”
if (text =~/HTTP/1.1 200 OK/) then
print  “Successn”
else
print  “Failn”
end
end

if (webserviceType==”MediaOrder”) then
print “Media Order: ”
if (text =~/HTTP/1.1 200 OK/) then
print  “Successn”
else
print  “Failn”
end
end

if (webserviceType==”PageUpPeople”) then
print “PageUpPeople: ”
if (text =~/HTTP/1.1 200 OK/ && text =~/tester145127@pup.com/) then
print  “Successn”
else
print  “Failn”
end
end

if (webserviceType==”PDFReportGenerator”) then
print “PDFReportGenerator: ”
if (text =~/HTTP/1.1 200 OK/) then
print  “Successn”
else
print  “Failn”
end
end

if (webserviceType==”PDFToHTML”) then
print “PDFToHTML: ”
if (text =~/HTTP/1.1 200 OK/) then
print  “Successn”
else
print  “Failn”
end
end

if (webserviceType==”JobRest”) then
print “JobRest: ”
if (text =~/Account management/) then
print  “Successn”
else
print  “Failn”
end
end

end

end

WebServices.new.main_method

Web Services Regression Test Suite

To follow the previous blog, I have completed the regression test suite for web services.

The code has been checked into GitHub at Git Hub

Regression Testing Web Services

The mechanism I use to regression test web services is the same as the mechanism I test API, which is by using:

  • Curl
  • Ruby
Curl is main tool to call the web services and it sits inside the Ruby script which is used to do other functions such as checking the output, etc.
For example, the Curl command below will pass the XML file to the JobSource web service and pipe the output to another XML file:

 f=IO.popen(“curl –request POST –header ‘Content-type: text/xml’ –data @jobSource2.xml -s -v -D – http://webservices.pageup.com.au/JobSourceWS/JobSourceWS.asmx?op=UpdateJobSource  2>/dev/null>result.xml”).readlines

© 2019 Chuan Chuan Law

Theme by Anders NorenUp ↑