Skip to content

Commit f0b8bb8

Browse files
selenium -java
1 parent 01ff016 commit f0b8bb8

File tree

5 files changed

+56
-218
lines changed

5 files changed

+56
-218
lines changed

assets/images/selenium/java1.png

-103 KB
Binary file not shown.

assets/images/selenium/java2.png

-67.8 KB
Binary file not shown.
101 KB
Loading

docs/java-frameworks.md

Lines changed: 55 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: java-framework
3-
title: Execute Selenium Tests With Java
3+
title: Run your Selenium Java tests on LambdaTest
44
sidebar_label: Java
55
description: Your guide to running tests using Java on LambdaTest's Selenium Grid of 3000+ real devices and desktop browsers.
66
keywords:
@@ -44,250 +44,88 @@ import TabItem from '@theme/TabItem';
4444
})
4545
}}
4646
></script>
47-
48-
# Selenium With Java Tutorial
49-
50-
---
51-
52-
In this topic, you will learn how to configure and run tests using **Java** on LambdaTest's [Selenium testing cloud platform](https://www.lambdatest.com/selenium-automation).
53-
54-
## Objectives
55-
56-
---
57-
58-
By the end of this topic, you will be able to:
59-
60-
1. Set up an environment for testing your hosted web pages using **Java** with Selenium.
61-
2. Specify which browsers to perform **Java** automation testing on.
62-
3. Test your locally hosted pages on LambdaTest platform.
63-
4. Explore advanced features of LambdaTest.
47+
This guide walks you through the process of running Selenium Java 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.
6448

6549
## Prerequisites
50+
Before you begin, ensure you have the following:
6651

67-
---
68-
69-
Before you can start performing Java automation testing with Selenium, you would need to:
70-
71-
- Install the latest **Java development environment**. We recommend to use **Java 11** version.
72-
73-
- 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.
74-
75-
- Setup your environment with required Selenium bindings if you are using an IDE for running the tests. These are the steps required to configure your IDE :
76-
77-
- **Step 1:** Download the latest Java Selenium Bindings from the [official website](https://www.selenium.dev/downloads/) and extract the **ZIP** file to your project directory.
78-
- **Step 2:** Create a new Java project and once it's created, you can open the project settings. Here we are showing an example for the same in **IntelliJ IDEA CE**.
79-
<img loading="lazy" src={require('../assets/images/selenium/java1.png').default} alt="cmd" width="1260" height="1071" className="doc_img"/>
80-
81-
- **Step 3:** Navigate to **dependencies** in module settings where you can add your external JARs.
82-
- **Step 4:** Under dependencies by clicking the **`+`** icon, you can add your downloaded **Selenium JARs** to the project. Once they are added, it should look like this :
83-
<img loading="lazy" src={require('../assets/images/selenium/java2.png').default} alt="cmd" width="1150" height="740" className="doc_img"/>
84-
85-
Now you are good to run the Java automation testing scripts.
86-
87-
## Run Your First Test
52+
- Your [LambdaTest Username and Access Key](https://accounts.lambdatest.com/)
53+
- Install Java Development Kit (JDK). We recommend Java version 11
54+
- Install [Maven](https://maven.apache.org/)
55+
- [Download](https://www.selenium.dev/downloads/) the latest Selenium Client and its WebDriver bindings
8856

89-
---
90-
91-
Let’s start with a simple Selenium Remote WebDriver test first. The Java script below tests a simple to-do application with basic functionalities like mark items as done, add items in a list, calculate total pending items etc.
92-
93-
### Sample Test with Java
94-
95-
```java title="JavaToDo.java"
96-
import java.net.MalformedURLException;
97-
import java.net.URL;
98-
import org.openqa.selenium.By;
99-
import org.openqa.selenium.JavascriptExecutor;
100-
import org.openqa.selenium.remote.DesiredCapabilities;
101-
import org.openqa.selenium.remote.RemoteWebDriver;
102-
public class JavaTodo {
103-
String username = "YOUR_LAMBDATEST_USERNAME";
104-
String accesskey = "YOUR_LAMBDATEST_ACCESS_KEY";
105-
static RemoteWebDriver driver = null;
106-
String gridURL = "@hub.lambdatest.com/wd/hub";
107-
boolean status = false;
108-
public static void main(String[] args) {
109-
new JavaTodo().test();
110-
}
111-
public void test() {
112-
// To Setup driver
113-
setUp();
114-
try {
115-
//Change it to production page
116-
driver.get("https://lambdatest.github.io/sample-todo-app/");
117-
118-
//Let's mark done first two items in the list.
119-
driver.findElement(By.name("li1")).click();
120-
driver.findElement(By.name("li2")).click();
121-
122-
// Let's add an item in the list.
123-
driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list");
124-
driver.findElement(By.id("addbutton")).click();
125-
126-
// Let's check that the item we added is added in the list.
127-
String enteredText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText();
128-
if (enteredText.equals("Yey, Let's add it to list")) {
129-
status = true;
130-
}
131-
} catch (Exception e) {
132-
System.out.println(e.getMessage());
133-
} finally {
134-
tearDown();
135-
}
136-
}
137-
private void setUp() {
138-
DesiredCapabilities capabilities = new DesiredCapabilities();
139-
capabilities.setCapability("browserName", "chrome");
140-
capabilities.setCapability("version", "70.0");
141-
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get any available one.
142-
capabilities.setCapability("build", "LambdaTestSampleApp");
143-
capabilities.setCapability("name", "LambdaTestJavaSample");
144-
try {
145-
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
146-
} catch (MalformedURLException e) {
147-
System.out.println("Invalid grid URL");
148-
} catch (Exception e) {
149-
System.out.println(e.getMessage());
150-
}
151-
}
152-
private void tearDown() {
153-
if (driver != null) {
154-
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
155-
driver.quit(); //really important statement for preventing your test execution from a timeout.
156-
}
157-
}
158-
}
159-
```
160-
161-
**Step 1:** Once you setup your environment with the latest Java Selenium bindings, create a new java file `<file_name>.java` in your current project or testing directory and add the above code snippet.
57+
## Step 1: Configure your test suite
16258

163-
### Setting up your Authentication
164-
165-
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).
166-
167-
**Step 2:** Please edit and add your **UserName** and **AccessKey** which are generated from the platform in your `<file_name>.java` file:
168-
169-
<div className="lambdatest__codeblock">
170-
<CodeBlock className="language-java">
171-
{`String username= "${ YOUR_LAMBDATEST_USERNAME()}";
172-
String accesskey= "${ YOUR_LAMBDATEST_ACCESS_KEY()}";`}
173-
</CodeBlock>
174-
</div>
175-
176-
### Configuring your Test Capabilities
177-
178-
**Step 3:** 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 is defined as:
179-
180-
```java
181-
DesiredCapabilities capabilities = new DesiredCapabilities();
182-
capabilities.setCapability("browserName", "chrome");
183-
capabilities.setCapability("version", "70.0");
184-
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
185-
capabilities.setCapability("build", "LambdaTestSampleApp");
186-
capabilities.setCapability("name", "LambdaTestJavaSample");
187-
```
188-
189-
:::info Note
190-
191-
You can generate capabilities for your test requirements with the help of our inbuilt :link: **[Capabilities Generator tool](https://www.lambdatest.com/capabilities-generator/)**.
59+
:::tip Sample repo
60+
Download or Clone the code sample for the Java from the LambdaTest GitHub repository to run the tests on our Standard Grid.
19261

62+
<a href="https://github.com/LambdaTest/java-selenium-sample" 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>
19363
:::
19464

195-
### Executing the Test
196-
197-
- If you are using an editor or IDE for running your tests, you can just build and run your configured java file in your editor/IDE.
198-
- If you are using a terminal/cmd, you would need to execute the following commands :
199-
20065
```bash
201-
cd to/file/location
202-
#Compile the test file:
203-
javac -classpath ".:/path/to/selenium/jarfile:" <file_name>.java
204-
#Run the test:
205-
java -classpath ".:/path/to/selenium/jarfile:" <file_name>
66+
git clone https://github.com/LambdaTest/java-selenium-sample.git
67+
cd java-selenium-sample
20668
```
20769

208-
**Example:**
70+
## Step 2: Update the dependencies
71+
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.
20972

21073
```bash
211-
cd /Users/macuser/Documents/LambdaTest_Java
212-
javac -classpath ".:/Users/macuser/Documents/LambdaTest_Java/selenium-server-4.1.1.jar:" JavaTodo.java
213-
java -classpath ".:/Users/macuser/Documents/LambdaTest_Java/selenium-server-4.1.1.jar:" JavaTodo
74+
mvn versions:display-dependency-updates
21475
```
21576

216-
:::info
217-
218-
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://automation.lambdatest.com/build). LambdaTest Automation Dashboard will help you view all your text logs, screenshots and video recording for your entire automation tests.
219-
220-
:::
221-
222-
## Testing Locally Hosted or Privately Hosted Projects
223-
224-
---
225-
226-
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.
227-
228-
:::tip Tunnel Help
229-
230-
Refer our :link: [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) for more information.
231-
232-
:::
233-
234-
Here’s how you can establish LambdaTest Tunnel.
77+
## Step 3: Configure your test Capabilities
78+
LambdaTest requires specific capabilities to set the browser, browser version, operating system, and other configurations for your test.
23579

236-
:::info Download the binary file
80+
Example desired capabilities for testing on Chrome 120:
23781

238-
- [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip)
239-
- [LambdaTest Tunnel for Mac](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip)
240-
- [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip)
82+
```java title="Test.java"
83+
DesiredCapabilities capabilities = new DesiredCapabilities();
84+
capabilities.setCapability("browserName", "chrome");
85+
capabilities.setCapability("version", "120.0");
86+
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
87+
capabilities.setCapability("build", "LambdaTestSeleniumSampleApp");
88+
capabilities.setCapability("name", "LambdaTestJavaSample");
89+
```
24190

91+
:::tip
92+
You can generate capabilities for your test requirements with the help of our inbuilt :link: **[Capabilities Generator tool](https://www.lambdatest.com/capabilities-generator/)**.
24293
:::
24394

244-
Open command prompt and navigate to the binary folder.
95+
## Step 4: Setup your LambdaTest credentials
96+
In your terminal (as per your respective Operating System), run these command to setup your LambdaTest credentials.
97+
> You can see your credentials below if you have logged into our platform.
24598
246-
Run the following command:
247-
248-
```bash
249-
./LT -user {user’s login email} -key {user’s access key}
250-
```
251-
252-
So if your user name is **[email protected]**, the command would be:
253-
254-
<div className="lambdatest__codeblock">
99+
<Tabs className="docs__val">
100+
<TabItem value="bash" label="Linux / MacOS" default>
101+
<div className="lambdatest__codeblock">
255102
<CodeBlock className="language-bash">
256-
{`./LT -user [email protected] -key ${ YOUR_LAMBDATEST_ACCESS_KEY()}`}
103+
{`export LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}"
104+
export LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`}
257105
</CodeBlock>
258106
</div>
107+
</TabItem>
259108

260-
Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code as shown:
109+
<TabItem value="powershell" label="Windows" default>
110+
<div className="lambdatest__codeblock">
111+
<CodeBlock className="language-powershell">
112+
{`set LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}"
113+
set LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`}
114+
</CodeBlock>
115+
</div>
116+
</TabItem>
117+
</Tabs>
261118

262-
```java title="Tunnel Capability"
263-
DesiredCapabilities capabilities = new DesiredCapabilities();
264-
capabilities.setCapability("tunnel", true);
119+
## Step 5: Execute your test
120+
Replace the `TEST_FILE_NAME` in the below command with your desired test file to execute that particular test on LambdaTest Grid:
121+
```bash
122+
mvn clean install exec:java -Dexec.mainClass="com.lambdatest.TEST_FILE_NAME" -Dexec.classpathScope=test -e
265123
```
266124

267-
## Additional Links
268-
269-
---
125+
Visit the [LambdaTest Web Automation](https://automation.lambdatest.com/build) page to check the status of your test execution.
126+
<img loading="lazy" src={require('../assets/images/selenium/language-frameworks/java/1.png').default} alt="Image" className="doc_img"/>
270127

128+
## Additional Links
271129
- [Advanced Configuration for Capabilities](https://www.lambdatest.com/support/docs/selenium-automation-capabilities/)
272130
- [How to test locally hosted apps](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/)
273-
- [How to integrate LambdaTest with CI/CD](https://www.lambdatest.com/support/docs/integrations-with-ci-cd-tools/)
274-
275-
<nav aria-label="breadcrumbs">
276-
<ul className="breadcrumbs">
277-
<li className="breadcrumbs__item">
278-
<a className="breadcrumbs__link" target="_self" href="https://www.lambdatest.com">
279-
Home
280-
</a>
281-
</li>
282-
<li className="breadcrumbs__item">
283-
<a className="breadcrumbs__link" target="_self" href="https://www.lambdatest.com/support/docs/">
284-
Support
285-
</a>
286-
</li>
287-
<li className="breadcrumbs__item breadcrumbs__item--active">
288-
<span className="breadcrumbs__link">
289-
Java Automation Testing
290-
</span>
291-
</li>
292-
</ul>
293-
</nav>
131+
- [How to integrate LambdaTest with CI/CD](https://www.lambdatest.com/support/docs/integrations-with-ci-cd-tools/)

docs/lambdatest-running-your-first-selenium-test.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ LambdaTest provides a powerful Selenium Grid that allows you to perform automate
4848
## Prerequisites
4949
Before you begin, ensure you have the following:
5050

51-
- Your [LambdaTest Username and Access Key](https://accounts.lambdatest.com/security)
51+
- Your [LambdaTest Username and Access Key](https://accounts.lambdatest.com/)
5252
- Install Java Development Kit (JDK). We recommend Java version 11
5353
- Install [Maven](https://maven.apache.org/)
5454
- [Download](https://www.selenium.dev/downloads/) the latest Selenium Client and its WebDriver bindings

0 commit comments

Comments
 (0)