Showing posts with label Learn SoapUI. Show all posts
Showing posts with label Learn SoapUI. Show all posts

Saturday, April 11, 2015

How to Read and Encode an Image file using Groovy in SoapUI

Make sure all the Groovy GDK ars are present in SoapUI/Lib folder

def s = 'Any tag value which has Base64 encoded image'
String encoded = s.bytes.encodeBase64().toString()
assert 'QXJnaCwgR3Jvb3Z5IHlvdSBzYXksIG1hdGU/' == encoded
byte[] decoded = encoded.decodeBase64()
assert s == new String(decoded)

Sunday, February 15, 2015

Execute Stored procedure in SoapUI using Groovy Script

try
{
   sql.call("call stored_proc.name('111', '222', 'xxx');")
}catch (Exception ex)
{
   log.info ex.message
}catch (Error er)
{
   log.info er.message
}

Run test step depending on status of a different test step in SoapUI using Groovy Script

testRunner.testCase.testSuite.project.name
import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus
import com.eviware.soapui.impl.wsdl.WsdlTestSuite
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep


myTestStepResult = testRunner.testCase.testSuite.getTestCaseByName("testCase_1").getTestStepByName("testStep_2").run(testRunner, context)
myStatus = myTestStepResult.getStatus()
if (myStatus == "OK")
testRunner.gotoStepByName("testStep_3")
else{
def tCase = testRunner.testCase.testSuite.testCases["Name of test cases"]
def tStep = tCase .testSteps["test step you want to run"]
tStep.run(testRunner, context)
}

List Names of Test cases in Test Suite in SoapUI using Groovy Script

import com.eviware.soapui.impl.wsdl.teststeps.*
def i=0;
for( testCase in testRunner.testCase.testSuite.getTestCaseList() ) {
    for( testStep in testCase.getTestStepList() ) {
        if( testStep instanceof WsdlTestRequestStep ) {
            log.info i++ +"Name of testcases in this suite: " +"[" +testCase.getLabel() +"]" +" Name of the WSDL step is " +testStep.getName();
        }
    }
}

Tuesday, November 25, 2014

How to execute SoapUI Project from Command Line

SoapUI currently comes with the following command line tools for running TestCases and LoadTests

loadtestrunner.bat => To run Load test
mockservicerunner.bat => to run mock services
securitytestrunner.bat => to run security test
testrunner.bat => Used to run your soapui testcases
toolruner.bat => Runs any of the configured Code Generation tools for specified project and interface
wargenerator.bat => used to generate war files

Syntax to run command line tools is

<command line tool> <parameters> <report(optional)> <project>
eg
testrunner.bat     -FPDF     -R"Project Report"     c:\projects\my-soapui-project.xml

Another example
Here we will pass the property which will be  used in run the test

1)testrunner.bat -P username=ss123 Cc:\projects\my-soapui-project.xml
-P => Sets project property with name=value which can be used to run testsuites

2)testrunner.bat -r c:\projects\my-soapui-project.xml
-r => prints a small report in console

SoapUI 5.0.0 TestCaseRunner Summary
-----------------------------
Time Taken: 5301ms
Total TestSuites: 2
Total TestCases: 2 (1 failed)
Total TestSteps: 3
Total Request Assertions: 2
Total Failed Assertions: 0
Total Exported Results: 1

3)testrunner.bat -j c:\projects\my-soapui-project.xml
-j will create Junit style reports, the xml generated will be in the bin folder of SoapUI

4)testrunner.bat -e www.google.com  c:\projects\my-soapui-project.xml
-e => set end point url, overrides the endpoint set in the project file

5)testrunner.bat -h www.google.com:9001  c:\projects\my-soapui-project.xml
 -h => overrides only the host part of the endpoint set in the project file

Sunday, November 23, 2014

getNodeValues in XmlHolder for SoapUI

def request = '''<xml>
   <node>
      <val1>pqr</val1>
      <val2>abc</val2>
   </node>
   <node>
      <val1>xyz</val1>
      <val2>lmn</val2>
   </node>
</xml>'''


def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = new com.eviware.soapui.support.XmlHolder( request )


// loop item nodes in response message

//get All occurance of node/val1
log.info "--------First For loop------------"
for( item in holder.getNodeValues( "//node/val1" ))
log.info "Node : [$item]"

OUTPUT

Sun Nov 23 19:51:36 IST 2014:INFO:--------First For loop------------
Sun Nov 23 19:51:36 IST 2014:INFO:Node : [pqr]
Sun Nov 23 19:51:36 IST 2014:INFO:Node : [xyz]
Sun Nov 23 19:51:36 IST 2014:INFO:--------Second For loop------------
Sun Nov 23 19:51:36 IST 2014:INFO:Node : [pqr]

How to removenodes from Request/Response for assertion

import com.eviware.soapui.support.*
def soapmessage = '''<ns:basket xmlns:ns="http://example.com">
                    <ns:fruit>
                    <ns:name>banana</ns:name>
                          <ns:color>yellow</ns:color>
                         </ns:fruit>
                       <ns:fruit>
                          <ns:name>apple</ns:name>
                          <ns:color>green</ns:color>
                         </ns:fruit>
                       <ns:fruit>
                         <ns:name>strawberry</ns:name>
                          <ns:color>red</ns:color>
                         </ns:fruit>
                    </ns:basket>'''


def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def holder = new com.eviware.soapui.support.XmlHolder( soapmessage )
  //holder.namespaces["ns"] = "http://example.com"
  //holder.declareNamespace("ns", "http://example.com")

assert holder.getNodeValue("//ns:fruit[2]/ns:color[1]") == "green"

holder.removeDomNodes("//*:fruit[2]")

assert holder.getNodeValue("//ns:fruit[2]/ns:color[1]") == "red"
log.info "Done"

How to access property in Script Assertion in SoapUI using Groovy Script

//Set Property
messageExchange.modelItem.testStep.testCase.setPropertyValue("MyProp", someValue)

//Get Property
def testCaseProperty = messageExchange.modelItem.testStep.testCase.getPropertyValue("MyProp")

Setting and Getting properties at all levels in SoapUI with Groovy Script

Getting Properties…

-From a Groovy Script test step
def testCaseProperty = testRunner.testCase.getPropertyValue("MyProp")
def testSuiteProperty = testRunner.testCase.testSuite.getPropertyValue("MyProp")
def projectProperty = testRunner.testCase.testSuite.project.getPropertyValue("MyProp")
def globalProperty = com.eviware.soapui.SoapUI.globalProperties.getPropertyValue("MyProp")

Setting Properties…

-From a Groovy Script test step:
testRunner.testCase.setPropertyValue("MyProp", someValue)
testRunner.testCase.testSuite.setPropertyValue("MyProp", someValue)
testRunner.testCase.testSuite.project.setPropertyValue("MyProp", someValue)
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue("MyProp", someValue)

How to save all Test Case properties in a Map using Groovy Script

def tcProps = testRunner.testCase.getProperties()
def map = [:]

//grab all the properties from the test case and put them in a map as long as they aren't empty
for(p in tcProps){    
            if (p.value.getValue() != "") {
            map.put(p.key, p.value.getValue())            
    } 
}

XML Parser in SoapUI

def studentRecords = ''' 
        <students> 
              <student name='sname' id='sid' college='collegename' /> 
        </students> 
    ''' 
def records = new XmlParser().parseText(studentRecords) 
def requiredMap = records.student[0].attributes()


log.info "entrySet() shows name value pairs ==> "+requiredMap.entrySet()
// An EntrySet is just a thin wrapper providing a view of objects that already exist inside the HashMap
log.info "keySet() shows only keys ==> "+requiredMap.keySet()
log.info "containsKey, check if id tag is present ==> "+requiredMap.containsKey("id")
log.info "conatinsValue, check if id tag VALUE is present ==> "+requiredMap.containsValue("sid")
log.info "get value of the key id ==> "+requiredMap.get("id")
log.info "check if map is empty ==> "+requiredMap.isEmpty()
log.info "add value to map ==> "+requiredMap.put("sagar" , "singh")
log.info "entrySet() values are ==> "+requiredMap.entrySet()
//log.info "remove from map ==> "+requiredMap.remove("sagar")
//log.info "entrySet() values are ==> "+requiredMap.entrySet()

OUTPUT

Sun Nov 23 19:11:59 IST 2014:INFO:entrySet() shows name value pairs ==> [name=sname, id=sid, college=collegename]
Sun Nov 23 19:11:59 IST 2014:INFO:keySet() shows only keys ==> [name, id, college]
Sun Nov 23 19:11:59 IST 2014:INFO:containsKey, check if id tag is present ==> true
Sun Nov 23 19:11:59 IST 2014:INFO:conatinsValue, check if id tag VALUE is present ==> true
Sun Nov 23 19:11:59 IST 2014:INFO:get value of the key id ==> sid
Sun Nov 23 19:11:59 IST 2014:INFO:check if map is empty ==> false
Sun Nov 23 19:11:59 IST 2014:INFO:add value to map ==> null
Sun Nov 23 19:11:59 IST 2014:INFO:entrySet() values are ==> [name=sname, id=sid, college=collegename, sagar=singh]
Sun Nov 23 19:11:59 IST 2014:INFO:remove from map ==> singh
Sun Nov 23 19:11:59 IST 2014:INFO:entrySet() values are ==> [name=sname, id=sid, college=collegename]

How to transfer property proerty to all Test Cases in Test Suite an access it

Transfer property to all TC

def ABC = testRunner.testCase.testSuite.project.getTestSuiteList()
def DEF = testRunner.testCase.testSuite.getPropertyValue("w2_SessionID")

for (testSuite in ABC){
for (testCase in testSuite.getTestCaseList()){
testCase.setPropertyValue("w2_SessionID", DEF)
}
}


Access property in each Test Case

def abdProjProp = context.expand( '${#Project#abdProjProp}' )

How to run a TestCase located in another project

def prj = testRunner.testCase.testSuite.project.workspace.getProjectByName("ProjectName")

2.tCase = prj.testSuites['TestSuiteName'].testCases['TestCaseName']

3.tStep = tCase.getTestStepByName("TestStepName")

4.def runner = tStep.run(testRunner, context)

How to transfer properties between projects

//get test case from other project
project = testRunner.getTestCase().getTestSuite().getProject().getWorkspace().getProjectByName(project_name)
testSuite = project.getTestSuiteByName(suite_name);
testCase = testSuite.getTestCaseByName(testcase_name);

//set properties
testRunner.testCase.setPropertyValue(property_name, property_value);
testRunner.testCase.setPropertyValue(another_property_name, another_property_value);

// run test case
runner = testCase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false);

Groovy Script to transfer Property in SoapUI

// get request property
def request = testRunner.testCase.getTestStepByName( "teststepname" )
def property = request.getProperty( "request" )

// set request property
request = testRunner.testCase.getTestStepByName( "newteststep" )
request.getProperty("request").setValue(property.getValue())

What are the common assertions used in API Testing

1)Check response status
2)Assert values saved in DB
3)Compare values between request xml and data saved in DB
4)Time taken for response
5)Check xpaths
6)Check the expected tag value
7)Assert response xml with wsdl

And lots more..

Saturday, November 22, 2014

How to Disable teststep within a test case in SoapUI with groovy script

def totalTestCases = testSuite.getTestCaseCount();
for(n in (0..totalTestCases-1)) {
    if (testSuite.getTestCaseAt(n).getTestStepByName("MyTestStep")){
        testSuite.getTestCaseAt(n).getTestStepByName("MyTestStep").setDisabled(true)
    }
}

Print Testcase name and Teststep names in SoapUI with Groovy Script

import com.eviware.soapui.impl.wsdl.teststeps.*
def i=0;
for( testCase in testRunner.testCase.testSuite.getTestCaseList() ) {
    for( testStep in testCase.getTestStepList() ) {
        if( testStep instanceof WsdlTestRequestStep ) {
        log.info i++ +"Name of testcases in this suite: " +"[" +testCase.getLabel() +"]" +" Name of the
        WSDL step is " +testStep.getName();
        }
    }
}

Constructors For XmlHolder

XmlHolder is the class provided by SoapUI to parse the Xml


1)def holder = XmlHolder(Node node)
Here were are passing the node directly

eg : def holder = XmlHolder("//parent/child")

2)def holder = XmlHolder(String Xml)
Here we are directly passing the xml itself

eg : def holder = XmlHolder("<body><name>xyz</name><phone>1234567890</phone></body>")

3)def holder = XmlHolder(org.apache.xmlbeans.XmlObject xmlObject)
here we pass an obect that has xml in it

How to access Nodes Using XmlHolder in SoapUI API

import com.eviware.soapui.support.*

def request='''
<Body>
<keyboard1>Qwerty~12-qwerty</keyboard1>
<conversationId1>sfksjkdjf2213123</conversationId1>
</Body>
'''

XmlHolder holder = new XmlHolder(request)
log.info holder.getNodeValue("//keyboard1")