Groovy in Cycle
Cycle includes the ability to execute simple Groovy statements inline and execute more complex Groovy scripts from external files.
The Steps used for these are:
I execute Groovy "<GROOVY_STATEMENTS>"
I execute Groovy script "<GROOVY_SCRIPT_FILE>"
This page assumes a conceptual familiarity with Groovy/Java, including general syntax, data types, import statements, class references, and method usage.
For more information on Groovy see Apache Groovy.
A few notes regarding Cycle and Groovy:
-
Cycle can currently only input and output
String
andNumber
data types with Groovy statements and scripts.- Dates, arrays, lists, and maps are not yet functional inputs or outputs but can be used within a Groovy script.
-
Cycle creates the variable
$groovy_result
which represents the last result.- Other custom variables can be assigned and used.
Inline Groovy Examples
These examples use classes and methods from the Java 8+ Date/Time API which replaced deprecated Date and Calendar functionality.
Getting the current date with an inline Groovy statement:
Scenario: Date
Given I execute Groovy """import java.time.*; dateStr = LocalDate.now()format("MM/dd/yyyy")"""
Then I echo $dateStr
This example imports the java.time library and in a single statement gets the current date, formats itm and converts it to a string for Cycle output. The format used in this example is MM/dd/yyyy but any syntactically valid format can be used.
A variation on this Scenario is to pass in the format from Cycle as a variable:
Scenario: Date Format Variable
Given I assign "MM/dd/yyyy" to variable "format"
Then I execute Groovy "import java.time.*; dateStr = LocalDate.now()format(format)"
Then I echo $dateStr
Getting current date time with an inline Groovy statement:
Scenario: Date Time
Given I execute Groovy """import java.time.*; dateTimeStr = LocalDateTime.now()format("MM/dd/yyyy HH:mm:ss")"""
Then I echo $dateTimeStr
This example imports the java.time library and in a single statement gets the current date time, formats it, and converts it to a string for Cycle output. The format used in this example is MM/dd/yyyy HH:mm:ss but any syntactically valid format can be used.
Groovy Script File Examples
Date Manipulation
The need may arise to not only access and use dates but also manipulate specified dates by creating ranges, date addition/subtraction, or reformatting. Since these require multiple steps we will be using Groovy scripts to perform the actions.
In the next example Scenario, we will be passing our known date as a variable and the user defined input format into our Groovy script. We will be returning the calculated dates based our user defined days variable in the user defined output format and echoing in the Cycle Output for validation. Once they are returned to Cycle they can be used however they are needed.
Scenario:Groovy script date
Given I assign "04/30/2019" to variable "dateStr"
And I assign "MM/dd/yyyy" to variable "format"
And I assign "dd-MM-yyyy" to variable "outFormat"
And I assign 7 to variable "days"
When I execute Groovy script "scripts\date_script.groovy" within 8 seconds
Then I echo $plusWeekStr
Then I echo $minusWeekStr
The following date_script.groovy
file is what gets executed in the Scenario.
We import the necessary libraries to perform the functions needed. As previously mentioned, Cycle can currently only parse strings and numbers so the next step is to convert the passed in string to a date data type to be manipulated in Groovy. We then add the number of days based on the days variable assigned in Cycle to create a new date variable. Similarly we create another date variable representing the passed in date minus the number of passed in days. The final step is to convert the new date variables into strings to be used as Cycle variables.
import java.text.DateFormat
import java.time.format.DateTimeFormatter
import java.time.*
//Convert string to date
strToDate = LocalDate.parse(dateStr,format)
//Add days value to the date
plusWeek = strToDate + days
//Minus days value from the date
minusWeek = strToDate - days
//Convert dates to strings for Cycle output
plusWeekStr = plusWeek.format(outFormat)
minusWeekStr = minusWeek.format(outFormat)
Date Time Manipulation
In the next example Scenario, we will be passing our known date time as a variable and the user defined input format into our Groovy script. We will be returning the calculated date times based our user defined hours or seconds variable in the user defined output format and echoing in the Cycle Output for validation. Once they are returned to Cycle they can be used however desired.
Scenario:Groovy script date time
Given I assign "04/30/2019 23:21:45" to variable "dateStr"
And I assign "MM/dd/yyyy HH:mm:ss" to variable "format"
And I assign "yyyy-MM-dd HH:mm:ss" to variable "outFormat"
And I assign 2 to variable "hours"
And I assign 45 to variable "seconds"
Then I execute Groovy script "scripts\date_time_script.groovy" within 8 seconds
Then I echo $plusHourStr
Then I echo $minusSecondsStr
The following date_time_script.groovy
file is what gets executed in the Scenario. We import the necessary libraries to perform the functions needed. As previously mentioned, Cycle can currently only parse strings and numbers so the next step is to convert the passed in string to a date data type to be manipulated in Groovy. We then add the number of hours based on the hours variable assigned in Cycle to create a new date variable. Similarly, we create another date variable representing the passed in date time minus the number of passed in seconds. The final step is to convert the new date variables into strings to be used as Cycle variables.
import java.text.DateFormat
import java.time.format.DateTimeFormatter
import java.time.*
//Convert string to date
strToDate = LocalDateTime.parse(dateStr,format)
//Add hours value to the date time
plusHour = strToDate.plusHours(hours)
//Minus seconds value from the date time
minusSeconds = strToDate.minusSeconds(seconds)
//Convert date back to string for Cycle output
plusHourStr = plusHour.format(outFormat)
minusSecondsStr = minusSeconds.format(outFormat)