Pages

soapUI: Scripting Objects - WsdlTestCase Class

This is a continuation of a series of posts attempting to review some of the objects (and methods) you're likely to encounter when you start scripting in soapUI.  To see the full soapUI javadoc, click here.

WsdlTestCase Class

SoapUI's WsdlTestCase class provides access to a web service test case's properties.  A WsdlTestCase object is available via the testCase variable in test case Setup and TearDown scripts, but there are plenty of other ways of getting an instance, including methods of the WsdlTestSuite, WsdlTestCaseRunner, and TestStep classes.

- getName() : returns the name of the test case as a String
- getTestSuite() : returns the test case's test suite as a WsdlTestSuite object
- getTestStepList() : returns a Java List of test step objects corresponding to the test case's test steps
- getTestStepCount() : returns a count (as an integer) of test steps contained in the test case
getTestStepAt(int index) : given an integer representing the zero-based index of a test step in the test case, returns an object corresponding to that test step
getTestStepByName(String name) : returns the test step contained in the test case with name name
- findNextStepOfType(TestStep refStep, Class stepClass) : returns an object representing the next test step after test step refStep belonging to class stepClass
- findPreviousStepOfType(TestStep refStep, Class stepClass) : returns an object representing the previous test step before test step refStep belonging to class stepClass
getPropertyList() : returns a Java List of TestProperty objects corresponding to the test case's properties
getPropertyAt(int index) : given an integer representing the zero-based index of a property in the test case's list of properties, returns a TestProperty object corresponding to that property
getProperty(String name) : returns a TestProperty object corresponding to the property with name name
getPropertyValue(String name) : returns the value of the property with name name
setPropertyValue(String name, String value) : sets the value for the property with name name to value value

On to some examples.  Here's a sample test suite that compares two different services that provide postal code information:


The Setup script:

import com.eviware.soapui.impl.wsdl.teststeps.*

//Getting test case info
log.info("Starting TestCase: " + testCase.getName())
log.info("   From TestSuite: " + testCase.getTestSuite().getName())
//Getting test steps by class:
log.info("First PropertyTransfer test step after the first test step:")
nextPropTestStep = testCase.findNextStepOfType(testCase.getTestStepAt(0),PropertyTransfersTestStep)
log.info("     " + nextPropTestStep.getName())
log.info("Last Groovy test step before the GetInfoByZIP-10111 test step:")
prevGroovyTestStep = testCase.findPreviousStepOfType(testCase.getTestStepByName("GetInfoByZIP-10111"),WsdlGroovyScriptTestStep)
log.info("     " + prevGroovyTestStep.getName())

The steps to get the name of the test case and test suite should be pretty clear, but the findNextStepOfType() and findPreviousStepOfType() methods may require some further explanation.  These methods take two arguments; the first is the reference step-- in other words, when we say we're looking for a next or previous step, it's relative to this one.  The second argument is a test step class indicating which type of test step we're looking for.  For example, with the findNextStepOfType() call (the line starting with "nextPropTestStep="), the reference step is the first test step in our test case, retrieved with the getTestStepAt() method-- in our example suite, this is the "GetInfoByZIP-94043" test step.  The second argument is a class-- specifically, the PropertyTransferTestStep class (the import statement at the top of the script saves me some extra typing here-- without it, I'd have to type the fully qualified class name com.eviware.soapui.impl.wsdl.teststeps.PropertyTransferTestStep).  So we're asking for the first occurrence of a Property Transfer test step that occurs after the "GetInfoByZIP-94043" test step.  Here's the resulting log output from our Setup script:



Here's our TearDown script, which just prints out the test step count and a list of the test steps:

//Test step count:
log.info("Test Case contains : " + testCase.getTestStepCount() + " test steps...")
//Test step list:
stepList = testCase.getTestStepList()
for(x in stepList){
 log.info("          " + x.getName())
}

The resulting output:



The "SetZipto10111" Groovy Script test step demonstrates some of the methods for dealing with test case properties:

curTestCase = testRunner.getTestCase()
//Get Property List and show current values
myPropList = curTestCase.getPropertyList()
log.info("Properties:")
for(i in myPropList){
 log.info("     " + i.getName() + " : " + i.getValue())
}
//Set the Property "Zip" to "10111"
curTestCase.setPropertyValue("Zip","10111")

Output in the log file:



Finally, here's the code for the "SetZipto94043" Groovy Script test case, demonstrating the getPropertyAt() and getProperty() methods:

curTestCase = testRunner.getTestCase()
log.info("Current value of Zip property: " + curTestCase.getPropertyAt(1).getValue())
//Change value
curTestCase.setPropertyValue("Zip","94043")
log.info("Reset value; new value = " + curTestCase.getProperty("Zip").getValue())

And the resulting log output:

2 comments:

  1. Hi,
    I have tried with give code in Soap UI groovy scrip, but I am not able to get the test cases in my test suite.

    import com.eviware.soapui.impl.wsdl.teststeps.*

    //Getting test case info
    log.info("Starting TestCase: " + testCase.getName())
    log.info(" From TestSuite: " + testCase.getTestSuite().getName())

    Can you please tell me what configuration i have to do? Do i need to add property file?
    Please suggest.

    Regards,
    Vinod

    ReplyDelete
    Replies
    1. Vinod,
      This shouldn't require any additional configuration as it's working with "built-in" properties and not user-defined properties. The import statement isn't relevant to these lines of code, either, so that shouldn't be the problem.
      As I look at this post again, one thing that may not be 100% clear: this script is intended to be used within a test case setup script (not a test suite setup script). I'm not sure if that might be the problem here-- if you're running this code as part of a test suite setup script the testCase variable won't be available to you.
      If that's not the problem, are you seeing anything in any of the logs? An error message, etc.? Note that errors may be on another log tab. Also, if you've entered the code and try to run it via the run button just above the setup script panel or the test case run button, you may expect the output to appear in the test case log panel (right there in the same window as the run buttons); however, it will write its output to the script log panel. I've made this mistake myself on occasion, expecting the script output on one tab while it was "hidden" on another tab all along...

      Delete

Please be respectful of others (myself included!) when posting comments. Unfortunately, I may not be able to address (or even read) all comments immediately, and I reserve the right to remove comments periodically to keep clutter to a minimum ("clean" posts that aren't disrespectful or off-topic should stay on the site for at least 30 days to give others a chance to read them). If you're looking for a solution to a particular issue, you're free to post your question here, but you may have better luck posting your question on the main forum belonging to your tool's home site (links to these are available on the navigation bar on the right).