Comprehensive Guide to Gauge APIs – Everything You Need to Know

Introduction to Gauge

Gauge is an open-source test automation tool that is simple and flexible to use. It aims to make test creation easy and maintainable. By utilizing the power of Markdown and rich ecosystem, users can create readable and understandable test cases.

Gauge API Examples

1. Gauge API Setup

gauge --init java

This command initializes a new Gauge project in Java. You can replace “java” with other supported languages like “ruby” or “python”.

2. Defining Specifications

Specification Heading
======================
This is a specification file where you write human-readable test steps.

## Test a feature
* Step 1: Do something
* Step 2: Verify something

3. Writing Steps in Java

import com.thoughtworks.gauge.Step;

public class StepImplementation {
    @Step("Step 1: Do something")
    public void doSomething() {
        // Code to do something
    }

    @Step("Step 2: Verify something")
    public void verifySomething() {
        // Code to verify something
    }
}

This example is written in Java; Gauge also supports other languages such as Ruby, Python, and C#.

4. Running Tests

gauge run specs

This command runs all the specifications present in the “specs” directory.

5. Custom Hooks

import com.thoughtworks.gauge.BeforeScenario;
import com.thoughtworks.gauge.AfterScenario;

public class Hooks {
    @BeforeScenario
    public void beforeScenario() {
        // Code to run before every scenario
    }

    @AfterScenario
    public void afterScenario() {
        // Code to run after every scenario
    }
}

Hooks are used to perform setup and teardown operations before or after scenarios.

App Example

Here’s a simple example of a to-do list application tested using Gauge APIs:

Specification

Empty the trash
===============
A simple to-do list specification.

## Scenario: Adding a Task
* Add a task "Buy groceries"
* Verify that the task "Buy groceries" is present in the list

Step Implementation

import com.thoughtworks.gauge.Step;

import java.util.ArrayList;
import java.util.List;

public class TodoSteps {
    private List todoList = new ArrayList<>();

    @Step("Add a task ")
    public void addTask(String task) {
        todoList.add(task);
    }

    @Step("Verify that the task  is present in the list")
    public void verifyTask(String task) {
        if(!todoList.contains(task)) {
            throw new RuntimeException("Task not found: " + task);
        }
    }
}

Running the App

To run the to-do list test:

gauge run specs

As you can see, Gauge makes it easy to create readable tests that anybody can understand. It also supports multiple programming languages, making it an excellent choice for test automation.

Hash: a90fd9a9a1e66597ae124f542f73ac08d3112e7d6f5e1781163be07ccae5be0d

Leave a Reply

Your email address will not be published. Required fields are marked *