How to perform basic math in Cycle using Groovy

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: 

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

This sample Feature contains 4 Scenarios representing the 4 staples of simple math. Groovy is powerful and can do much more complicated processing but this article focuses on the basics.

  • Addition
  • Subtraction
  • Multiplication
  • Division

In each of the Scenarios the numbers to work with are assigned to Cycle variables and then passed into the inline version of the Groovy step to perform the function. The inline Groovy step will work with direct numbers as well but more commonly variables will be referenced in your Features.

After the mathematical function is performed a step to validate the results is executed. 

Feature: Simple Math

Scenario: Addition
Given I assign 4 to variable "qty"
And I assign 8 to variable "lines"
When I execute Groovy "sum = qty + lines"
Then I verify number $sum is equal to 12

Scenario: Subtraction
Given I assign 4 to variable "qty"
And I assign 8 to variable "lines"
When I execute Groovy "difference = lines - qty"
Then I verify number $difference is equal to 4

Scenario: Multiplication
Given I assign 4 to variable "qty"
And I assign 8 to variable "lines"
When I execute Groovy "product = qty * lines"
Then I verify number $product is equal to 32

Scenario: Division
Given I assign 4 to variable "qty"
And I assign 8 to variable "lines"
When I execute Groovy "quotient = lines/qty"
Then I verify number $quotient is equal to 2
    • Related Articles

    • 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 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 ...
    • 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 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 ...
    • 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 ...