Open In App

Run Selenium Tests with Gauge

Last Updated : 20 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Gauge is an open-source framework for test automation that is especially useful for Behavior Driven Development (BDD). It is characterized by its simplicity, scalability, and compatibility with other languages, such as Java, C#, and JavaScript. Gauge and Selenium work together to provide reliable, understandable, and manageable test suites for web applications. This tutorial will walk you through configuring and executing Selenium tests using Gauge. It covers advanced testing methodologies, authoring requirements, and project setup.

Setting Up the Test Environment

A. Project Setup

1. Install Gauge:

Download and install Gauge from the official website. Follow the instructions for your operating system.

2. Initialize a Gauge Project:

Open a terminal and navigate to your desired project directory. Run the following command to create a new Gauge project:

gauge init java

This command creates a basic Gauge project with Java.

3. IDE Plugin:

Install the Gauge plugin for your preferred IDE (IntelliJ IDEA, Visual Studio Code, etc.) to facilitate easy creation and management of Gauge specifications.

B. Dependencies

1. Build Tool:

Ensure you have a build tool like Maven or Gradle installed. This guide uses Maven for simplicity.

2. Add Dependencies:

Add the necessary dependencies for Gauge, Selenium, and the WebDriver to your pom.xml file (if using Maven):

<dependencies>
<dependency>
<groupId>com.thoughtworks.gauge</groupId>
<artifactId>gauge-java</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>

C. WebDriver Configuration

1. WebDriver Setup:

Download the appropriate WebDriver for your browser (e.g., ChromeDriver for Google Chrome) and ensure it is accessible in your system's PATH.

2. Driver Initialization:

Create a DriverFactory class to manage the WebDriver instance:

Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DriverFactory {
    private static WebDriver driver;

    public static WebDriver getDriver() {
        if (driver == null) {
            System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
            driver = new ChromeDriver();
        }
        return driver;
    }

    public static void closeDriver() {
        if (driver != null) {
            driver.quit();
            driver = null;
        }
    }
}

D. Page Object Model (POM) Implementation (Optional)

Implementing the Page Object Model (POM) enhances the maintainability and readability of your tests. Create separate classes for each page of your application and define the elements and actions.

Example of a LoginPage class:

Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {
    WebDriver driver;

    @FindBy(id = "username")
    WebElement username;

    @FindBy(id = "password")
    WebElement password;

    @FindBy(id = "loginButton")
    WebElement loginButton;

    public LoginPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void login(String user, String pass) {
        username.sendKeys(user);
        password.sendKeys(pass);
        loginButton.click();
    }
}

Output:

Selenium-4
OUTPUT

Writing Gauge Specifications

A. Specification Structure

Gauge specifications are written in Markdown and stored in the specs directory. A typical specification file (.spec) might look like this:

# Login Specification

## User should be able to login with valid credentials
* Navigate to the login page
* Enter valid credentials
* Click the login button
* Verify the user is logged in

B. Using Built-in Steps

Gauge provides several built-in steps for common actions like navigating to URLs, clicking elements, and verifying text. Use these to reduce redundancy in your specifications.

C. Writing Custom Steps

For steps specific to your application, write custom step implementations in Java. For example:

Java
import com.thoughtworks.gauge.Step;

public class LoginSteps {
    @Step("Navigate to the login page")
    public void navigateToLoginPage() {
        DriverFactory.getDriver().get("http://example.com/login");
    }

    @Step("Enter <username> and <password>")
    public void enterCredentials(String username, String password) {
        LoginPage loginPage = new LoginPage(DriverFactory.getDriver());
        loginPage.login(username, password);
    }

    @Step("Verify the user is logged in")
    public void verifyUserLoggedIn() {
        // Add verification logic here
    }
}

Advanced Integration Testing

A. Data-Driven Testing with Gauge

Gauge supports data-driven testing using table-driven specifications or external data sources like CSV or Excel files.

Example of a table-driven specification:

# Login Specification

## User should be able to login with valid credentials
* Navigate to the login page
* Enter <username> and <password>
* Click the login button
* Verify the user is logged in

| username | password |
| --------- | ---------- |
| user1 | pass1 |
| user2 | pass2 |

B. BDD with Gauge

Gauge naturally supports BDD by allowing you to write human-readable specifications that describe the behavior of your application. Use clear and concise language to make your specifications understandable to all stakeholders.

C. Continuous Integration and Deployment (CI/CD)

Integrate Gauge tests into your CI/CD pipeline using tools like Jenkins, GitLab CI, or GitHub Actions. Add a step in your pipeline configuration to run Gauge tests:

gauge run specs

D. Parallel Testing

Gauge supports parallel execution of tests to reduce overall test execution time. Configure parallel execution in the env/default/default.properties file:

# Number of parallel streams

gauge_parallel_streams = 4

E. Reporting and Debugging

Gauge provides detailed HTML reports by default. After running your tests, view the reports in the reports directory. For debugging, use Gauge’s --log-level option to control the verbosity of logs:

gauge run specs --log-level debug

Conclusion

By combining Gauge and Selenium, you can create robust, manageable, and understandable automated test suites. By following the instructions provided in this article, you can set up your testing environment, develop comprehensive specifications, and take advantage of advanced testing capabilities like data-driven testing, BDD, CI/CD integration, parallel testing, and thorough reporting. This approach ensures that your web applications are reliable and well-tested, leading to smoother releases and better user experiences.


Next Article
Article Tags :

Similar Reads