How to use Groovy to work with Strings

How to use Groovy to work with Strings

Problem

Other than concatenate Cycle does not include steps to work with strings.

Solution

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.

  • 1. Cycle can only input and output String and Number data types with Groovy statements and scripts.
    • Dates, arrays, lists, maps are not yet functional inputs or outputs but can be used within a Groovy script.
  • 2. Cycle outputs a variable called $groovy_result which represents the final result
    • Custom variables can be assigned and used

Examples

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.

Change the case of a string:

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

Get the length of a string:

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.

Trim leading or trailing spaces:

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.

Replace characters in a string:

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. 

Replace all characters in a string:

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. 

Return part of a string:

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.

    • Related Articles

    • How to use Groovy to work with Dates and Times

      Problem Cycle does not include steps to access and work with Date and Time. Solution Cycle now includes steps to execute simple Groovy statements as well as execute complex Groovy scripts. The 2 Cycle steps that execute Groovy are: I execute Groovy ...
    • How to use Groovy to return values in XML strings

      Problem It may be necessary to return values from XML strings in your automated tests. This article provides examples for common ways to use the Groovy programming language to output XML information. Solution Cycle includes steps to execute simple ...
    • Where can I get help using Groovy with Cycle?

      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>" Creating ...
    • How to perform basic math in Cycle using Groovy

      Problem Cycle can increment variable values but does not include steps for simple math Solution 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: ...
    • How to create methods in Groovy and call from Cycle

      Cycle includes a step to execute Groovy scripts. I execute Groovy script "<GROOVY_SCRIPT_FILE>" Groovy scripts can be constructed to perform multiple functions in a single file by creating user defined methods. Cycle variables can be used to control ...