Author Topic: SoapUI: Setting Project-wide variables in code, then accessing them  (Read 7012 times)

Offline John Gimber

  • QA Test Manager
  • Administrator
  • Newbie
  • *****
  • Posts: 14
  • Karma: +0/-0
  • QualityScape FTW!
    • View Profile
    • QualityScape
So, coding in SoapUI.  Always fun.

I'm aware that it is possible to have project-wide groovy setup scrips, but these seem a bit limited, and won't run sometimes.

My challenge was to create some dates (for WSDL testing), then use these dates throughout the project (both in code, and directly inside the requests).

Here are the code examples of how I did it:

Setting the project-wide variables

OK, so I selected the Project root, and viewed the Project's [Custom Properties] tab.
I added each of the property names that I wanted to use, e.g.

dtToday (current date)
dtTomorrow (tomorrow)
dtNextWeekDay (the next available weekday after today)
dtNextSaturday (the next available Saturday)
dtNextSunday (the next available Sunday)

Setting the variables in code

I tried to create a Project setup script, but this failed with an error: "java.lang.NullPointerException: Cannot get property 'testSuite' on null object"

To get around this, I created a "First Test Suite" containing just a groovy script, and called it "Setup Script", imaginatively enough.

This contains:

Code: [Select]
import groovy.time.TimeCategory
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

log.info "--------------------------------------------------------------------------------"
log.info "TEST SETUP"
log.info "--------------------------------------------------------------------------------"

// To read a project-wide variable:
// log.info context.expand('${#Project#dtToday}')

// To write to a project-wide variable:
// context.testCase.testSuite.project.setPropertyValue("dtToday","coo2")
// log.info context.expand('${#Project#dtToday}')

// To use inside a request...
// <TargetDepartureTime>${#Project#dtNextWeekDay}</TargetDepartureTime

use (TimeCategory)
{
// Get (today)
dtToday = new Date().format("yyyy-MM-dd'T12:00:00'")
context.testCase.testSuite.project.setPropertyValue("dtToday",dtToday)
log.info "Today = " + context.expand('${#Project#dtToday}')

// Get (tomorrow)
dtTomorrow = new Date() + 1.day
dtTomorrow = dtTomorrow.format("yyyy-MM-dd'T12:00:00'")
context.testCase.testSuite.project.setPropertyValue("dtTomorrow",dtTomorrow)
log.info "Tomorrow = " + context.expand('${#Project#dtTomorrow}')

// Get (nextWeekDay)
dt = new Date() + 1.day
weekday = dt[Calendar.DAY_OF_WEEK]
if (weekday == Calendar.SATURDAY)
{
dt += 2
}
if (weekday == Calendar.SUNDAY)
{
dt += 1
}
weekday = dt[Calendar.DAY_OF_WEEK]

dtNextWeekDay = dt.format("yyyy-MM-dd'T12:00:00'")
weekday = dt.format("EEEE")
context.testCase.testSuite.project.setPropertyValue("dtNextWeekDay",dtNextWeekDay)
log.info "Next Weekday = " + context.expand('${#Project#dtNextWeekDay}') + " ($weekday)"

// Get (nextSaturday)
dt = new Date() + 1.day
while (dt[Calendar.DAY_OF_WEEK] != Calendar.SATURDAY)
{
dt++
}
weekday = dt[Calendar.DAY_OF_WEEK]
dtNextSaturday = dt.format("yyyy-MM-dd'T12:00:00'")
weekday = dt.format("EEEE")
context.testCase.testSuite.project.setPropertyValue("dtNextSaturday",dtNextSaturday)
log.info "Next Saturday = " + context.expand('${#Project#dtNextSaturday}') + " ($weekday)"

// Get (nextSunday)
dt++
weekday = dt[Calendar.DAY_OF_WEEK]
dtNextSunday = dt.format("yyyy-MM-dd'T12:00:00'")
weekday = dt.format("EEEE")
context.testCase.testSuite.project.setPropertyValue("dtNextSunday",dtNextSunday)
log.info "Next Sunday = " + context.expand('${#Project#dtNextSunday}') + " ($weekday)"
}

log.info "--------------------------------------------------------------------------------"
log.info "TEST SETUP COMPLETED"
log.info "--------------------------------------------------------------------------------"

When I run this Groovy script, the log output is as follows:

Code: [Select]
Sun Mar 08 07:40:49 GMT 2015:INFO:--------------------------------------------------------------------------------
Sun Mar 08 07:40:49 GMT 2015:INFO:TEST SETUP
Sun Mar 08 07:40:49 GMT 2015:INFO:--------------------------------------------------------------------------------
Sun Mar 08 07:40:49 GMT 2015:INFO:Today = 2015-03-08T12:00:00
Sun Mar 08 07:40:49 GMT 2015:INFO:Tomorrow = 2015-03-09T12:00:00
Sun Mar 08 07:40:49 GMT 2015:INFO:Next Weekday = 2015-03-09T12:00:00 (Monday)
Sun Mar 08 07:40:49 GMT 2015:INFO:Next Saturday = 2015-03-14T12:00:00 (Saturday)
Sun Mar 08 07:40:49 GMT 2015:INFO:Next Sunday = 2015-03-15T12:00:00 (Sunday)
Sun Mar 08 07:40:49 GMT 2015:INFO:--------------------------------------------------------------------------------
Sun Mar 08 07:40:49 GMT 2015:INFO:TEST SETUP COMPLETED
Sun Mar 08 07:40:49 GMT 2015:INFO:--------------------------------------------------------------------------------

Accessing / reading project-wide variables in Groovy

So, we have a script which runs when the project is run (via Bamboo, or TestRunner, whatever).
That sets the dates, which we we can use in our tests.

Here is a section of code which would use this variable:

Code: [Select]
log.info context.expand('${#Project#dtToday}')
That picks up the project-wide property "dtToday" and prints it out.  Or puts it into a local variable for working with.

Accessing / reading project-wide variables in the SoapUI request

How do we use this value inside a SoapUI WSDL call?  Not too difficult.  Like this:

Code: [Select]
         <Departure>
            <TargetDepartureTime>${#Project#dtToday}</TargetDepartureTime>
            <EarliestDepartureLeeway>PT0M</EarliestDepartureLeeway>
            <LatestDepartureLeeway>PT120M</LatestDepartureLeeway>
         </Departure>

So you can see here we pipe in the project-wide property dtToday directly, and this gets expanded and sent as part of the test request.

Writing to project-wide variables in a Groovy script

To write information back out to the project-wide variable, we can use a line of Groovy code like this:

Code: [Select]
context.testCase.testSuite.project.setPropertyValue("dtToday","2015-03-08T15:45:00")
That will override any existing value and will be the new value to use.
« Last Edit: March 08, 2017, 10:02:32 AM by John Gimber »
Most people just don't understand QA.  Spread the good word!
QualityScape - Forum
View John Gimber's profile on LinkedIn