Selenium Testing With Geb Tutorial
In this topic, you will learn how to configure and run your Java automation testing scripts on LambdaTest Selenium cloud platform using Java framework Geb.
Objective
By the end of this topic, you will be able to:
- Set up an environment for testing your hosted web pages using Geb framework with Selenium.
- Understand and configure the core capabilities required for your Selenium test suite.
- Run test cases in parallel using Geb with Selenium to reduce build times.
- Test your locally hosted pages on LambdaTest platform.
- Explore advanced features of LambdaTest.
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. View on GitHub
Prerequisites
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.
Cloning Repo and Installing Dependencies
Step 1: Clone the LambdaTest’s geb-parallel-sample-code repository and navigate to the code directory as shown below:
git clone https://github.com/LambdaTest/geb-parallel-sample-code
cd geb-parallel-sample-code
Step 2: Compile Maven dependencies using the below command:
mvn compile
You may also want to run the command below to check for outdated dependencies.
mvn versions:display-dependency-updates
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.
- Linux / MacOS
- Windows
export LT_USERNAME="undefined" \
export LT_ACCESS_KEY="undefined"
set LT_USERNAME="undefined" `
set LT_ACCESS_KEY="undefined"
Run Your First Test
Configuring your Test Capabilities
Step 4: In capabilities.json
file, you need to update your test capabilities. Here, 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:
{
"build": "GebFirstTest",
"platform": "Windows 8",
"browserName": "Mozilla",
"version": "65.0"
}
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 from the root of the project using the following commands:
./run.sh
mvn -Dlambdageb.capabilities=capabilities clean test
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.
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.
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": true,