4.10.17

Groovy Script for reading json file and post to REST API Request in SOAPUI

//Import groovy JSON
import groovy.json.*



//Read the JOSN File and parse the json arrays
def fileContent = new File('C:\\Office\\records_JSON.json').text
def json = new JsonSlurper().parseText(fileContent)


//opening new file for storing the REST API Response.
File file = new File("C:\\Office\\Response.txt")


//Count the number of records
def lt = json.size()

//Preparing the list of arrays
//def forgivnessMap = [:]
def forgivnessDataMap = [:]



//Loop to POST Forgivness request for number of records
for (i = 0; i <lt; i++)
{

//log.info(json[i])
//log.info(i)
//Mapping the array values to the vaiables
def amt = json[i].amount
def div = json[i].division


//Mapping varaibles to the LIST MAP
forgivnessDataMap.put("amount",amt.toInteger())



//Eliminate the mapping of divisionnumber if its is NULL
if(div != null)
{
forgivnessDataMap.put("div",downPaymentDocuNo)
}

//Preapring the JSON Payload for forgivness
def ForgivnessPayload = new JsonBuilder(forgivnessDataMap).toPrettyString()

log.info("forgivnessBody ="+ForgivnessPayload)

//Calling the POST Forgivness API for each record in the JSON File.
//def POSTForgivness = testRunner.testCase.getTestStepByName("postforgivness").getTestRequest()
//POSTForgivness.setRequestContent(ForgivnessPayload)

//Get the next step
def nextStep = context.testCase.getTestStepByName("postforgivness")
//Set the new request
//nextStep.getT‌​estRequest().setRequestContent(ForgivnessPayload)
nextStep.httpRequest.requestContent = ForgivnessPayload
//run next step
nextStep.run(testRunner, context)


//Reading the response after the POST Forgivness request
def ForgivnessResponse = testRunner.testCase.testSteps["postforgivness"].testRequest.response.contentAsString
def ForgivnessResponseJson = new JsonSlurper().parseText(ForgivnessResponse)

//saving the ForgivnessResponse into textfile.
file.append(ForgivnessResponseJson)
log.info(ForgivnessResponseJson)

}

12.4.17

My pom.xml file for Selenium TestNG Maven Project for referance

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>GHP.com</groupId>
  <artifactId>ghp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>LH</name>
  <description>Selenium Automation with TestNG &amp; Maven</description>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-api</artifactId>
      <version>2.0rc2</version>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-support</artifactId>
      <version>3.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-firefox-driver</artifactId>
      <version>3.3.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-chrome-driver</artifactId>
      <version>3.3.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-ie-driver</artifactId>
      <version>3.3.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>net.sourceforge.htmlunit</groupId>
      <artifactId>htmlunit</artifactId>
      <version>2.25</version>
    </dependency>
       
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.11</version>
        <scope>test</scope>
    </dependency>
   
    <dependency>
      <groupId>org.littleshoot</groupId>
      <artifactId>littleproxy</artifactId>
      <version>0.5.3</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>2.7.6</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>net.bytebuddy</groupId>
      <artifactId>byte-buddy</artifactId>
      <version>1.6.8</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
      <version>2.25.1</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-servlet</artifactId>
      <version>9.4.2.v20170220</version>
    </dependency>
     <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-maven-plugin</artifactId>
      <version>9.4.2.v20170220</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
</project>