1. Preface
    1. About MarathonITE
    2. Supported Platforms
      1. Java/Swing™
      2. Java/FX™
      3. Web Applications
    3. What's New in MarathonITE 5.0
    4. Change Log a.k.a Version History
    5. System Requirements
    6. Copyright Notice
  2. Getting Started
    1. Introduction to Test Automation
      1. Unattended Testing
      2. Semi Automated Testing
      3. Exploratory Testing
    2. MarathonITE Projects
    3. Your First Project
      1. Java/Swing™ Test Project
        1. Application Under Test - SwingSet3
        2. Creating a Project
        3. Recording a Test
        4. Anatomy of a Test Script
        5. Running Tests
        6. Looking at Results
      2. Java/FX™ Test Project
        1. Application Under Test - Ensemble
        2. Creating a Project
        3. Recording a Test
        4. Anatomy of a Test Script
        5. Running Tests
        6. Looking at Results
      3. Web Application Test Project
        1. Application Under Test - DuckDuckGo Search
        2. Creating a Project
        3. Recording a Test
        4. Anatomy of a Test Script
        5. Running Tests
        6. Looking at Results
    4. MarathonITE Sample Projects
  3. User Guide
    1. Installation and Startup
    2. MarathonITE User Interface
      1. Views
      2. Editors
      3. Output Views
    3. Creating Marathonite Test Projects
    4. Recording Tests
    5. Managing Checklists
    6. Exploratory Tests
    7. Semi Automated Tests
    8. Executing Tests
      1. Executing a Test from Editor
        1. Debugging Scripts
        2. Using Script Console
      2. Executing Tests from Test Runner
      3. Executing Tests in Batch Mode
    9. Organizing Tests
      1. Organizing Tests in Folder Heirarchy
      2. Organizing Tests as Features and Stories
      3. Organizing Tests in Suites
      4. Linking Tests to TMS and Issue Manager
    10. Modularizing Test Scripts
      1. Module Methods
      2. Extract Method Refactoring
      3. Using Data Loops
      4. Convert to Data Loop Refactoring
    11. Data Driven Tests
      1. Convert to DDT Refactoring
  4. Advanced Scripting
    1. Ruby Programming Language
    2. Marathon and Ruby
    3. Selenium/WebDriver Bindings
      1. Java/Swing™ Components
      2. Java/FX™ Controls

4.1.Ruby Programming Language

JRuby – not Ruby

Though Marathon refers to the scripting as ruby it is actually uses JRuby – a Java implementation of ruby. Your Marathon or MarathonITE installation contains a copy of JRuby included.

Ruby Console

Run mirb to invoke JRuby console or mruby to invoke JRuby from Marathon installation folder.

Ruby Basics

In this section we will quickly walk through the basics of Ruby as a programming language.

Variables

There are 4 different types of variables you will come across in Ruby.

  • Constants
    Ruby constants begin with an uppercase letter. All Java class names are considered as constants in JRuby.

    Example Constants.

    Var1 = 100. The variable Var1 is a constant.

    System.getProperty('java.version') or System.get_property('java.version'). In this statement System is a Java class and also a JRuby constant.

  • Local Variables
    Local variables begin with a lowercase letter or _. The scope of a local variable ranges from beginning of a block (class, module, def) till the end of that block.

  • Golbal Variables
    Global variables begin with $. It is best to avoid global variables. Marathon uses global variables for data driven testing.

    Example Global Variables.

    setnamepassword($name, $password).

    When using data driven testing, each record from the data file is read and each column value is set to a global variable (whose name is in the header row of that column).

  • Ruby Instance and Class variables
    Instance variables begin with @ and Ruby class variables begin with @@.

Ruby Special Variables

A set of special variables are defined by Ruby – these might appear as local variables – but you cannot write into these variables.

  • self – The current object.

  • true – Value representing true boolean value.

  • false – Value representing false boolean value

  • nil – Value representing undefined or uninitialized.

  • __FILE__\ – The name of the current source file (string)

  • __LINE__\ – The current line number (integer)

Strings

Ruby strings are enclosed in single quote or double quote characters. A string delimiter within a string can be escaped using the backslash character. Ruby strings are powerful. You can substitute the value of any ruby expression into a string using #{ and } delimeters.

Ruby String Examples.

The output from the above snippet will be:

Methods

Ruby methods are used to extract repeated set of statements into a single unit. Method names should begin with a lowercase letter.

Method without any arguments.

Method with arguments.

Method with arguments and default values.

Default values can only be omitted for the last set of arguments.

Conditionals

Ruby provides if/else, unless/else and case conditional statements.

Example if…else statement.

Output.

You can also use an if modifier at tail end of code that is to be executed if the condition is true.

Example if modifier.

Output.

I shall always print

The unless statement is reverse of if. The body is executed only when the conditional is false. unless can also be suffixed to code.

Example unless statement.

Output.

You can also use the case statement for reducing the number of if…else branches where appropriate.

Example case statement.

Output.

Note the use of inclusive range operator ..(double dots). Ruby also has an exclusive range operator ...(triple dots)

Loops

Loops and iterators are used to execute the same block of code multiple times. In a loop, a condition controls the number of times a loop is executed, whereas you use an iterator to perform an action for each of the elements in the container.

A while statement executes the given block of code till a given conditional becomes false.

while statement example.

Output.

The while keyword can also be used as a modifier at the end of a block of code. The loop body is executed at least once in this case.

while modifier example.

Output.

The while modifier can also be used on a single line as follows:

while modifier single line example.

Output.

until statement is complementary to the while statement. The loop is executed till the given conditional becomes true.

until statement example.

Output.

until can also be used as a modifier and given at the end of a code block.

until modifier example.

Output.

A for statement executes code once for each element in an expression.

for statement example.

Output.

A loop can be terminated prematurely by using the break statement.

break example.

Output.

You can skip processing the current iteration of a loop and go to the next iteration by using next statement.

next example.

Output.

Arrays

Ruby arrays are ordered, integer indexed collections of heterogeneous objects. Each element of an array is accessed using an integer index. Array indexing starts at 0.

Example Array and Iteration.

Output.

Hashes (or Maps)

A Hash is a collection of key/value pairs. Hash indexing is done by arbitrary keys of any type.

Example Hash and usage.

Output.

Note that the months.keys returns an Array.

Iterators

Iterators are methods that perform an action on each element of a collection. each method performs a body of code for each element of the collection.

each Example.

Output.

Using each on a Hash gives each element as an array containing the key and value.

each Example with Hash.

Output.

You can use collect method to transform a collection.

collect Example.

Output.

collect can also be used on a Hash.

collect Example with a Hash.

Output.

You can find an element that confirms to a certain criteria using the find method.

find Example.

Output.

The find method returns only the first element. Use find_all method to find all elements.

find_all Example.

Output.

Java Access

You can access Java objects from JRuby scripts. This is very useful in Marathon scripts because you can access any Java API to perform operations. One example of this is accessing excel sheets using apache POI library. You can create new objects of these classes using regular Ruby syntax.

Creating a new Java Object.

Accessing Object properties.

Once you create a Java object you can call methods and access properties of the object using regular ruby operations.

Suggest Edit