Tuesday, February 2, 2016

Groovy code and XPATH expressions

XML REQUEST:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.myeclipseide.com">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:add>
         <ws:a>asd</ws:a>
         <ws:b>10</ws:b>
      </ws:add>
   </soapenv:Body>
</soapenv:Envelope>
===================================================================
XML RESPONSE:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.myeclipseide.com">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:addResponse>
         <ws:addReturn>10</ws:addReturn>
      </ws:addResponse>
      <ws:addResponse>
         <ws:addReturn>10</ws:addReturn>
      </ws:addResponse>
      <ws:addResponse>
         <ws:addReturn>test</ws:addReturn>
      </ws:addResponse>
      <ws:addResponse>
         <ws:addReturn>20</ws:addReturn>
      </ws:addResponse>
      <ws:addResponse>
         <ws:addReturn>20</ws:addReturn>
      </ws:addResponse>
      <ws:addResponse>
         <ws:addReturn>20</ws:addReturn>
      </ws:addResponse>
      <ws:addResponse>
         <ws:addReturn>20</ws:addReturn>
      </ws:addResponse>
      <ws:addResponse>
         <ws:addReturn>20</ws:addReturn>
      </ws:addResponse>
   </soapenv:Body>
</soapenv:Envelope>
=========================================================
/* count using Groovy first method
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
def responseHolder = groovyUtils.getXmlHolder("add Request#Response");
responseHolder.namespaces["ws"] ="http://ws.myeclipseide.com"
def nodeCount = responseHolder.getDomNodes("//ws:addResponse/*").length
log.info nodeCount

*/

//Content Assertion for response : (?s).*20.*

/*XPATH EXPRESSIONS

//below code will return the count of particular node.
declare namespace ws='http://ws.myeclipseide.com';
count( //ws:addResponse/ws:addReturn)

//below code copare the actual result and expected result.
declare namespace ws='http://ws.myeclipseide.com';
matches( //ws:addResponse[1]/ws:addReturn[1]/text(), '20' )

//to check if value is digit or not.
declare namespace ws='http://ws.myeclipseide.com';
matches( //ws:addResponse[1]/ws:addReturn[1]/text(), '.\d' )

//to check if value is string or not
declare namespace ws='http://ws.myeclipseide.com';
matches( //ws:addResponse[3]/ws:addReturn[1]/text(), '.\c' )

//below code will check if node exist or not.
declare namespace ws='http://ws.myeclipseide.com';
exists( //ws:addResponse/ws:addReturn)

//below code will return the particular node value.
declare namespace ws='http://ws.myeclipseide.com';
//ws:addResponse[1]/ws:addReturn[1]/text()

*/



def properties = testRunner.testCase.getTestStepByName("Properties");
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ).projectPath
properties.setPropertyValue( "projectPath", groovyUtils)
String projectpath = properties.getPropertyValue("projectPath").trim();
log.info("The current project path of SoapUI Project is :"+projectpath);

import com.eviware.soapui.support.*;
def addRequest = testRunner.testCase.getTestStepByName("add Request").getProperty("Request").getValue();;
def addResponse = testRunner.testCase.getTestStepByName("add Request").getProperty("Response").getValue();;
def node = new XmlSlurper().parseText(addResponse);
String output1 = node.Body.addResponse.addReturn[0].text();

assert output1 != null
assert output1 != "";
log.info("The value of addReturn is :"+output1);

String size = node.Body.addResponse.addReturn.size();
log.info("The count value of addReturn is :"+size);

properties = testRunner.testCase.getTestStepByName("Properties");
String add = properties.getPropertyValue("add");
if (output1 == add)
{
boolean flag = true
log.info("The values are compared against Property value:" + flag);
}

myRequestStep = testRunner.testCase.getTestStepByName('add Request')
String GetrequestData = new String(myRequestStep.testRequest.messageExchange.rawRequestData).toString();
String GetresponseData = new String(myRequestStep.testRequest.messageExchange.rawResponseData).toString();
def today = new Date();
String NewFileName= testRunner.testCase.name+"_"+today.format("yyyyddMM")

def RootFolder = projectpath
def RMDir = new File("$RootFolder") // Path of folder to create

//If the folder doesn't exist, create it
if(RMDir.exists())
{
new File(RootFolder+"/Outputs").mkdir()
}

String filename= projectpath+"/Outputs/"+NewFileName+".txt";
 
FileWriter fw = new FileWriter(filename,true);
fw.write("\r\n"+"*********************RAW REQUEST*********************"+"\r\n");
fw.write("\r\n"+GetrequestData+"\r\n");
fw.write("\r\n"+"*********************RAW RESPONSE*********************"+"\r\n");
fw.write("\r\n"+GetresponseData+"\r\n");
fw.write("\r\n"+"*********************SOAP REQUEST*********************"+"\r\n");
fw.write("\r\n"+addRequest+"\r\n");
fw.write("\r\n"+"*********************SOAP RESPONSE*********************"+"\r\n");
fw.write("\r\n"+addResponse+"\r\n");
fw.close();


/********RANDOM LOGIC***********/
import org.apache.commons.lang.RandomStringUtils

def properties1 = testRunner.testCase.getTestStepByName("Properties");
//to generate random string for element packet no.
String charset = (('A'..'Z') + ('0'..'0')).join()
Integer length = 15
String randomString = RandomStringUtils.random(length, charset.toCharArray())
String packetno  = randomString+".xsl"
properties1.setPropertyValue( "randomString", packetno.trim());
log.info(packetno);