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:

soapUI: Scripting Objects - WsdlTestCaseRunner 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.

WsdlTestCaseRunner Class

The WsdlTestCaseRunner object is analogous to the WsdlTestSuiteRunner object at the test case level.  A WsdlTestCaseRunner object for each web service test case is accessible directly via the testRunner variable available in test case level Setup and TearDown scripts and Groovy Script test steps; another common way of accessing it is through the WsdlTestSuiteRunner getResults() method (which returns a list of test case runners).  Through the WsdlTestCaseRunner object, you can control execution of a test case (altering the order in which test steps are executed, for example) and get test results.

Some of its methods:

getResults() : returns a Java List of objects representing results for the test steps contained in the test case
getTestCase() : returns a WsdlTestCase object representing this test case runner's test case
getStartTime() : returns the time (as a long) this test case runner was last started
getTimeTaken() : returns the time taken (as a long) since the test case runner was last started
getStatus() : returns the test case's run status (as defined in the TestRunner.Status enum type; possible statuses include CANCELED, FAILED, FINISHED, INITIALIZED, RUNNING, and WARNING)
- gotoStep(int index) : designates the test step at 0-based index index as the next step to be executed; note that execution is not immediately passed to the designated test step-- the current step will complete execution first
- gotoStepByName(String name) : designates the test step with name name as the next step to be executed; execution is not immediately passed to the designated test step
- runTestStepByName(String name) : runs the test step with name name and returns a test result; note that unlike the goto... methods above, the runTestStepByName() method will cause execution to pass immediately to the designated test step and return execution back to the step where it was invoked

Let's take a look at some examples.  Let's start with this sample test suite (a modified version of the test suite used in my data looping post:


The ElementSpecificRequests test case has the following Setup script:

//Get start time and test case name from the runner
startTime = new Date(testRunner.getStartTime())
log.info(testRunner.getTestCase().getName() + " started at: " + startTime)
//Get test case runner status
log.info("Runner status: " + testRunner.getStatus())
//Run Groovy3 Test Step-- returns a result
groovy3Result = testRunner.runTestStepByName("Groovy3")
log.info("Back in Setup: Groovy3 Result " + groovy3Result.getStatus())

Here's the Teardown script:

//Get time taken and runner status
log.info("Status: " + testRunner.getStatus())
log.info("Time Taken: " + testRunner.getTimeTaken() + "ms")
//Get and list step results
listStepResults = testRunner.getResults()
for (res in listStepResults){
 log.info(res.getTestStep().getName() + " : " + res.getStatus()) 
}

The scripts in the SkipGroovy, Groovy2, and Groovy3 steps simply announce that they're running by writing a line to the script log, but the Groovy1 step contains this:

//Illustrates gotoStep
log.info("In Groovy1; executing gotoStep(2)...")
testRunner.gotoStep(2)
log.info("Still in Groovy1 step!  Goto is not immediate...")

The output when this test suite is run is:



There are a few things worth pointing out in the scripts and log output.  First, take a look at lines 3 and 4 in the script log (starting with "Groovy Step Groovy3 is executing!")-- these correspond to the lines (starting at line 7) in the Setup script where the runTestStepByName() method was called.  Note that in this case the Groovy3 step is run immediately for a result (passed back by the method) and then execution continues in the Setup script.

Compare this with the behavior of the gotoStep() method called in line 2 of the Groovy1 test step.  The method calls the step at 0-based index 2 (for those unfamiliar with the terminology, 0-based essentially means you start counting at 0 when counting items in the list-- the first item is at index 0, the second at index 1, etc.).  The step at index 2 corresponds to the Groovy2 test step, but execution is delayed; you can see the the final line of the Groovy1 script is executed before the Groovy2 script begins.  Also note that the GroovySkip step is skipped altogether-- after execution is passed from Groovy1 to Groovy2 it continues on as normal from there.

Finally, if you look at our step results (starting at line 11 of the script log: "Groovy3 : OK"), you'll see that the Groovy3 step appears twice.  That particular step was run twice (called once from the Setup script and run again as part of the normal step sequence); consequently, both run instances are reflected in our list of results.

soapUI: Scripting Objects - WsdlTestSuite 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.

WsdlTestSuite Class

A WsdlTestSuite object representing your web service test suite is accessible directly via the testSuite variable available to Setup and TearDown scripts and through methods of some of its children (e.g., test cases) and the WsdlTestSuiteRunner object.  Through the WsdlTestSuite object, you can access test cases belonging to the test suite and test suite properties.

A few (and really only a few-- there are many many more) of its methods:

- getName() : returns the name of the test suite as a String
- getTestCaseList() : returns a Java List of WsdlTestCase objects corresponding to test cases contained in the suite
- getTestCaseCount() : returns a count of the test cases contained in the suite (as an integer)
- getTestCaseAt(int index) : given an integer representing the zero-based index of a test case in the test suite, returns an object corresponding to that test case
- getTestCaseByName(String name) : returns the test case contained in the test suite with name name
- getProperties() : returns a Map of the test suite's properties
- getPropertyList() : returns a Java List of TestProperty objects corresponding to the test suite's properties
- getPropertyAt(int index) : given an integer representing the zero-based index of a property in the test suite'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

Here's the starting test suite properties for a sample test suite with two test cases simply named TestCase1 and TestCase2:


A sample Setup script (at the suite level) for this test suite that illustrates some of these methods:

//Get the test suite name
log.info("Test Suite Name: " + testSuite.getName())
//Get the number of test cases in the suite
log.info("Test Case Count: " + testSuite.getTestCaseCount())
//Get test suite's test cases as a list
myTCList = testSuite.getTestCaseList()
for(i in myTCList){
 log.info("Test Case Name: " + i.getName())
}
//Get the test case at index 0
log.info("Test Case at index 0: " + testSuite.getTestCaseAt(0).getName())
//Get the test case named TestCase2 - a silly example for illustrative purposes only!
log.info("Getting TestCase2 by Name: " + testSuite.getTestCaseByName("TestCase2").getName())
//Get the value of Property1
log.info("Value of Property1: " + testSuite.getPropertyValue("Property1"))
//Change the value of Property1 - we'll see the change in TearDown
testSuite.setPropertyValue("Property1", "Prop1Changed")

The resulting output in the script log:



A sample Teardown script for the same test suite illustrating some more methods:

//Get properties as a Map
myPropertiesMap = testSuite.getProperties()
//Note that Property1 reflects the value we set it to at the end of the SetUp script
log.info("Property1 value = " + myPropertiesMap["Property1"].getValue())
//Set Property1 back to PropertyValue1
testSuite.setPropertyValue("Property1","PropertyValue1")
//Get properties as a List
myPropList = testSuite.getPropertyList()
//Property1 value reflects change back to PropertyValue1
for(prop in myPropList){
 log.info(prop.getName() + " = " + prop.getValue())
}
//Get a property by index
myTestProperty = testSuite.getPropertyAt(2)
log.info("Property at index 2: " + myTestProperty.getName() + " = " + myTestProperty.getValue())
//Get a property by name
myNamedTestProperty = testSuite.getProperty("UName")
log.info("UName value = " + myNamedTestProperty.getValue())

The resulting output in the script log:

soapUI: Scripting Objects - WsdlTestSuiteRunner Class

Looking over traffic on past posts, I can see that one of the more popular topics has been the walk-through of data looping using scripting in soapUI, so I thought it might be a good idea to return to the topic in general, and the soapUI API in particular.  By Smartbear's own admission, it's pretty massive-- on the one hand, it's a testament to how much is packed into the program to begin with, but wading through the Javadoc (you can find it here) can get a bit tedious.  In the next few posts I'm going to try to review some of the objects you're likely to be interested in when you begin to use scripting in soapUI.

Obviously, these posts will be far from complete; to avoid biting off more than I can chew, I'm going to limit the discussion to scripting via the "containers" provided in soapUI-- in other words, the Setup and TearDown scripts available at the test suite and test case levels and Groovy Script testing steps.  I'm also not providing constructor methods, the assumption being that objects will be accessed directly or indirectly via the variables (like the runner variable) provided by soapUI.

With each post I'll try to tackle one or two objects and some of their methods.  This week we'll look at the WsdlTestSuiteRunner object.

WsdlTestSuiteRunner Class

A WsdlTestSuiteRunner object is easily accessible in soapUI via the runner variable available in a web service test suite's Setup and TearDown scripts.  As you might expect, it controls execution of the test suite, but it can also be used to access run results and other useful scripting objects.

A few of its methods:

- getResults() : returns a Java List of TestCaseRunner objects (see the post for the WsdlTestCaseRunner class, which is generally what you'll be dealing with) corresponding to the suite's test cases; these can be used in turn to access test step results
- getTestSuite() : returns a WsdlTestSuite object representing this test suite runner's test suite
- getStartTime() : returns the time (as a long) the runner was last started
- getTimeTaken() : returns the time taken (as a long) since the runner was last started
- getStatus() : returns the test suite's run status (as defined in the TestRunner.Status enum type; possible statuses include CANCELED, FAILED, FINISHED, INITIALIZED, RUNNING, and WARNING)

Here's a sample script (from the Setup script for a standard web service test suite called "Object Model Test Suite") that demonstrates some of these methods:

//Demonstrating the object type provided by the runner variable
log.info(runner.toString())
//Getting the runner's test suite
tsMySuite = runner.getTestSuite()
log.info("Name of Test Suite is " + tsMySuite.getName())
//Get test suite run start time
tsStart = runner.getStartTime()
//Convert from long to date format and display
startTime = new Date(tsStart)
log.info(startTime)
//Display test suite status
log.info(runner.getStatus())

Here's the corresponding output in the script log window when the test suite is run:



Here's an example TearDown script:

//Get test suite status
log.info(runner.getStatus())
//Get test suite time taken
tsTimeTaken = runner.getTimeTaken()
log.info("Time Taken: " + tsTimeTaken + "ms")
//Get results as List of TestCaseRunners
lstTCRunners = runner.getResults()
//Iterate through test case runners to get test case info and statuses
for(tcr in lstTCRunners){
 log.info("Test case name: " + tcr.getTestCase().getName())
 log.info("Test case status: " + tcr.getStatus())
}

And the TearDown script's corresponding output in the script log window: