Skip to main content

Selenium With Selenide Tutorial


In this topic, you will learn how to configure and run tests using Selenide on LambdaTest's Selenium testing cloud platform.

Objective


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

  1. Set up an environment for testing your hosted web pages using Selenide automation framework.
  2. Understand and configure the core capabilities required for your Selenium test suite.
  3. Run test cases in parallel using Selenide 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

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 selenide-testng-sample repository and navigate to the code directory as shown below:

git clone https://github.com/LambdaTest/selenide-testng-sample
cd selenide-testng-sample

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 2: Set LambdaTest Username and Access Key in environment variables.

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

You can also mention your LambdaTest credentials in each of the config files located at selenide-testng-sample/src/test/resources/conf/.

Run Your First Test


Sample Test with Selenide

Let’s checkout sample Selenide code running LambdaTest Selenium grid. This is a simple Selenide automation script similar to the script we have used in our Github repo.

Selenide To Do
package com.lambdatest;

import java.io.FileReader;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.sleep;
import static com.codeborne.selenide.Selenide.title;

import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.codeborne.selenide.WebDriverRunner;

public class LambdaTestSetup {
public RemoteWebDriver driver;
public String status="failed";

public static String username;
public static String accessKey;
public static String sessionId;

@BeforeMethod(alwaysRun = true)
@Parameters(value = { "config", "environment" })
public void setUp(String config_file, String environment) throws Exception {
JSONParser parser = new JSONParser();
JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
JSONObject envs = (JSONObject) config.get("environments");

DesiredCapabilities capabilities = new DesiredCapabilities();

Map<String, String> envCapabilities = (Map<String, String>) envs.get(environment);
Iterator it = envCapabilities.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
}

Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
it = commonCapabilities.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
if (capabilities.getCapability(pair.getKey().toString()) == null) {
capabilities.setCapability(pair.getKey().toString(),
(pair.getValue().toString().equalsIgnoreCase("true")
|| (pair.getValue().toString().equalsIgnoreCase("false"))
? Boolean.parseBoolean(pair.getValue().toString())
: pair.getValue().toString()));
}
}
capabilities.setCapability("name", this.getClass().getName());

username = System.getenv("LT_USERNAME");
if (username == null) {
username = (String) config.get("user");
}

accessKey = System.getenv("LT_ACCESS_KEY");
if (accessKey == null) {
accessKey = (String) config.get("key");
}

driver = new RemoteWebDriver(
new URL("http://" + username + ":" + accessKey + "@" + config.get("server") + "/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
sessionId = driver.getSessionId().toString();

WebDriverRunner.setWebDriver(driver);
}

@Test
public void test() throws Exception {

open("http://www.google.co.uk");

$(By.name("q")).setValue("LambdaTest").pressEnter();

sleep(2000);

Assert.assertEquals(title(), "LambdaTest - Google Search");

status = "passed";
}

@AfterMethod(alwaysRun = true)
public void tearDown() throws Exception {
driver.executeScript("lambda-status="+status);
driver.quit();
}

}

Configuring your Test Capabilities

Step 3: 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 4: Install the mandatory Selenium dependencies for Maven by running the below command:

mvn compile

Step 5: The tests can be executed in the terminal using the following command:

mvn test -P single

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 Selenide


Setting up the Parallel Environment

Here is an example of parallel.testng.xml file which would help you to run a single test on various browsers at the same time, you would also need to generate a config file parallel.config.json to define capabilities of the browsers.

parallel.testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel" thread-count="3" parallel="tests">
<test name="SingleTestChrome">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="chrome"/>
<classes>
<class name="com.lambdatest.SingleTest"/>
</classes>
</test>

<test name="SingleTestFirefox">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="firefox"/>
<classes>
<class name="com.lambdatest.SingleTest"/>
</classes>
</test>

<test name="SingleTestSafari">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="safari"/>
<classes>
<class name="com.lambdatest.SingleTest"/>
</classes>
</test>
</suite>
parallel.config.json
{
"server": "hub.lambdatest.com",
"user": "YOUR_USERNAME",
"key": "YOUR_ACCESS_KEY",

"capabilities": {
"build": "Java Selenide Parallel"
},

"environments": {
"chrome": {
"platform": "windows 10",
"browserName": "chrome",
"version": "latest"
},
"firefox": {
"platform": "windows 8.1",
"browserName": "firefox",
"version": "latest"
},
"safari": {
"platform": "macOS Mojave",
"browserName": "safari",
"version": "latest"
}
}
}

Executing Parallel Tests Using Selenide

To run parallel tests using Selenide, we would have to execute the below command in the terminal:

mvn test -P parallel
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);

Test across 3000+ combinations of browsers, real devices & OS.

Book Demo

Help and Support

Related Articles