Skip to content

Commit 2429ad8

Browse files
selenium - testng updates
1 parent 2bcbb56 commit 2429ad8

File tree

2 files changed

+66
-236
lines changed

2 files changed

+66
-236
lines changed

docs/java-frameworks.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,7 @@ cd java-selenium-sample
7070
If you are using your own project, make sure you update the **Hub endpoint** in your tests file. By setting up the Hub endpoint, you establish the communication channel between your tests and the browser nodes, enabling effective test distribution and execution.
7171

7272
```java title="Test.java"
73-
// highlight-next-line
7473
public static String hubURL = "https://hub.lambdatest.com/wd/hub";
75-
private WebDriver driver;
76-
public void setup() throws MalformedURLException {
77-
//... your capabilities
78-
// highlight-next-line
79-
driver = new RemoteWebDriver(new URL(hubURL), capabilities);
80-
}
8174
```
8275

8376
## Step 2: Update the dependencies

docs/testng-framework.md

Lines changed: 66 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: testng-framework
3-
title: Execute Selenium Tests With TestNG
3+
title: Run your Selenium TestNG tests on LambdaTest
44
sidebar_label: TestNG
55
description: Your guide to running tests using TestNG on LambdaTest's Selenium Grid of 3000+ real devices and desktop browsers.
66
keywords:
@@ -21,6 +21,8 @@ slug: testng-with-selenium-running-java-automation-scripts-on-lambdatest-seleniu
2121

2222
import CodeBlock from '@theme/CodeBlock';
2323
import {YOUR_LAMBDATEST_USERNAME, YOUR_LAMBDATEST_ACCESS_KEY} from "@site/src/component/keys";
24+
import Tabs from '@theme/Tabs';
25+
import TabItem from '@theme/TabItem';
2426

2527
<script type="application/ld+json"
2628
dangerouslySetInnerHTML={{ __html: JSON.stringify({
@@ -45,198 +47,112 @@ import {YOUR_LAMBDATEST_USERNAME, YOUR_LAMBDATEST_ACCESS_KEY} from "@site/src/co
4547
})
4648
}}
4749
></script>
50+
This guide walks you through the process of running Selenium TestNG tests on LambdaTest, a cloud-based cross-browser testing platform. By following these steps, you can seamlessly execute automated tests on a wide range of browsers and operating systems using LambdaTest’s Selenium Grid.
4851

49-
import Tabs from '@theme/Tabs';
50-
import TabItem from '@theme/TabItem';
51-
52-
# Selenium With TestNG Tutorial
53-
54-
---
55-
56-
In this topic, you will learn how to configure and run tests using **TestNG** on LambdaTest's [Selenium testing cloud platform](https://www.lambdatest.com/selenium-automation).
57-
58-
## Objectives
59-
60-
---
52+
## Prerequisites
53+
Before you begin, ensure you have the following:
6154

62-
By the end of this topic, you will be able to:
55+
- Your [LambdaTest Username and Access Key](https://accounts.lambdatest.com/)
56+
- Install Java Development Kit (JDK). We recommend Java version 11
57+
- Install [Maven](https://maven.apache.org/)
58+
- [Download](https://www.selenium.dev/downloads/) the latest Selenium Client and its WebDriver bindings
6359

64-
1. Set up an environment for testing your hosted web pages using **TestNG** framework with **Selenium**.
65-
2. Understand and configure the core capabilities required for your Selenium test suite.
66-
3. Run test cases in parallel using **TestNG** with Selenium to reduce build times.
67-
4. Test your locally hosted pages on LambdaTest platform.
68-
5. Explore advanced features of LambdaTest.
60+
## Step 1: Configure your test suite
6961

7062
:::tip Sample repo
63+
Download or Clone the code sample for the TestNG from the LambdaTest GitHub repository to run the tests on our Standard Grid.
7164

72-
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. <a href="https://github.com/LambdaTest/Java-TestNG-Selenium" className="github__anchor"><img loading="lazy" src={require('../assets/images/icons/github.png').default} alt="Image" className="doc_img"/> View on GitHub</a>
73-
65+
<a href="https://github.com/LambdaTest/Java-TestNG-Selenium" className="github__anchor" target="_blank"><img loading="lazy" src={require('../assets/images/icons/github.png').default} alt="Image" className="doc_img"/> View on GitHub</a>
7466
:::
7567

76-
## Prerequisites
77-
78-
---
79-
80-
Before you can start performing Java automation testing with Selenium, you would need to:
81-
82-
- Install the latest **Java development environment**. We recommend to use **Java 11** version.
83-
84-
- Download the latest **Selenium Client** and its **WebDriver bindings** from the [official website](https://www.selenium.dev/downloads/). Latest versions of Selenium Client and WebDriver are ideal for running your automation script on LambdaTest Selenium cloud grid.
85-
86-
- Install **Maven** which supports **TestNG** framework out of the box. **Maven** can be downloaded and installed following the steps from [the official website](https://maven.apache.org/). Maven can also be installed easily on **Linux/MacOS** using [**Homebrew**](https://brew.sh/) package manager.
87-
88-
### Cloning Repo and Installing Dependencies
89-
90-
**Step 1:** Clone the LambdaTest’s [Java-TestNG-Selenium](https://github.com/LambdaTest/Java-TestNG-Selenium) repository and navigate to the code directory as shown below:
91-
9268
```bash
93-
git clone https://github.com/LambdaTest/Java-TestNG-Selenium
69+
git clone https://github.com/LambdaTest/Java-TestNG-Selenium.git
9470
cd Java-TestNG-Selenium
9571
```
9672

97-
You may also want to run the command below to check for outdated dependencies.
73+
If you are using your own project, make sure you update the **Hub endpoint** in your tests file. By setting up the Hub endpoint, you establish the communication channel between your tests and the browser nodes, enabling effective test distribution and execution.
74+
75+
```java title="Test.java"
76+
public static String hubURL = "https://hub.lambdatest.com/wd/hub";
77+
```
78+
79+
## Step 2: Update the dependencies
80+
Run the command below to check for outdated dependencies. Review updates carefully before modifying your `pom.xml`, as they might not be compatible with your code.
9881

9982
```bash
10083
mvn versions:display-dependency-updates
10184
```
10285

103-
### Setting up your Authentication
86+
## Step 3: Configure your test Capabilities
87+
LambdaTest requires specific capabilities to set the browser, browser version, operating system, and other configurations for your test.
10488

105-
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](https://automation.lambdatest.com/build) or through [LambdaTest Profile](https://accounts.lambdatest.com/login).
89+
Example desired capabilities for testing on Chrome 120:
10690

107-
**Step 2:** Set LambdaTest **Username** and **Access Key** in environment variables.
91+
```java title="Test.java"
92+
DesiredCapabilities capabilities = new DesiredCapabilities();
93+
capabilities.setCapability("browserName", "chrome");
94+
capabilities.setCapability("version", "120.0");
95+
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
96+
capabilities.setCapability("build", "LambdaTestSampleApp");
97+
capabilities.setCapability("name", "LambdaTestJavaSample");
98+
```
10899

109-
<Tabs className="docs__val">
100+
:::tip
101+
You can generate capabilities for your test requirements with the help of our inbuilt :link: **[Capabilities Generator tool](https://www.lambdatest.com/capabilities-generator/)**.
102+
:::
110103

111-
<TabItem value="bash" label="Linux / MacOS" default>
104+
## Step 4: Setup your LambdaTest credentials
105+
In your terminal (as per your respective Operating System), run these command to setup your LambdaTest credentials.
106+
> You can see your credentials below if you have logged into our platform.
112107
108+
<Tabs className="docs__val">
109+
<TabItem value="bash" label="Linux / MacOS" default>
113110
<div className="lambdatest__codeblock">
114111
<CodeBlock className="language-bash">
115-
{`export LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}" \\
112+
{`export LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}"
116113
export LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`}
117114
</CodeBlock>
118115
</div>
119-
120116
</TabItem>
121117

122118
<TabItem value="powershell" label="Windows" default>
123-
124119
<div className="lambdatest__codeblock">
125120
<CodeBlock className="language-powershell">
126-
{`set LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}" \`
121+
{`set LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}"
127122
set LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`}
128123
</CodeBlock>
129124
</div>
130-
131125
</TabItem>
132126
</Tabs>
133127

134-
## Run Your First Test
128+
## Step 5: Execute your test
129+
If you are using the sample code repository (provided above), then use the below mentioned command to trigger your tests:
135130

136-
---
137-
138-
### Sample Test with TestNG
139-
140-
```java title="TestNG To Do"
141-
import org.openqa.selenium.By;
142-
import org.openqa.selenium.Platform;
143-
import org.openqa.selenium.WebDriver;
144-
import org.openqa.selenium.remote.DesiredCapabilities;
145-
import org.openqa.selenium.remote.RemoteWebDriver;
146-
import org.testng.Assert;
147-
import org.testng.annotations.AfterClass;
148-
import org.testng.annotations.BeforeClass;
149-
import org.testng.annotations.Test;
150-
import java.net.MalformedURLException;
151-
import java.net.URL;
152-
public class TestNGTodo{
153-
public String username = "YOUR_LAMBDATEST_USERNAME";
154-
public String accesskey = "YOUR_LAMBDATEST_ACCESS_KEY";
155-
public static RemoteWebDriver driver = null;
156-
public String gridURL = "@hub.lambdatest.com/wd/hub";
157-
boolean status = false;
158-
@BeforeClass
159-
public void setUp() throws Exception {
160-
DesiredCapabilities capabilities = new DesiredCapabilities();
161-
capabilities.setCapability("browserName", "chrome");
162-
capabilities.setCapability("version", "70.0");
163-
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
164-
capabilities.setCapability("build", "LambdaTestSampleApp");
165-
capabilities.setCapability("name", "LambdaTestJavaSample");
166-
try {
167-
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
168-
} catch (MalformedURLException e) {
169-
System.out.println("Invalid grid URL");
170-
} catch (Exception e) {
171-
System.out.println(e.getMessage());
172-
}
173-
}
174-
@Test
175-
public void testSimple() throws Exception {
176-
try {//Change it to production page
177-
driver.get("https://lambdatest.github.io/sample-todo-app/");
178-
//Let's mark done first two items in the list.
179-
driver.findElement(By.name("li1")).click();
180-
driver.findElement(By.name("li2")).click();
181-
// Let's add an item in the list.
182-
driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list");
183-
driver.findElement(By.id("addbutton")).click();
184-
// Let's check that the item we added is added in the list.
185-
String enteredText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText();
186-
if (enteredText.equals("Yey, Let's add it to list")) {
187-
status = true;
188-
}
189-
} catch (Exception e) {
190-
System.out.println(e.getMessage());
191-
}
192-
}
193-
@AfterClass
194-
public void tearDown() throws Exception {
195-
if (driver != null) {
196-
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
197-
driver.quit();
198-
}
199-
}
200-
}
131+
<Tabs className="docs__val" queryString="testType">
132+
<TabItem value="single" label="Run single tests" default>
133+
```xml reference
134+
https://github.com/LambdaTest/java-testng-selenium/blob/master/single.xml
201135
```
202136

203-
### Configuring your Test Capabilities
204-
205-
**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:
206-
207-
```java
208-
DesiredCapabilities capabilities = new DesiredCapabilities();
209-
capabilities.setCapability("browserName", "chrome");
210-
capabilities.setCapability("version", "70.0");
211-
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
212-
capabilities.setCapability("build", "LambdaTestSampleApp");
213-
capabilities.setCapability("name", "LambdaTestJavaSample");
214-
```
215-
> You can also use **dev** and **beta** browser versions. To generate capabilities for your test requirements, you can use our inbuilt 🔗 [Capabilities Generator Tool](https://www.lambdatest.com/capabilities-generator/).
216-
217-
### Executing the Test
218-
219-
**Step 4:** The tests can be executed in the terminal using the following command.
220-
221137
```bash
222138
mvn test -D suite=single.xml
223139
```
140+
</TabItem>
224141

225-
:::info
226-
227-
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](https://accounts.lambdatest.com/login). LambdaTest Automation Dashboard will help you view all your text logs, screenshots and video recording for your entire automation tests.
228-
229-
:::
230-
231-
## Run Parallel Tests Using TestNG
232-
233-
---
142+
<TabItem value="parallel" label="Run parallel tests" default>
143+
```xml reference
144+
https://github.com/LambdaTest/java-testng-selenium/blob/master/parallel.xml
145+
```
234146

235-
### Setting up the Parallel Environment
147+
```bash
148+
mvn test -D suite=parallel.xml
149+
```
150+
</TabItem>
236151

237-
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`).
152+
<TabItem value="single-parallel" label="Single test on multiple browsers" default>
153+
Here is an example xml file which would help you to run a single test on various browsers at the same time:
238154

239-
```xml title="testng.xml"
155+
```xml
240156
<?xml version="1.0" encoding="UTF-8"?>
241157
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
242158
<suite thread-count="3" name="LambaTestSuite" parallel="tests">
@@ -269,93 +185,14 @@ Here is an example `xml` file which would help you to run a single test on vario
269185

270186
</suite>
271187
```
188+
</TabItem>
272189

273-
### Executing Parallel Tests using TestNG
274-
275-
To run parallel tests using **TestNG**, we would have to execute the below commands in the terminal:
276-
277-
- For the above example code
278-
```bash
279-
mvn test
280-
```
281-
- For the cloned Java-TestNG-Selenium repo used to run our first sample test
282-
```bash
283-
mvn test -D suite=parallel.xml
284-
```
285-
286-
:::info
287-
288-
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](https://accounts.lambdatest.com/login).
289-
290-
:::
291-
292-
## Testing Locally Hosted or Privately Hosted Projects
293-
294-
---
295-
296-
You can test your locally hosted or privately hosted projects with [LambdaTest Selenium grid cloud](https://www.lambdatest.com/selenium-automation) 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.
297-
298-
:::tip Tunnel Help
299-
300-
Refer our :link: [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) for more information.
301-
302-
:::
303-
304-
Here’s how you can establish LambdaTest Tunnel.
305-
306-
:::info Download the binary file
307-
308-
- [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip)
309-
- [LambdaTest Tunnel for Mac](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip)
310-
- [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip)
311-
312-
:::
313-
314-
Open command prompt and navigate to the binary folder.
315-
316-
Run the following command:
317-
318-
```bash
319-
./LT -user {user’s login email} -key {user’s access key}
320-
```
321-
322-
So if your user name is **[email protected]**, the command would be:
323-
324-
<div className="lambdatest__codeblock">
325-
<CodeBlock className="language-bash">
326-
{`./LT -user [email protected] -key ${ YOUR_LAMBDATEST_ACCESS_KEY()}`}
327-
</CodeBlock>
328-
</div>
190+
</Tabs>
329191

330-
Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code as shown:
331-
332-
```java title="Tunnel Capability"
333-
DesiredCapabilities capabilities = new DesiredCapabilities();
334-
capabilities.setCapability("tunnel", true);
335-
```
192+
Visit the [LambdaTest Web Automation](https://automation.lambdatest.com/build) page to check the status of your test execution.
193+
<img loading="lazy" src={require('../assets/images/selenium/language-frameworks/java/1.png').default} alt="Image" className="doc_img"/>
336194

337195
## Additional Links
338-
339-
---
340-
341196
- [Advanced Configuration for Capabilities](https://www.lambdatest.com/support/docs/selenium-automation-capabilities/)
342197
- [How to test locally hosted apps](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/)
343-
- [How to integrate LambdaTest with CI/CD](https://www.lambdatest.com/support/docs/integrations-with-ci-cd-tools/)
344-
345-
<nav aria-label="breadcrumbs">
346-
<ul className="breadcrumbs">
347-
<li className="breadcrumbs__item">
348-
<a className="breadcrumbs__link" target="_self" href="https://www.lambdatest.com">
349-
Home
350-
</a>
351-
</li>
352-
<li className="breadcrumbs__item">
353-
<a className="breadcrumbs__link" target="_self" href="https://www.lambdatest.com/support/docs/">
354-
Support
355-
</a>
356-
</li>
357-
<li className="breadcrumbs__item breadcrumbs__item--active">
358-
<span className="breadcrumbs__link"> TestNG Automation Testing </span>
359-
</li>
360-
</ul>
361-
</nav>
198+
- [How to integrate LambdaTest with CI/CD](https://www.lambdatest.com/support/docs/integrations-with-ci-cd-tools/)

0 commit comments

Comments
 (0)