diff --git a/docs/java-frameworks.md b/docs/java-frameworks.md index 612db52f0..85c4e99d8 100644 --- a/docs/java-frameworks.md +++ b/docs/java-frameworks.md @@ -62,7 +62,7 @@ Download or Clone the code sample for the Java from the LambdaTest GitHub reposi Image View on GitHub ::: -```bash +```bash title="terminal" git clone https://github.com/LambdaTest/java-selenium-sample.git cd java-selenium-sample ``` @@ -76,10 +76,14 @@ public static String hubURL = "https://hub.lambdatest.com/wd/hub"; ## Step 2: Update the dependencies 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. -```bash +```bash title="terminal" mvn versions:display-dependency-updates ``` +```xml reference +https://github.com/LambdaTest/java-selenium-sample/blob/main/pom.xml +``` + ## Step 3: Configure your test Capabilities LambdaTest requires specific capabilities to set the browser, browser version, operating system, and other configurations for your test. @@ -95,7 +99,7 @@ capabilities.setCapability("name", "LambdaTestJavaSample"); ``` :::tip -You can generate capabilities for your test requirements with the help of our inbuilt :link: **[Capabilities Generator tool](https://www.lambdatest.com/capabilities-generator/)**. +Use our [Capability Generator](https://www.lambdatest.com/capabilities-generator/) to select from a wide range of options for customizing your tests. ::: ## Step 4: Setup your LambdaTest credentials @@ -124,7 +128,7 @@ set LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`} ## Step 5: Execute your test Replace the `TEST_FILE_NAME` in the below command with your desired test file to execute that particular test on LambdaTest Grid: -```bash +```bash title="terminal" mvn clean install exec:java -Dexec.mainClass="com.lambdatest.TEST_FILE_NAME" -Dexec.classpathScope=test -e ``` diff --git a/docs/junit-framework.md b/docs/junit-framework.md index 5650623c3..5e54f407a 100644 --- a/docs/junit-framework.md +++ b/docs/junit-framework.md @@ -1,6 +1,6 @@ --- id: junit-frameworks -title: Execute Selenium Tests With JUnit +title: Run your Selenium JUnit tests on LambdaTest sidebar_label: JUnit description: Your guide to running tests using JUnit on LambdaTest's Selenium Grid of 3000+ real devices and desktop browsers. keywords: @@ -47,436 +47,153 @@ import TabItem from '@theme/TabItem'; }) }} > +This guide walks you through the process of running Selenium JUnit 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. -# Selenium With JUnit Tutorial - ---- - -In this topic, you will learn how to configure and run tests using **JUnit** on LambdaTest's [Selenium testing cloud platform](https://www.lambdatest.com/selenium-automation). - -## Objectives - ---- - -By the end of this topic, you will be able to: +## Prerequisites +Before you begin, ensure you have the following: -1. Set up an environment for testing your hosted web pages using **JUnit** framework with **Selenium**. -2. Understand and configure the core capabilities required for your Selenium test suite. -3. Run test cases in parallel using **JUnit** with Selenium to reduce build times. -4. Test your locally hosted pages on LambdaTest platform. -5. Explore advanced features of LambdaTest. +- Your [LambdaTest Username and Access Key](https://accounts.lambdatest.com/) +- Install Java Development Kit (JDK). We recommend Java version 11 +- Install [Maven](https://maven.apache.org/) +- [Download](https://www.selenium.dev/downloads/) the latest Selenium Client and its WebDriver bindings +## Step 1: Configure your test suite :::tip Sample repo +Download or Clone the code sample for the JUnit from the LambdaTest GitHub repository to run the tests on our Standard Grid. -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 - +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](https://www.selenium.dev/downloads/). Latest versions of Selenium Client and WebDriver are ideal for running your automation script on LambdaTest Selenium cloud grid. +```bash title="terminal" +git clone https://github.com/LambdaTest/junit-selenium-sample.git +cd junit-selenium-sample +``` -- Install **Maven** which supports **JUnit** 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. +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. -- You would have to add the following maven dependency to your `pom.xml` file if working on your local project. - ```xml - - junit - junit - 4.12 - test - - ``` +```java title="Test.java" +public static String hubURL = "https://hub.lambdatest.com/wd/hub"; +``` -### Cloning Repo and Installing Dependencies +## Step 2: Update the dependencies +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. -**Step 1:** Clone the LambdaTest’s [junit-selenium-sample](https://github.com/LambdaTest/junit-selenium-sample) repository and navigate to the code directory as shown below: +```bash title="terminal" +mvn versions:display-dependency-updates +``` -```bash -git clone https://github.com/LambdaTest/junit-selenium-sample -cd junit-selenium-sample +```xml reference +https://github.com/LambdaTest/junit-selenium-sample/blob/master/pom.xml ``` -You may also want to run the command below to check for outdated dependencies. +## Step 3: Configure your test Capabilities +LambdaTest requires specific capabilities to set the browser, browser version, operating system, and other configurations for your test. -```bash -mvn versions:display-dependency-updates -``` +Example desired capabilities for testing on Chrome 120: -### Setting up your Authentication +```java title="Test.java" +DesiredCapabilities capabilities = new DesiredCapabilities(); +capabilities.setCapability("browserName", "chrome"); +capabilities.setCapability("version", "120.0"); +capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one +capabilities.setCapability("build", "LambdaTestSeleniumSampleApp"); +capabilities.setCapability("name", "LambdaTestJUnitSample"); +``` -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). +:::tip +Use our [Capability Generator](https://www.lambdatest.com/capabilities-generator/) to select from a wide range of options for customizing your tests. +::: -**Step 2:** Set LambdaTest **Username** and **Access Key** in environment variables. +## Step 4: Setup your LambdaTest credentials +In your terminal (as per your respective Operating System), run these command to setup your LambdaTest credentials. +> You can see your credentials below if you have logged into our platform. - -
- {`export LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}" \\ + {`export LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}" export LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`}
-
-
- {`set LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}" \` + {`set LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}" set LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`}
-
-## Run Your First Test - ---- - -### Sample Test with JUnit - -Let’s checkout sample JUnit code running LambdaTest Selenium grid. This is a simple JUnit automation script similar to the script we have used in our [Github repo](https://github.com/LambdaTest/junit-selenium-sample). This JUnit Selenium script tests a sample to-do list app by marking couple items as done, adding a new item to the list and finally displaying the count of pending items as output. - -```java title="JUnit To Do" -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.openqa.selenium.JavascriptExecutor; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.remote.CapabilityType; -import org.openqa.selenium.remote.DesiredCapabilities; -import org.openqa.selenium.remote.RemoteWebDriver; -import java.net.URL; -public class JUnitTodo { - public String username = "YOUR_USERNAME"; - public String accesskey = "YOUR_ACCESS_KEY"; - public static RemoteWebDriver driver = null; - public String gridURL = "@hub.lambdatest.com/wd/hub"; - boolean status = false; - @Before - 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"); - 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 - 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()); - } - } - @After - public void tearDown() throws Exception { - if (driver != null) { - ((JavascriptExecutor) 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: - -```java -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 -``` - -:::info Note - -You can generate capabilities for your test requirements with the help of our inbuilt :link: **[Capabilities Generator Tool](https://www.lambdatest.com/capabilities-generator/)**. -::: - -### Executing the Test +## Step 5: Execute your test +If you are using the sample code repository (provided above), then use the below mentioned command to trigger your tests: -**Step 4:** The tests can be executed in the terminal using the following command. + + -```bash +```bash title="terminal" mvn test -P single ``` -:::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](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. - -::: - -## Run Parallel Tests Using JUnit - ---- - -### Setting up the Parallel Environment - -Here is the `Parallelized class` we use for running our Parallel Tests using JUnit. You can also find the below code in our [GitHub repo](https://github.com/LambdaTest/junit-selenium-sample). - -```java title="Parallelized Class" -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -import org.junit.runners.Parameterized; -import org.junit.runners.model.RunnerScheduler; - -public class Parallelized extends Parameterized { - - private static class ThreadPoolScheduler implements RunnerScheduler { - private ExecutorService executor; - - public ThreadPoolScheduler() { - String threads = System.getProperty("junit.parallel.threads", "15"); - int numThreads = Integer.parseInt(threads); - executor = Executors.newFixedThreadPool(numThreads); - } - - @Override - public void finished() { - executor.shutdown(); - try { - executor.awaitTermination(10, TimeUnit.MINUTES); - } catch (InterruptedException exc) { - throw new RuntimeException(exc); - } - } - - @Override - public void schedule(Runnable childStatement) { - executor.submit(childStatement); - } - } - - public Parallelized(Class klass) throws Throwable { - super(klass); - setScheduler(new ThreadPoolScheduler()); - } -} -``` - -Let us take a look at a reference of the above helper class for executing parallel test using JUnit automation framework. - -```java title="Concurrent Todo App" -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.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.LinkedList; -@RunWith(Parallelized.class) -public class JUnitConcurrentTodo { - public String username = "YOUR_LT_USERNAME"; - public String accesskey = "YOUR_LT_ACCESS_KEY"; - public String gridURL = "@hub.lambdatest.com/wd/hub"; - public String platform; - public String browserName; - public String browserVersion; - public RemoteWebDriver driver = null; - boolean status = false; - @Parameterized.Parameters - public static LinkedList getEnvironments() throws Exception { - LinkedList env = new LinkedList(); - env.add(new String[]{"WIN10", "chrome", "70.0"}); - env.add(new String[]{"macos 10.12","firefox","62.0"}); - env.add(new String[]{"WIN8","internet explorer","10"}); - return env; - } - public JUnitConcurrentTodo(String platform, String browserName, String browserVersion) { - this.platform = platform; - this.browserName = browserName; - this.browserVersion = browserVersion; - } - @Before - public void setUp() throws Exception { - DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setCapability("browserName", browserName); - capabilities.setCapability("version", browserVersion); - capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one - capabilities.setCapability("build", "JUnitParallelSample"); - capabilities.setCapability("name", "JUnitParallelSampleTest"); - 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 testParallel() 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()); - } - } - @After - public void tearDown() throws Exception { - if (driver != null) { - ((JavascriptExecutor) driver).executeScript("lambda-status=" + status); - driver.quit(); - } - } -} +```xml title="pom.xml" + + single + + + + org.apache.maven.plugins + maven-surefire-plugin + + + com/lambdatest/JUnitTodo.java + + + + + + ``` -### Executing Parallel Tests using JUnit + -To run parallel tests using **JUnit**, we would have to execute the below command in the terminal: + -```bash +```bash title="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](https://automation.lambdatest.com/build). - -::: - -## Testing Locally Hosted or Privately Hosted Projects - ---- - -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. - -:::tip Tunnel Help - -Refer our :link: [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) for more information. - -::: - -Here’s how you can establish LambdaTest Tunnel. - -:::info Download the binary file - -- [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip) -- [LambdaTest Tunnel for Mac](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip) -- [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip) - -::: - -Open command prompt and navigate to the binary folder. - -Run the following command: - -```bash -./LT -user {user’s login email} -key {user’s access key} +```xml title="pom.xml" + + parallel + + + + org.apache.maven.plugins + maven-surefire-plugin + + + com/lambdatest/JUnitConcurrentTodo.java + + + + + + ``` -So if your user name is **lambdatest@example.com**, the command would be: - -
- - {`./LT -user lambdatest@example.com -key ${ YOUR_LAMBDATEST_ACCESS_KEY()}`} - -
+
+
-Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code as shown: +Image -```java title="Tunnel Capability" -DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setCapability("tunnel", true); -``` +Visit the [LambdaTest Web Automation](https://automation.lambdatest.com/build) page to check the status of your test execution. ## Additional Links - ---- - - [Advanced Configuration for Capabilities](https://www.lambdatest.com/support/docs/selenium-automation-capabilities/) - [How to test locally hosted apps](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) - [How to integrate LambdaTest with CI/CD](https://www.lambdatest.com/support/docs/integrations-with-ci-cd-tools/) - diff --git a/docs/testng-framework.md b/docs/testng-framework.md index 86f0a60d1..39c6f2dbd 100644 --- a/docs/testng-framework.md +++ b/docs/testng-framework.md @@ -65,7 +65,7 @@ Download or Clone the code sample for the TestNG from the LambdaTest GitHub repo Image View on GitHub ::: -```bash +```bash title="terminal" git clone https://github.com/LambdaTest/Java-TestNG-Selenium.git cd Java-TestNG-Selenium ``` @@ -79,10 +79,14 @@ public static String hubURL = "https://hub.lambdatest.com/wd/hub"; ## Step 2: Update the dependencies 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. -```bash +```bash title="terminal" mvn versions:display-dependency-updates ``` +```xml reference +https://github.com/LambdaTest/java-testng-selenium/blob/master/pom.xml +``` + ## Step 3: Configure your test Capabilities LambdaTest requires specific capabilities to set the browser, browser version, operating system, and other configurations for your test. @@ -94,11 +98,11 @@ capabilities.setCapability("browserName", "chrome"); capabilities.setCapability("version", "120.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("name", "LambdaTestTestNGSample"); ``` :::tip -You can generate capabilities for your test requirements with the help of our inbuilt :link: **[Capabilities Generator tool](https://www.lambdatest.com/capabilities-generator/)**. +Use our [Capability Generator](https://www.lambdatest.com/capabilities-generator/) to select from a wide range of options for customizing your tests. ::: ## Step 4: Setup your LambdaTest credentials @@ -130,23 +134,25 @@ If you are using the sample code repository (provided above), then use the below -```xml reference -https://github.com/LambdaTest/java-testng-selenium/blob/master/single.xml -``` -```bash +```bash title="terminal" mvn test -D suite=single.xml ``` - - ```xml reference -https://github.com/LambdaTest/java-testng-selenium/blob/master/parallel.xml +https://github.com/LambdaTest/java-testng-selenium/blob/master/single.xml ``` + + + -```bash +```bash title="terminal" mvn test -D suite=parallel.xml ``` + +```xml reference +https://github.com/LambdaTest/java-testng-selenium/blob/master/parallel.xml +```