Selenide Automated Testing
TestingBot has a Selenide example GitHub project available. This contains an example on how to run Selenide tests with TestingBot.
Selenide is a Java-based test framework, capable of using Selenium WebDriver to test websites on (remote) browsers. You can use Selenide in combination with a test framework such as JUnit, TestNG, Cucumber and others. The syntax used by Selenide is short, it uses a concise API that makes your test code shorter and perhaps more readable.
To get started, let's go over all the necessary steps required to run your first test with TestNG and Selenide, using TestingBot's remote browsers.
Prerequisites
Let's start with making sure Java is available on your system. Starting with Selenide 7, at least Java 17 is required.
For Windows:- You can download Java for Windows from Java.com
- Run the installer and follow the setup wizard to install Java.
Java should already be present on macOS by default. But you can install JDK17 with Brew.
Set up pom.xml dependencies
Now we can specify which dependencies we need to be able to run a test. Create a new file called pom.xml
and make sure to define at least the following dependencies:
-
com.codeborne:selenide
org.testng:testng
org.seleniumhq.selenium:selenium-java
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>testingbot-testng-selenide</artifactId>
<groupId>com.testingbot</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testingbot_selenide_testng</name>
<description>An example project to run Selenide + TestNG tests on TestingBot's browser grid</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>7.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.24.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<parallel>classes</parallel>
<threadCount>40</threadCount>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
Define TestingBot driver
We need to define a base class to let Selenide know how to connect to the TestingBot browser grid.
package com.testingbotdemo.selenium.selenide.drivers;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import com.codeborne.selenide.WebDriverRunner;
public class TestingBotDriver {
private RemoteWebDriver driver;
@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {
String username = System.getenv("TESTINGBOT_KEY") == null ? "TESTINGBOT_KEY" : System.getenv("TESTINGBOT_KEY");
String accesskey = System.getenv("TESTINGBOT_SECRET") == null ? "TESTINGBOT_SECRET" : System.getenv("TESTINGBOT_SECRET");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("browserVersion", "latest");
capabilities.setCapability("platformName", "WIN10");
Map<String, Object> testingBotOptions = new HashMap<>();
testingBotOptions.put("name", "Selenide Example");
capabilities.setCapability("tb:options", testingBotOptions);
try {
String gridURL = "https://" + username + ":" + accesskey + "@hub.testingbot.com/wd/hub";
driver = new RemoteWebDriver(new URL(gridURL), capabilities);
WebDriverRunner.setWebDriver(driver);
} catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Invalid URL provided for TestingBot hub", e);
}
}
@AfterMethod(alwaysRun = true)
public void tearDown() throws Exception {
if (driver != null) {
driver.quit();
}
}
}
The DesiredCapabilities
are used to define which remote browser you want to use on TestingBot.
Test
Now we can create our first Selenide test. Create a new class that extends from TestingBotDriver
with the following contents:
package com.testingbotdemo.selenium.selenide.drivers;
import static com.codeborne.selenide.Selenide.*;
import static com.codeborne.selenide.Condition.*;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
public class ExampleTest extends TestingBotDriver {
@Test
public void test() throws Exception {
open("https://testingbot.com");
$("body").shouldHave(text("TestingBot"));
}
}
Run Selenide test
You can now run the Selenide test from the commandline:
The Selenide test will run on a remote TestingBot browser. Once the test has finished, you will be able to see the result, together with a video and logs in the TestingBot dashboard.
Specify Browsers & Devices
To let TestingBot know on which browser/platform/device you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.
Testing Internal Websites
We've built TestingBot Tunnel, to provide you with a secure way to run tests against your staged/internal webapps.
Please see our TestingBot Tunnel documentation for more information about this easy to use tunneling solution.
The example below shows how to easily run a TestNG test with our Tunnel:
1. Download our tunnel and start the tunnel:
2. Adjust your test: instead of pointing to 'hub.testingbot.com/wd/hub'
like the example above - change it to point to your tunnel's IP address.
Assuming you run the tunnel on the same machine you run your tests, change to 'localhost:4445/wd/hub'
. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.
This way your test will go securely through the tunnel to TestingBot and back:
package com.testingbotdemo.selenium.selenide.drivers;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import com.codeborne.selenide.WebDriverRunner;
public class TestingBotDriver {
private RemoteWebDriver driver;
@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {
String username = System.getenv("TESTINGBOT_KEY") == null ? "TESTINGBOT_KEY" : System.getenv("TESTINGBOT_KEY");
String accesskey = System.getenv("TESTINGBOT_SECRET") == null ? "TESTINGBOT_SECRET" : System.getenv("TESTINGBOT_SECRET");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("browserVersion", "latest");
capabilities.setCapability("platformName", "WIN10");
Map<String, Object> testingBotOptions = new HashMap<>();
testingBotOptions.put("name", "Selenide Example");
capabilities.setCapability("tb:options", testingBotOptions);
String gridURL = "http://localhost:4445/wd/hub";
driver = new RemoteWebDriver(new URL(gridURL), capabilities);
WebDriverRunner.setWebDriver(driver);
}
@AfterMethod(alwaysRun = true)
public void tearDown() throws Exception {
if (driver != null) {
driver.quit();
}
}
}
Run tests in Parallel
Parallel Testing means running the same test, or multiple tests, simultaneously. This greatly reduces your total testing time.
You can run the same tests on all different browser configurations or run different tests all on the same browser configuration.
TestingBot has a large grid of machines and browsers, which means you can use our service to do efficient parallel testing. It is one of the key features we provide to greatly cut down on your total testing time.
To run a test on different browsers at the same time, you can run the same mvn test
command multiple times in parallel, each with a different browser/capability environment variable.
For example, assuming you define the desired capabilities with environment variables:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", System.getenv("CAPABILITY_BROWSERNAME"));
capabilities.setCapability("browserVersion", System.getenv("CAPABILITY_BROWSERVERSION"));
capabilities.setCapability("platformName", System.getenv("CAPABILITY_PLATFORMNAME"));
Now you can run the same test on multiple browsers simultaneously:
mvn test -DCAPABILITY_BROWSERNAME=chrome -DCAPABILITY_BROWSERVERSION=latest -DCAPABILITY_PLATFORMNAME=WIN10
mvn test -DCAPABILITY_BROWSERNAME=safari -DCAPABILITY_BROWSERVERSION=latest -DCAPABILITY_PLATFORMNAME=SONOMA
Queuing
Every plan we provide comes with a limit of parallel tests.
If you exceed the number of parallel tests assigned to your account, TestingBot will queue the additional tests (for up to 6 minutes) and run the tests as soon as slots become available.
Mark tests as passed/failed
To see if a test passed or not in our member area, or to send additional meta-data to TestingBot, you can use the TestingBot API.
TestingBot has a Java client to facilitate interacting with the TestingBot API.
Once you've included this client with your tests, you will be able to send back the test status and other meta-data to TestingBot:
import com.testingbot.testingbotrest.TestingbotREST;
@After
public void tearDown() throws Exception {
TestingbotREST r = new TestingbotREST("key", "secret");
Map<String, String> data = new HashMap<String, String>();
data.put("success", "1");
data.put("name", "My Test");
r.updateTest(driver.getSessionId(), data);
driver.quit();
}
Other Java Framework examples
-
JUnit
JUnit is a unit testing framework for the Java programming language.
-
Parallel JUnit
By running multiple JUnit tests at the same time you can cut down on overall test time.
-
TestNG
TestNG is a framework similar to JUnit and NUnit, which supports some additional commands and features.
-
TestNG + Cucumber
Run tests with TestNG and BDD Cucumber.