├── .gitignore ├── dashboard.png ├── mobile.xml ├── single.xml ├── parallel.xml ├── .gitpod.yml ├── cloudbuild.yaml ├── .project ├── .classpath ├── pom.xml ├── src └── test │ └── java │ └── com │ └── lambdatest │ ├── TestNGTodo2.java │ ├── TestNGTodo3.java │ ├── TestNGTodoMobile.java │ └── TestNGTodo1.java └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /bin/ 3 | .project 4 | .settings 5 | .theia 6 | .idea 7 | -------------------------------------------------------------------------------- /dashboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LambdaTest/java-testng-selenium/HEAD/dashboard.png -------------------------------------------------------------------------------- /mobile.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /single.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /parallel.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | # List the ports you want to expose and what to do when they are served. See https://www.gitpod.io/docs/config-ports/ 2 | ports: 3 | - port: 3000 4 | onOpen: open-preview 5 | 6 | # List the start up tasks. You can start them in parallel in multiple terminals. See https://www.gitpod.io/docs/config-start-tasks/ 7 | tasks: 8 | - init: mvn install -Dsuite=parallel.xml # runs during prebuild 9 | command: echo 'Please check your test on https://automation.lambdatest.com/' 10 | -------------------------------------------------------------------------------- /cloudbuild.yaml: -------------------------------------------------------------------------------- 1 | steps: 2 | - name: gcr.io/cloud-builders/docker 3 | args: ['run' , '-d', '--name=lt', '--network=cloudbuild', 'lambdatest/tunnel', '--user', '${_LT_USERNAME}', '--key', '${_LT_ACCESS_KEY}', '--tunnelName', 'GCloud', '--infoAPIPort', '15000','--load-balanced'] 4 | - name: curlimages/curl 5 | args: ['-s', '--retry-connrefused', '--connect-timeout', '5', '--max-time', '5', '--retry', '30', '--retry-delay', '2', '--retry-max-time', '60', 'http://lt:15000/api/v1.0/info'] 6 | - name: 'ubuntu' 7 | args: ['sleep', '20'] 8 | - name: 'bash' 9 | args: ['ls'] 10 | - name: 'maven' 11 | entrypoint: 'mvn' 12 | args: ['test', '-P', 'local'] 13 | env: 14 | - 'LT_USERNAME=${_LT_USERNAME}' 15 | - 'LT_ACCESS_KEY=${_LT_ACCESS_KEY}' 16 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | lambdatest-TestNG-project/src 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | 25 | 1643911286853 26 | 27 | 30 28 | 29 | org.eclipse.core.resources.regexFilterMatcher 30 | node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | Java-TestNG-Selenium 6 | Java-TestNG-Selenium 7 | 0.0.1-SNAPSHOT 8 | 9 | Java-TestNG-Selenium/src 10 | 11 | 12 | Java-TestNG-Selenium/src 13 | 14 | **/*.java 15 | 16 | 17 | 18 | 19 | 20 | maven-compiler-plugin 21 | 3.14.1 22 | 23 | 10 24 | 25 | 26 | 27 | org.apache.maven.plugins 28 | maven-surefire-plugin 29 | 3.5.4 30 | 31 | 32 | 33 | test 34 | 35 | 36 | 37 | 38 | 39 | 40 | ${suite} 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.seleniumhq.selenium 50 | selenium-java 51 | 4.38.0 52 | 53 | 54 | 55 | org.testng 56 | testng 57 | 7.11.0 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/TestNGTodo2.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import java.lang.reflect.Method; 4 | import java.net.MalformedURLException; 5 | import java.net.URL; 6 | 7 | import org.openqa.selenium.By; 8 | import org.openqa.selenium.MutableCapabilities; 9 | import org.openqa.selenium.chrome.ChromeOptions; 10 | import org.openqa.selenium.remote.RemoteWebDriver; 11 | import org.testng.Assert; 12 | import org.testng.ITestContext; 13 | import org.testng.annotations.AfterMethod; 14 | import org.testng.annotations.BeforeMethod; 15 | import org.testng.annotations.Test; 16 | 17 | public class TestNGTodo2 { 18 | 19 | private RemoteWebDriver driver; 20 | private String Status = "failed"; 21 | 22 | @BeforeMethod 23 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 24 | String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); 25 | String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); 26 | 27 | String hub = "@hub.lambdatest.com/wd/hub"; 28 | 29 | // ✅ LambdaTest-specific options (must go under "LT:Options") 30 | MutableCapabilities ltOptions = new MutableCapabilities(); 31 | ltOptions.setCapability("build", "TestNG With Java"); 32 | ltOptions.setCapability("name", m.getName() + this.getClass().getName()); 33 | ltOptions.setCapability("platformName", "Windows 10"); 34 | ltOptions.setCapability("plugin", "git-testng"); 35 | ltOptions.setCapability("tags", new String[] { "Feature", "Magicleap", "Severe" }); 36 | 37 | /* 38 | Enable Smart UI Project 39 | ltOptions.setCapability("smartUI.project", ""); 40 | */ 41 | 42 | // ✅ Standard browser options 43 | ChromeOptions browserOptions = new ChromeOptions(); 44 | browserOptions.setCapability("browserVersion", "latest"); 45 | browserOptions.setCapability("LT:Options", ltOptions); 46 | 47 | driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), browserOptions); 48 | } 49 | 50 | @Test 51 | public void basicTest() throws InterruptedException { 52 | System.out.println("Loading Url"); 53 | 54 | driver.get("https://lambdatest.github.io/sample-todo-app/"); 55 | 56 | System.out.println("Checking boxes..."); 57 | driver.findElement(By.name("li1")).click(); 58 | driver.findElement(By.name("li2")).click(); 59 | driver.findElement(By.name("li3")).click(); 60 | driver.findElement(By.name("li4")).click(); 61 | 62 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6"); 63 | driver.findElement(By.id("addbutton")).click(); 64 | 65 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7"); 66 | driver.findElement(By.id("addbutton")).click(); 67 | 68 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8"); 69 | driver.findElement(By.id("addbutton")).click(); 70 | 71 | driver.findElement(By.name("li1")).click(); 72 | driver.findElement(By.name("li3")).click(); 73 | driver.findElement(By.name("li7")).click(); 74 | driver.findElement(By.name("li8")).click(); 75 | 76 | System.out.println("Entering Text"); 77 | driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It"); 78 | driver.findElement(By.id("addbutton")).click(); 79 | 80 | driver.findElement(By.name("li9")).click(); 81 | 82 | // ✅ Assert that the newly added todo is present 83 | String spanText = driver.findElement(By.xpath("//li[9]/span")).getText(); 84 | Assert.assertEquals(spanText, "Get Taste of Lambda and Stick to It"); 85 | 86 | Status = "passed"; 87 | Thread.sleep(150); 88 | System.out.println("Test Finished"); 89 | } 90 | 91 | @AfterMethod 92 | public void tearDown() { 93 | if (driver != null) { 94 | driver.executeScript("lambda-status=" + Status); 95 | driver.quit(); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/TestNGTodo3.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import java.lang.reflect.Method; 4 | import java.net.MalformedURLException; 5 | import java.net.URL; 6 | 7 | import org.openqa.selenium.By; 8 | import org.openqa.selenium.MutableCapabilities; 9 | import org.openqa.selenium.chrome.ChromeOptions; 10 | import org.openqa.selenium.remote.RemoteWebDriver; 11 | import org.testng.Assert; 12 | import org.testng.ITestContext; 13 | import org.testng.annotations.AfterMethod; 14 | import org.testng.annotations.BeforeMethod; 15 | import org.testng.annotations.Test; 16 | 17 | public class TestNGTodo3 { 18 | 19 | private RemoteWebDriver driver; 20 | private String Status = "failed"; 21 | 22 | @BeforeMethod 23 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 24 | String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); 25 | String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); 26 | 27 | String hub = "@hub.lambdatest.com/wd/hub"; 28 | 29 | // ✅ W3C-compliant capabilities using ChromeOptions + LT:Options 30 | MutableCapabilities ltOptions = new MutableCapabilities(); 31 | ltOptions.setCapability("build", "TestNG With Java"); 32 | ltOptions.setCapability("name", m.getName() + " - " + this.getClass().getName()); 33 | ltOptions.setCapability("platformName", "macOS Sonoma"); // updated OS name 34 | ltOptions.setCapability("plugin", "git-testng"); 35 | ltOptions.setCapability("tags", new String[] { "Feature", "Tag", "Moderate" }); 36 | 37 | // ✅ Chrome-specific options 38 | ChromeOptions browserOptions = new ChromeOptions(); 39 | browserOptions.setBrowserVersion("latest"); 40 | browserOptions.setCapability("LT:Options", ltOptions); 41 | 42 | driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), browserOptions); 43 | } 44 | 45 | @Test 46 | public void basicTest() throws InterruptedException { 47 | System.out.println("Loading URL..."); 48 | driver.get("https://lambdatest.github.io/sample-todo-app/"); 49 | Thread.sleep(200); 50 | 51 | System.out.println("Checking Boxes..."); 52 | driver.findElement(By.name("li1")).click(); 53 | driver.findElement(By.name("li2")).click(); 54 | driver.findElement(By.name("li3")).click(); 55 | driver.findElement(By.name("li4")).click(); 56 | 57 | System.out.println("Adding New Items..."); 58 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6"); 59 | driver.findElement(By.id("addbutton")).click(); 60 | 61 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7"); 62 | driver.findElement(By.id("addbutton")).click(); 63 | 64 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8"); 65 | driver.findElement(By.id("addbutton")).click(); 66 | 67 | System.out.println("Checking More Boxes..."); 68 | driver.findElement(By.name("li1")).click(); 69 | driver.findElement(By.name("li3")).click(); 70 | driver.findElement(By.name("li7")).click(); 71 | driver.findElement(By.name("li8")).click(); 72 | 73 | System.out.println("Adding Final Item..."); 74 | driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It"); 75 | driver.findElement(By.id("addbutton")).click(); 76 | 77 | driver.findElement(By.name("li9")).click(); 78 | 79 | // ✅ Safer and cleaner XPath 80 | String spanText = driver.findElement(By.xpath("//li[9]/span")).getText(); 81 | Assert.assertEquals(spanText, "Get Taste of Lambda and Stick to It"); 82 | 83 | Status = "passed"; 84 | Thread.sleep(300); 85 | System.out.println("✅ Test Finished Successfully"); 86 | } 87 | 88 | @AfterMethod 89 | public void tearDown() { 90 | try { 91 | driver.executeScript("lambda-status=" + Status); 92 | } finally { 93 | if (driver != null) { 94 | driver.quit(); 95 | } 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/TestNGTodoMobile.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import java.lang.reflect.Method; 4 | import java.net.MalformedURLException; 5 | import java.net.URL; 6 | 7 | import org.openqa.selenium.By; 8 | import org.openqa.selenium.MutableCapabilities; 9 | import org.openqa.selenium.remote.RemoteWebDriver; 10 | import org.testng.Assert; 11 | import org.testng.ITestContext; 12 | import org.testng.annotations.AfterMethod; 13 | import org.testng.annotations.BeforeMethod; 14 | import org.testng.annotations.Test; 15 | 16 | public class TestNGTodoMobile { 17 | 18 | private RemoteWebDriver driver; 19 | private String Status = "failed"; 20 | 21 | @BeforeMethod 22 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 23 | String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); 24 | String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); 25 | 26 | String hub = "@mobile-hub.lambdatest.com/wd/hub"; 27 | 28 | // ✅ LambdaTest Mobile W3C-compliant options 29 | MutableCapabilities ltOptions = new MutableCapabilities(); 30 | ltOptions.setCapability("platformName", "Android"); 31 | ltOptions.setCapability("deviceName", "Pixel 4a"); 32 | ltOptions.setCapability("platformVersion", "11"); 33 | ltOptions.setCapability("isRealMobile", true); 34 | ltOptions.setCapability("build", "TestNG With Java - Mobile"); 35 | ltOptions.setCapability("name", m.getName() + " - " + this.getClass().getSimpleName()); 36 | ltOptions.setCapability("plugin", "git-testng"); 37 | ltOptions.setCapability("tags", new String[] { "Feature", "Tag", "Mobile" }); 38 | 39 | // ✅ If you want to specify browser explicitly (mobile web) 40 | ltOptions.setCapability("browserName", "Chrome"); 41 | 42 | driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), ltOptions); 43 | } 44 | 45 | @Test 46 | public void basicTest() throws InterruptedException { 47 | System.out.println("Loading URL..."); 48 | driver.get("https://lambdatest.github.io/sample-todo-app/"); 49 | Thread.sleep(300); 50 | 51 | System.out.println("Checking Boxes..."); 52 | driver.findElement(By.name("li1")).click(); 53 | driver.findElement(By.name("li2")).click(); 54 | driver.findElement(By.name("li3")).click(); 55 | driver.findElement(By.name("li4")).click(); 56 | 57 | System.out.println("Adding New Items..."); 58 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6"); 59 | driver.findElement(By.id("addbutton")).click(); 60 | 61 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7"); 62 | driver.findElement(By.id("addbutton")).click(); 63 | 64 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8"); 65 | driver.findElement(By.id("addbutton")).click(); 66 | 67 | System.out.println("Rechecking Boxes..."); 68 | driver.findElement(By.name("li1")).click(); 69 | driver.findElement(By.name("li3")).click(); 70 | driver.findElement(By.name("li7")).click(); 71 | driver.findElement(By.name("li8")).click(); 72 | 73 | System.out.println("Adding Final Todo Item..."); 74 | driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It"); 75 | driver.findElement(By.id("addbutton")).click(); 76 | 77 | driver.findElement(By.name("li9")).click(); 78 | 79 | // ✅ Stable and cleaner XPath check 80 | String spanText = driver.findElement(By.xpath("//li[9]/span")).getText(); 81 | Assert.assertEquals(spanText, "Get Taste of Lambda and Stick to It"); 82 | 83 | Status = "passed"; 84 | Thread.sleep(500); 85 | System.out.println("✅ Test Completed Successfully"); 86 | } 87 | 88 | @AfterMethod 89 | public void tearDown() { 90 | try { 91 | driver.executeScript("lambda-status=" + Status); 92 | } finally { 93 | if (driver != null) { 94 | driver.quit(); 95 | } 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/TestNGTodo1.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import java.lang.reflect.Method; 4 | import java.net.MalformedURLException; 5 | import java.net.URL; 6 | 7 | import org.openqa.selenium.By; 8 | import org.openqa.selenium.MutableCapabilities; 9 | import org.openqa.selenium.safari.SafariOptions; 10 | import org.openqa.selenium.remote.RemoteWebDriver; 11 | import org.testng.Assert; 12 | import org.testng.ITestContext; 13 | import org.testng.annotations.AfterMethod; 14 | import org.testng.annotations.BeforeMethod; 15 | import org.testng.annotations.Test; 16 | 17 | public class TestNGTodo1 { 18 | 19 | private RemoteWebDriver driver; 20 | private String Status = "failed"; 21 | 22 | @BeforeMethod 23 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 24 | String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); 25 | String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); 26 | 27 | String hub = "@hub.lambdatest.com/wd/hub"; 28 | 29 | // ✅ Use LambdaTest W3C-compliant structure (LT:Options) 30 | MutableCapabilities ltOptions = new MutableCapabilities(); 31 | ltOptions.setCapability("build", "TestNG With Java"); 32 | ltOptions.setCapability("name", m.getName() + " - " + this.getClass().getName()); 33 | ltOptions.setCapability("platformName", "macOS Sonoma"); // modern macOS 34 | ltOptions.setCapability("plugin", "git-testng"); 35 | ltOptions.setCapability("tags", new String[] { "Feature", "Falcon", "Severe" }); 36 | 37 | /* 38 | Enable Smart UI Project (optional) 39 | ltOptions.setCapability("smartUI.project", ""); 40 | */ 41 | 42 | // ✅ Safari browser setup (works with Selenium 4.x) 43 | SafariOptions browserOptions = new SafariOptions(); 44 | browserOptions.setCapability("browserVersion", "latest"); 45 | browserOptions.setCapability("LT:Options", ltOptions); 46 | 47 | driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), browserOptions); 48 | } 49 | 50 | @Test 51 | public void basicTest() throws InterruptedException { 52 | System.out.println("Loading Url"); 53 | driver.executeScript("lambdatest_executor: {\"action\": \"stepcontext\", \"arguments\": {\"data\": \"Opening WebApp\", \"level\": \"info\"}}"); 54 | 55 | driver.get("https://lambdatest.github.io/sample-todo-app/"); 56 | 57 | driver.executeScript("lambdatest_executor: {\"action\": \"stepcontext\", \"arguments\": {\"data\": \"Checking List Items\", \"level\": \"info\"}}"); 58 | 59 | System.out.println("Checking Boxes..."); 60 | driver.findElement(By.name("li1")).click(); 61 | driver.findElement(By.name("li2")).click(); 62 | driver.findElement(By.name("li3")).click(); 63 | driver.findElement(By.name("li4")).click(); 64 | 65 | driver.executeScript("lambdatest_executor: {\"action\": \"stepcontext\", \"arguments\": {\"data\": \"Adding Items\", \"level\": \"info\"}}"); 66 | 67 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6"); 68 | driver.findElement(By.id("addbutton")).click(); 69 | 70 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7"); 71 | driver.findElement(By.id("addbutton")).click(); 72 | 73 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8"); 74 | driver.findElement(By.id("addbutton")).click(); 75 | 76 | driver.executeScript("lambdatest_executor: {\"action\": \"stepcontext\", \"arguments\": {\"data\": \"Checking More Items\", \"level\": \"info\"}}"); 77 | 78 | driver.findElement(By.name("li1")).click(); 79 | driver.findElement(By.name("li3")).click(); 80 | driver.findElement(By.name("li7")).click(); 81 | driver.findElement(By.name("li8")).click(); 82 | Thread.sleep(300); 83 | 84 | driver.executeScript("lambdatest_executor: {\"action\": \"stepcontext\", \"arguments\": {\"data\": \"Adding and Verify List Items\", \"level\": \"info\"}}"); 85 | driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It"); 86 | driver.findElement(By.id("addbutton")).click(); 87 | 88 | driver.findElement(By.name("li9")).click(); 89 | 90 | String spanText = driver.findElement(By.xpath("//li[9]/span")).getText(); 91 | Assert.assertEquals(spanText.trim(), "Get Taste of Lambda and Stick to It"); 92 | 93 | Status = "passed"; 94 | Thread.sleep(150); 95 | 96 | System.out.println("Test Finished"); 97 | } 98 | 99 | @AfterMethod 100 | public void tearDown() { 101 | try { 102 | driver.executeScript("lambdatest_executor: {\"action\": \"stepcontext\", \"arguments\": {\"data\": \"Adding Test Result and Closing Browser\", \"level\": \"info\"}}"); 103 | driver.executeScript("lambda-status=" + Status); 104 | } finally { 105 | if (driver != null) { 106 | driver.quit(); 107 | } 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Run Selenium Tests With TestNG On LambdaTest 2 | 3 | ![image](https://user-images.githubusercontent.com/70570645/171934563-4806efd2-1154-494c-a01d-1def95657383.png) 4 | 5 | 6 |

7 | Blog 8 |   ⋅   9 | Docs 10 |   ⋅   11 | Learning Hub 12 |   ⋅   13 | Newsletter 14 |   ⋅   15 | Certifications 16 |   ⋅   17 | YouTube 18 |

19 |   20 |   21 |   22 | 23 | *Learn how to use TestNG framework to configure and run your Java automation testing scripts on the LambdaTest platform* 24 | 25 | [](https://accounts.lambdatest.com/register?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 26 | 27 | ## Table Of Contents 28 | 29 | * [Pre-requisites](#pre-requisites) 30 | * [Run Your First Test](#run-your-first-test) 31 | * [Parallel Testing With TestNG](#executing-parallel-tests-using-testng) 32 | * [Local Testing With TestNG](#testing-locally-hosted-or-privately-hosted-projects) 33 | 34 | ## Pre-requisites 35 | 36 | Before you can start performing Java automation testing with Selenium, you would need to: 37 | 38 | - Install the latest **Java development environment** i.e. **JDK 1.6** or higher. We recommend using the latest version. 39 | 40 | - 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. 41 | 42 | - 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. 43 | 44 | ### Cloning Repo And Installing Dependencies 45 | 46 | **Step 1:** Clone the LambdaTest’s Java-TestNG-Selenium repository and navigate to the code directory as shown below: 47 | 48 | ```bash 49 | git clone https://github.com/LambdaTest/Java-TestNG-Selenium 50 | cd Java-TestNG-Selenium 51 | ``` 52 | 53 | You can also run the command below to check for outdated dependencies. 54 | 55 | ```bash 56 | mvn versions:display-dependency-updates 57 | ``` 58 | 59 | ### Setting Up Your Authentication 60 | 61 | Make sure you have your LambdaTest credentials with you to run test automation scripts. You can get these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/build?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) or by your [LambdaTest Profile](https://accounts.lambdatest.com/login?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium). 62 | 63 | **Step 2:** Set LambdaTest **Username** and **Access Key** in environment variables. 64 | 65 | * For **Linux/macOS**: 66 | 67 | ```bash 68 | export LT_USERNAME="YOUR_USERNAME" 69 | export LT_ACCESS_KEY="YOUR ACCESS KEY" 70 | ``` 71 | * For **Windows**: 72 | ```bash 73 | set LT_USERNAME="YOUR_USERNAME" 74 | set LT_ACCESS_KEY="YOUR ACCESS KEY" 75 | ``` 76 | 77 | ## Run Your First Test 78 | 79 | >**Test Scenario**: The sample [TestNGTodo1.java](https://github.com/LambdaTest/Java-TestNG-Selenium/blob/master/src/test/java/com/lambdatest/TestNGTodo1.java) 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. 80 | 81 | 82 | ### Configuring Your Test Capabilities 83 | 84 | **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: 85 | 86 | ```java 87 | DesiredCapabilities capabilities = new DesiredCapabilities(); 88 | capabilities.setCapability("browserName", "chrome"); 89 | capabilities.setCapability("version", "70.0"); 90 | capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one 91 | capabilities.setCapability("build", "LambdaTestSampleApp"); 92 | capabilities.setCapability("name", "LambdaTestJavaSample"); 93 | ``` 94 | 95 | You can generate capabilities for your test requirements with the help of our inbuilt [Desired Capability Generator](https://www.lambdatest.com/capabilities-generator/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium). 96 | 97 | ### Executing The Test 98 | 99 | **Step 4:** The tests can be executed in the terminal using the following command. 100 | 101 | ```bash 102 | mvn test -D suite=single.xml 103 | ``` 104 | 105 | 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. 106 | 107 | ## Run Parallel Tests Using TestNG 108 | 109 | 110 | 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`). 111 | 112 | ```xml title="testng.xml" 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | ``` 145 | 146 | ### Executing Parallel Tests Using TestNG 147 | 148 | To run parallel tests using **TestNG**, we would have to execute the below commands in the terminal: 149 | 150 | - For the above example code 151 | ```bash 152 | mvn test 153 | ``` 154 | - For the cloned Java-TestNG-Selenium repo used to run our first sample test 155 | ```bash 156 | mvn test -D suite=parallel.xml 157 | ``` 158 | 159 | ## Testing Locally Hosted Or Privately Hosted Projects 160 | 161 | You can test your locally hosted or privately hosted projects with LambdaTest Selenium grid using LambdaTest Tunnel. All you would have to do is set up an SSH tunnel using tunnel 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 live. 162 | 163 | Refer our [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) for more information. 164 | 165 | Here’s how you can establish LambdaTest Tunnel. 166 | 167 | Download the binary file of: 168 | * [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip) 169 | * [LambdaTest Tunnel for macOS](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip) 170 | * [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip) 171 | 172 | Open command prompt and navigate to the binary folder. 173 | 174 | Run the following command: 175 | 176 | ```bash 177 | LT -user {user’s login email} -key {user’s access key} 178 | ``` 179 | So if your user name is lambdatest@example.com and key is 123456, the command would be: 180 | 181 | ```bash 182 | LT -user lambdatest@example.com -key 123456 183 | ``` 184 | Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code shown below : 185 | 186 | **Tunnel Capability** 187 | 188 | ```java 189 | DesiredCapabilities capabilities = new DesiredCapabilities(); 190 | capabilities.setCapability("tunnel", true); 191 | ``` 192 | 193 | ## Tutorials 📙 194 | 195 | Check out our latest tutorials on TestNG automation testing 👇 196 | 197 | * [JUnit 5 vs TestNG: Choosing the Right Framework for Automation Testing](https://www.lambdatest.com/blog/junit-5-vs-testng/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 198 | * [How To Install TestNG?](https://www.lambdatest.com/blog/how-to-install-testng-in-eclipse-step-by-step-guide/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 199 | * [Create TestNG Project in Eclipse & Run Selenium Test Script](https://www.lambdatest.com/blog/create-testng-project-in-eclipse-run-selenium-test-script/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 200 | * [A Complete Guide for Your First TestNG Automation Script](https://www.lambdatest.com/blog/a-complete-guide-for-your-first-testng-automation-script/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 201 | * [How to Automate using TestNG in Selenium?](https://www.lambdatest.com/blog/testng-in-selenium/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 202 | * [How to Perform Parallel Test Execution in TestNG with Selenium](https://www.lambdatest.com/blog/parallel-test-execution-in-testng/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 203 | * [Creating TestNG XML File & Execute Parallel Testing](https://www.lambdatest.com/blog/create-testng-xml-file-execute-parallel-testing/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 204 | * [Speed Up Automated Parallel Testing in Selenium with TestNG](https://www.lambdatest.com/blog/speed-up-automated-parallel-testing-in-selenium-with-testng/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 205 | * [Automation Testing With Selenium, Cucumber & TestNG](https://www.lambdatest.com/blog/automation-testing-with-selenium-cucumber-testng/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 206 | * [How to Run JUnit Selenium Tests using TestNG](https://www.lambdatest.com/blog/test-example-junit-and-testng-in-selenium/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 207 | * [How to Group Test Cases in TestNG [With Examples]](https://www.lambdatest.com/blog/grouping-test-cases-in-testng/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 208 | * [How to Set Test Case Priority in TestNG with Selenium](https://www.lambdatest.com/blog/prioritizing-tests-in-testng-with-selenium/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 209 | * [How to Use Assertions in TestNG with Selenium](https://www.lambdatest.com/blog/asserts-in-testng/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 210 | * [How to Use DataProviders in TestNG [With Examples]](https://www.lambdatest.com/blog/how-to-use-dataproviders-in-testng-with-examples/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 211 | * [Parameterization in TestNG - DataProvider and TestNG XML [With Examples]](https://www.lambdatest.com/blog/parameterization-in-testng-dataprovider-and-testng-xml-examples/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 212 | * [TestNG Listeners in Selenium WebDriver [With Examples]](https://www.lambdatest.com/blog/testng-listeners-in-selenium-webdriver-with-examples/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 213 | * [TestNG Annotations Tutorial with Examples for Selenium Automation](https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 214 | * [How to Use TestNG Reporter Log in Selenium](https://www.lambdatest.com/blog/how-to-use-testng-reporter-log-in-selenium/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 215 | * [How to Generate TestNG Reports in Jenkins](https://www.lambdatest.com/blog/how-to-generate-testng-reports-in-jenkins/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 216 | 217 | 218 | ## Documentation & Resources :books: 219 | 220 | 221 | Visit the following links to learn more about LambdaTest's features, setup and tutorials around test automation, mobile app testing, responsive testing, and manual testing. 222 | 223 | * [LambdaTest Documentation](https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 224 | * [LambdaTest Blog](https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 225 | * [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 226 | 227 | ## LambdaTest Community :busts_in_silhouette: 228 | 229 | The [LambdaTest Community](https://community.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) allows people to interact with tech enthusiasts. Connect, ask questions, and learn from tech-savvy people. Discuss best practises in web development, testing, and DevOps with professionals from across the globe 🌎 230 | 231 | ## What's New At LambdaTest ❓ 232 | 233 | To stay updated with the latest features and product add-ons, visit [Changelog](https://changelog.lambdatest.com) 234 | 235 | ## About LambdaTest 236 | 237 | [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) is a leading test execution and orchestration platform that is fast, reliable, scalable, and secure. It allows users to run both manual and automated testing of web and mobile apps across 3000+ different browsers, operating systems, and real device combinations. Using LambdaTest, businesses can ensure quicker developer feedback and hence achieve faster go to market. Over 500 enterprises and 1 Million + users across 130+ countries rely on LambdaTest for their testing needs. 238 | 239 | ### Features 240 | 241 | * Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments. 242 | * Real-time cross browser testing on 3000+ environments. 243 | * Test on Real device cloud 244 | * Blazing fast test automation with HyperExecute 245 | * Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale. 246 | * Smart Visual Regression Testing on cloud 247 | * 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more. 248 | * Automated Screenshot testing across multiple browsers in a single click. 249 | * Local testing of web and mobile apps. 250 | * Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems. 251 | * Geolocation testing of web and mobile apps across 53+ countries. 252 | * LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports 253 | 254 | 255 | [](https://accounts.lambdatest.com/register?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 256 | 257 | 258 | 259 | ## We are here to help you :headphones: 260 | 261 | * Got a query? we are available 24x7 to help. [Contact Us](mailto:support@lambdatest.com) 262 | * For more info, visit - [LambdaTest](https://www.lambdatest.com?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) 263 | --------------------------------------------------------------------------------