Other than concatenate Cycle does not include steps to work with strings.
Cycle now includes steps to execute simple Groovy statements inline as well as execute complex Groovy scripts.
The 2 Cycle steps that execute Groovy are:
I execute Groovy "<GROOVY_STATEMENTS>"
I execute Groovy script "<GROOVY_SCRIPT_FILE>"
This article 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
Two items of note regarding Cycle and Groovy.
The following examples use methods from Groovy's String class does not require an explicit import statement. As such, they can easily be used as inline Groovy statements as opposed to an external Groovy script.
Scenario:Groovy To Upper and Lower Case
Given I assign "user" to variable "username"
And I assign "PASS" to variable "password"
When I execute groovy "upper = username.toUpperCase()"
Then I echo $upper
When I execute groovy "lower = password.toLowerCase()"
Then I echo $lower
This Scenario uses the toUpperCase() and toLowerCase() methods in Groovy to convert the case of string characters. In this example the Cycle variable username with value user is passed as an object to the toUpperCase() method and the value USER is returned. Similarly the Cycle variable password with value PASS is passed as an object to the toLowerCase() method and the value pass is returned
Scenario: Groovy inline length
Given I assign " A " to variable "spaces"
Then I execute groovy "a = spaces.length()"
Then I echo $a
This Scenario uses the length() method in a Groovy statement to return the total character count of the passed in string.
Scenario: Groovy inline trim
Given I assign " A " to variable "spaces"
Then I execute groovy "a = spaces.length()"
Then I echo $a
Then I execute groovy "noSpaces = spaces.trim()"
Then I echo $noSpaces
Then I execute groovy "b = noSpaces.length()"
Then I echo $b
Then I verify number $a is greater than $b
And I verify number $b is equal to 1
This Scenario uses the trim() method in a Groovy statement to remove the leading and trailing spaces in the string. It builds on the use of length() in order to validate our trim() was successful.
Scenario:Groovy Inline Replace
Given I assign "CASEORD001" to variable "order"
Then I assign "001" to variable "oldSuffix"
Then I assign "002" to variable "newSuffix"
Then I execute groovy "newOrder = order.replace(oldSuffix,newSuffix)"
Then I echo $newOrder
This Scenario uses the replace(target,replacement) method to replace character values within a string. The method requires target and replacement value which can be explicitly set in the call or passed in with Cycle variables.
Scenario:Groovy Inline Replace All
Given I assign "This string should have all of its spaces removed" to variable "spaces"
Then I execute groovy """noSpaces = spaces.replaceAll("\\s","")"""
Then I echo $noSpaces
Similar to replace (target,replacement) is replaceAll(target,replacement). True to its name the replaceAll() method will change all occurrences of the target to the replacement. In the Scenario all occurrences of a space are replaced with a null string, or more simply removed.
Scenario:Groovy Inline Substring
Given I assign "TodayIsAGreatDay" to variable "sentence"
#Provide a starting point and go to the end
Then I execute groovy "full = sentence.substring(0)"
Then I echo $full
#Provide a starting point and an endpoint
Then I execute groovy "great = sentence.substring(8,13)"
Then I echo $great
#Get the user defined end
Given I assign 3 to variable "end"
Then I execute groovy "day = sentence.substring(sentence.length() - end)"
Then I echo $day
The substring(start, end) method in Groovy is used to return pieces of a string. This Scenario demonstrates 3 common ways to get parts of a string.
The first way is by only providing a start parameter. In this case the characters from the start position until the end will be returned. A start index of 0 without and end index will return the original string.
The second way is providing both the start and the end. The return will be the characters in that range. In this example the sentence string charters from 8 to 13 are returned which is the word Great.
The third way is return the last X characters from a string. In this example the number of characters to grab from the end is set in a Cycle variable. That variable is passed in and subtracted from the length (we are using length() again!) to calculate the desired starting point. Since that is the only index provided the return is from that point until the end of the string which in this case is the word Day.