id | title | sidebar_label | description | keywords | image | url | site_name | slug | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
testng-framework |
TestNG with Selenium |
TestNG |
Now you can run your automation scripts using Selenium with TestNG on LambdaTest online grid of 3000+ real desktop browsers and real operating systems. |
|
/assets/images/og-images/TestNG-framework-Selenium.jpg |
LambdaTest |
testng-with-selenium-running-java-automation-scripts-on-lambdatest-selenium-grid/ |
import CodeBlock from '@theme/CodeBlock'; import {YOUR_LAMBDATEST_USERNAME, YOUR_LAMBDATEST_ACCESS_KEY} from "@site/src/component/keys";
<script type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify({ "@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{ "@type": "ListItem", "position": 1, "name": "Home", "item": "https://www.lambdatest.com" },{ "@type": "ListItem", "position": 2, "name": "Support", "item": "https://www.lambdatest.com/support/docs/" },{ "@type": "ListItem", "position": 3, "name": "Running Java Automation Scripts On Selenium Grid Using TestNG Framework", "item": "https://www.lambdatest.com/support/docs/testng-with-selenium-running-java-automation-scripts-on-lambdatest-selenium-grid/" }] }) }} ></script>import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
In this topic, you will learn how to configure and run your Java automation testing scripts on LambdaTest Selenium cloud platform using Java framework TestNG.
By the end of this topic, you will be able to:
- Set up an environment for testing your hosted web pages using TestNG framework with Selenium.
- Understand and configure the core capabilities required for your Selenium test suite.
- Run test cases in parallel using TestNG with Selenium to reduce build times.
- Test your locally hosted pages on LambdaTest platform.
- Explore advanced features of LambdaTest.
:::tip 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. <img loading="lazy" src={require('../assets/images/icons/github.png').default} alt="Image" className="doc_img"/> View on GitHub
:::
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 which supports TestNG framework out of the box. Maven 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.
Step 1: Clone the LambdaTest’s Java-TestNG-Selenium repository and navigate to the code directory as shown below:
git clone https://github.com/LambdaTest/Java-TestNG-Selenium
cd Java-TestNG-Selenium
You may also want to run the command below to check for outdated dependencies.
mvn versions:display-dependency-updates
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.
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class TestNGTodo{
public String username = "YOUR_LAMBDATEST_USERNAME";
public String accesskey = "YOUR_LAMBDATEST_ACCESS_KEY";
public static RemoteWebDriver driver = null;
public String gridURL = "@hub.lambdatest.com/wd/hub";
boolean status = false;
@BeforeClass
public void setUp() throws Exception {
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");
try {
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Test
public void testSimple() throws Exception {
try {//Change it to production page
driver.get("https://lambdatest.github.io/sample-todo-app/");
//Let's mark done first two items in the list.
driver.findElement(By.name("li1")).click();
driver.findElement(By.name("li2")).click();
// Let's add an item in the list.
driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list");
driver.findElement(By.id("addbutton")).click();
// Let's check that the item we added is added in the list.
String enteredText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText();
if (enteredText.equals("Yey, Let's add it to list")) {
status = true;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@AfterClass
public void tearDown() throws Exception {
if (driver != null) {
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
driver.quit();
}
}
}
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");
:::info Note
You can generate capabilities for your test requirements with the help of our inbuilt 🔗 Capabilities Generator Tool.
:::
Step 4: The tests can be executed in the terminal using the following command.
mvn test -D suite=single.xml
:::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.
:::
Here is an example 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 testcase which makes use of TestNG framework parameters (org.testng.annotations.Parameters
).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="3" name="LambaTestSuite" parallel="tests">
<test name="WIN8TEST">
<parameter name="browser" value="firefox"/>
<parameter name="version" value="62.0"/>
<parameter name="platform" value="WIN8"/>
<classes>
<class name="LambdaTest.TestNGToDo"/>
</classes>
</test> <!-- Test -->
<test name="WIN10TEST">
<parameter name="browser" value="chrome"/>
<parameter name="version" value="79.0"/>
<parameter name="platform" value="WIN10"/>
<classes>
<class name="LambdaTest.TestNGToDo"/>
</classes>
</test> <!-- Test -->
<test name="MACTEST">
<parameter name="browser" value="safari"/>
<parameter name="version" value="11.0"/>
<parameter name="platform" value="macos 10.13"/>
<classes>
<class name="LambdaTest.TestNGToDo"/>
</classes>
</test> <!-- Test -->
</suite>
To run parallel tests using TestNG, we would have to execute the below commands in the terminal:
- For the above example code
mvn test
- For the cloned Java-TestNG-Selenium repo used to run our first sample test
mvn test -D suite=parallel.xml
:::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.
:::
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.
:::tip Tunnel Help
Refer our 🔗 LambdaTest Tunnel documentation for more information.
:::
Here’s how you can establish LambdaTest Tunnel.
:::info Download the binary file
:::
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:
Once you are able to connect LambdaTest Tunnel successfully, you would just have to pass on tunnel capabilities in the code as shown:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("tunnel", true);