Skip to main content

Gauge with Selenium

Tutorial to Run Your First Test on LambdaTest


In this topic, you will learn how to configure and run your Java automation testing scripts on LambdaTest Selenium cloud platform using Gauge framework. Like Selenium, Gauge also offers support for various languages such as JavaScript, C#, Ruby, Python, and Java.

Objective


By the end of this topic, you will be able to:

  1. Set up an environment for testing your hosted web pages using Gauge framework.
  2. Understand and configure the core capabilities required for your Selenium test suite.
  3. Run test cases in parallel using Gauge framework to reduce build times.
  4. Test your locally hosted pages on LambdaTest platform.
  5. Explore advanced features of LambdaTest.
Sample repo

All the code samples in this documentation can be found on LambdaTest's Github Repository. You can either download or clone the repository to quickly run your tests. Image View on GitHub

Pre-requisites


Before you can start performing Java automation testing with Selenium, you would need to:

  • Install the latest Java development environment. We recommend to use Java 11 version.

  • Download the latest Selenium Client and its WebDriver bindings from the official website. Latest versions of Selenium Client and WebDriver are ideal for running your automation script on LambdaTest Selenium cloud grid.

  • Install Maven. It can be downloaded and installed following the steps from the official website. Maven can also be installed easily on Linux/MacOS using Homebrew package manager.

  • Install the Gauge framework which can be downloaded from the official website of Gauge.

Cloning Repo and Installing Dependencies

Step 1: Clone the LambdaTest’s gauge-selenium-sample repository and navigate to the code directory as shown below:

git clone https://github.com/LambdaTest/gauge-selenium-sample
cd gauge-selenium-sample

You may also want to run the command below to check for outdated dependencies.

mvn versions:display-dependency-updates

Step 2: Install the mandatory Selenium dependencies for Maven by running the below command:

mvn compile

Setting up Your Authentication

Make sure you have your LambdaTest credentials with you to run test automation scripts on LambdaTest Selenium Grid. You can obtain these credentials from the LambdaTest Automation Dashboard or through LambdaTest Profile.

Step 3: Set LambdaTest Username and Access Key in environment variables.

export LT_USERNAME="undefined" \
export LT_ACCESS_KEY="undefined"

Run Your First Test


Sample Test with Gauge

Let’s checkout sample Gauge code running LambdaTest Selenium grid.

StepImplementation_ToDo.java
package driver.driver;

import com.thoughtworks.gauge.Step;
import driver.driver.DriverFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;

public class StepImplementation_ToDo {

private final WebDriver driver;

public StepImplementation_ToDo() {
this.driver = DriverFactory.getDriver();
}

@Step("Open the todo app")
public void gotoApp() throws InterruptedException {

System.out.println(DriverFactory.getDriver());
driver.get("https://lambdatest.github.io/sample-todo-app/");
String title = driver.getTitle();
assertEquals(title,"Sample page - lambdatest.com");
}

@Step("Select the desired items")
public void selectItems() throws InterruptedException {

WebElement itemOne = driver.findElement(By.name("li1"));
itemOne.click();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

WebElement itemThird = driver.findElement(By.name("li3"));
itemThird.click();
}

@Step("Add new item <itemName>")
public void addNewItem(String itemName) throws InterruptedException {

WebElement addItem = driver.findElement(By.id("sampletodotext"));
addItem.sendKeys(itemName);
Thread.sleep(2000);

WebElement addButton = driver.findElement(By.id("addbutton"));
addButton.click();
}
}

Configuring your Test Capabilities

Step 4: In the test script, you need to update your test capabilities. In this code, we are passing browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via capabilities object. The capabilities object in the above code are defined as:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("version", "70.0");
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
capabilities.setCapability("build", "LambdaTestSampleApp");
capabilities.setCapability("name", "LambdaTestJavaSample");
capabilities.setCapability("network", true); // To enable network logs
capabilities.setCapability("visual", true); // To enable step by step screenshot
capabilities.setCapability("video", true); // To enable video recording
capabilities.setCapability("console", true); // To capture console logs
Note

You can generate capabilities for your test requirements with the help of our inbuilt 🔗 Capabilities Generator Tool.

Executing the Test

Step 5: The tests can be executed in the terminal using either of the following commands:

mvn test

or

mvn clean install
info

Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest automation dashboard. LambdaTest Automation Dashboard will help you view all your text logs, screenshots and video recording for your entire automation tests.

Run Parallel Tests using Gauge


Gauge framework do support parallel test execution on the LambdaTest grid. To perform parallel testing, more than one browser specifications need to be updated in the env folder of the project. For instance, we have defined specifications for Chrome, Firefox, Safari and Edge in the below screenshot. Once the test is executed, all parallel browser specifications will hit on the LambdaTest grid and execute simultaneously to achieve parallelism.

cmd

In case you’d need to chuck out test for any browser, you would need to either delete the folder or comment out the code in it.

Executing Parallel Tests Using Gauge

To run parallel tests using Gauge, we would have to execute the same command we had used for single test which would be:

mvn test  

or

mvn clean install
info

Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest automation dashboard.

Testing Locally Hosted or Privately Hosted Projects


You can test your locally hosted or privately hosted projects with LambdaTest Selenium grid cloud using LambdaTest Tunnel app. All you would have to do is set up an SSH tunnel using LambdaTest Tunnel app and pass toggle tunnel = True via desired capabilities. LambdaTest Tunnel establishes a secure SSH protocol based tunnel that allows you in testing your locally hosted or privately hosted pages, even before they are made live.

Tunnel Help

Refer our 🔗 LambdaTest Tunnel documentation for more information.

Here’s how you can establish LambdaTest Tunnel.

Open command prompt and navigate to the binary folder.

Run the following command:

./LT -user {user’s login email} -key {user’s access key}

So if your user name is lambdatest@example.com, the command would be:

./LT -user lambdatest@example.com -key undefined

Once you are able to connect LambdaTest Tunnel successfully, you would just have to pass on tunnel capabilities in the code as shown:

Tunnel Capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("tunnel", true);