Monday, September 21, 2020

Automating Web Service Call in SOAP UI using Groovy

While working on Implementation Projects, many a time we need to call a web service multiple times for same purpose using web services testing tools like SOAP UI.

Let us take example of BI cloud connector(BICC). We configure BICC to upload the extracts to UCM and from UCM a third party application will fetch these files. Sometimes when there is any issue we need to delete these files. We will have lot of extracts in BICC and hence the count of files sometimes will be 300+.

Below are some of the possible options to delete these files

1). Go to content server and delete the files one by one

2). Call generic SOAP service to delete the files based on DOC ID one by one.

3). Create Test Suite in SOAP UI and use Groovy to call the  Point 2 multiple times

When can also take example of Customer data conversion where customer profile was not created and if count of customer accounts are huge then we can use the Groovy script to call the customer profile creation Web service multiple times.

Let us understand in detail how we can achieve the automation of Web service call in SOAP UI using Groovy(Example: Deleting files from UCM).

  • We will create a simple data model and report that will fetch us the Doc ID's from UCM which needs to be deleted using the Fusion UCM tables mentioned in this link.
  • Export the output in file eg. csv
  • In SOAP UI create a SOAP Project->Test suite->test case->Test Steps-> (a) Properties  (b). Groovy Script  (c). SOAP Request
  • Run the test case/suite or Run Groovy script step

Follow the below steps and Screenshots to call the service for file deletion from UCM multiple times.

  • Create a data model and report to fetch the Doc Id's of the documents which needs to be deleted.





  • View the data and save as sample data. Save the Data model.



  • Now Create a report to get all the doc Id's( The data model has limit to display only 200 records and also we cannot get csv output from data model, it exports to only XML).

  • In the next window, click on cancel. We will use the generate RTF option, which is the easies one.


  • Select the data model by clicking on search icon.





  • Now click on "Generate RTF layout based on selected Data Model." Provide template name and click on generate. RTF template will be created automatically.



  • Now we need to set the default output to Data(CSV), so click on "View a list" and Add the "Data(CSV) by checking the checkbox  in output formats and change the default output from HTM to Data(CSV).





  • Save the report.





  • Click on "View Report". The output will be generated, export the output to CSV.







  • Now we have the csv file with the list of Doc Id's from UCM which needs to be deleted.
  • We will now see how we can use this file in Soap Ui to call the webservice multiple times


  • Open SOAP UI
  • Create a new SOAP Project, provide the necessary details and click on ok.






  • Now Create a Test suite, under this project. For this right click on the Project and then click on New TestSuite as shown in below screenshot.



  • Now we will create "Test Case" under the Test Suite. Right click on "delete_ucm_doc" test suite and then click on "New TestCase".




  • Till now we have created a SOAP Project, Test Suite and Test Case.
  • Now we need to add the test steps to the test case.
  • We will have 3 test steps. Add these steps one by one.
                                1). Properties- To define the variable, which will get updated from file.
                                2). Groovy Script - The automation Script.
                                3). SOAP request - Request to delete the file, it will be called multiple times( in
                                                                 groovy as many Doc Id's we have in file)






Give properties step name as Properties. Add a variable and give name as "did"(this will be used in Groovy) and keep value as blank.





Add the groovy script step. Write a groovy script to read values from file, update the properties value and automate the webservice call. The file name as per the script should be "docid.csv". You can have any name but it should be in sync with the script(directory and file name should be in Sync with code).










Add SOAP request test step. It is a soap request to delete a file from UCM.
Provide Authentication for the SOAP request. Update the payload, add value for dID  as ${did} this will be dynamically updated with the properties value.



  • Now we are done with the setups. We can either run the test case or we can directly  run the groovy script to call the SOAP request multiple times as the SOAP request is getting called inside for loop with limit as number of doc id's in the file.

OR





  • If we run the test case we would get the below screen on successful completion.

  • If we run Groovy script then we would get below.



  • Below is the screenshot of one of the soap request-response call ran as part of automation to delete the file from UCM.



  • Below is the Groovy script that we have written to automate the web service call. This can be re-used by modifying as per the requirement.


log.info "Start The Program"
def testDataSet = []
def failureList = []
def fileName = "C:\\Users\\Sumit.Kumar17\\Desktop\\docid.csv"

//open the file, iterate trgough each line and split based on comma add add to testDataSet
new File(fileName).eachLine {
  line ->testDataSet.add(line.split(","))
}

//Print number of lines to read from file name with path
log.info("Read: " + testDataSet.size() + " test values from: " + fileName)

//if iteration fails add entry to failurelist
context.setProperty("failurelistist", failureList)

//Store the data in testDataSet
context.setProperty("testDataSet", testDataSet)

//set the index to read lines in file, it starts from 0
//but since in our csv file we have header line so we will start with 1
context.setProperty("index", 1)

//Track which line of the input file we are curently on
def index = context.getProperty("index")

//Print the index
log.info(index)

//select the current test data line, to print exact value we need to do toString() 
def testDataLine = testDataSet[index]

//print current line
log.info(testDataLine.toString())

//define project var
def prj = testRunner.testCase.testSuite.project.workspace.getProjectByName("Generic Soap Service")

//Print Project
log.info prj

//Define Test case var, provide test suite and test case name
tCase = prj.testSuites["delete_ucm_doc"].testCases["delete_case"]

//Print Testcase
log.info tCase

//Define tolal lines in the file vas
def w = testDataSet.size()

//print total lines
log.info w

for (int i = 1; i < w; i++) {
  log.info("Param " + i + "  " + testDataSet[i][0].toString())
  log.info("=================================================================")
  def props = tCase.getTestStepByName("Properties")

  props.setPropertyValue("did", testDataSet[i][0].toString())

  log.info("Document ID: " + props.getPropertyValue("did"))
  log.info("=================================================")

  tCase.testSteps["SOAPRequest"].setPropertyValue("did", props.getPropertyValue("did"))
  def request = tCase.getTestStepByName("SOAPRequest")

  //Run Request
  def runner = request.run(testRunner, context)
  log.info request

}




  • Similarly we can automate Rest Api calls as well.

No comments:

Post a Comment