Run Selenium Tests with Gauge
Last Updated :
20 Aug, 2024
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:
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.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING