├── .gitignore ├── .gitpod.yml ├── README.md ├── Screenshot from 2022-01-29 13-26-03.png ├── pom.xml └── src └── test └── java └── com └── lambdatest └── BrowserCache.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | settings.json 4 | 5 | # Log file 6 | *.log 7 | 8 | # BlueJ files 9 | *.ctxt 10 | 11 | # Mobile Tools for Java (J2ME) 12 | .mtj.tmp/ 13 | 14 | # Package Files # 15 | *.jar 16 | *.war 17 | *.nar 18 | *.ear 19 | *.zip 20 | *.tar.gz 21 | *.rar 22 | 23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 24 | hs_err_pid* 25 | target/ 26 | .idea/ 27 | -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | tasks: 2 | - init: mvn test 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Run Selenium Tests With JUnit On LambdaTest (Browser Cache Clearing Example) 2 | 3 | ![image](https://user-images.githubusercontent.com/70570645/171432631-dcc31b10-6590-4877-98c0-4ac702fbd441.png) 4 | 5 |

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

18 |   19 |   20 |   21 | 22 | *Learn how to use JUnit framework to configure and run your Java automation testing scripts on the LambdaTest platform* 23 | 24 | [](https://accounts.lambdatest.com/register) 25 | 26 | 27 | ## Table Of Contents 28 | 29 | * [Pre-requisites](#pre-requisites) 30 | * [Run Your First Test](#run-your-first-test) 31 | * [Parallel Testing With JUnit](#run-parallel-tests-using-junit) 32 | * [Local Testing With JUnit](#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 | - You would have to add the following maven dependency to your `pom.xml` file if working on your local project. 45 | ```xml 46 | 47 | junit 48 | junit 49 | 4.12 50 | test 51 | 52 | ``` 53 | 54 | ### Cloning Repo And Installing Dependencies 55 | 56 | **Step 1:** Clone the LambdaTest’s JUnit-Selenium-Sample repository and navigate to the code directory as shown below: 57 | 58 | ```bash 59 | git clone https://github.com/LambdaTest/junit-selenium-sample 60 | cd junit-selenium-sample 61 | ``` 62 | 63 | You may also want to run the command below to check for outdated dependencies. 64 | 65 | ```bash 66 | mvn versions:display-dependency-updates 67 | ``` 68 | 69 | ### Setting Up Your Authentication 70 | 71 | 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) or by your [LambdaTest Profile](https://accounts.lambdatest.com/login). 72 | 73 | **Step 2:** Set LambdaTest **Username** and **Access Key** in environment variables. 74 | 75 | * For **Linux/macOS**: 76 | 77 | ```bash 78 | export LT_USERNAME="YOUR_USERNAME" 79 | export LT_ACCESS_KEY="YOUR ACCESS KEY" 80 | ``` 81 | * For **Windows**: 82 | ```bash 83 | set LT_USERNAME="YOUR_USERNAME" 84 | set LT_ACCESS_KEY="YOUR ACCESS KEY" 85 | ``` 86 | 87 | ## Run Your First Test 88 | 89 | >**Test Scenario**: Checkout sample [JUnitTodo.java](https://github.com/LambdaTest/junit-selenium-sample/blob/master/src/test/java/com/lambdatest/JUnitTodo.java) file. 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. 90 | 91 | ### Configuring your Test Capabilities 92 | 93 | **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: 94 | 95 | ```java 96 | DesiredCapabilities capabilities = new DesiredCapabilities(); 97 | capabilities.setCapability("browserName", "chrome"); 98 | capabilities.setCapability("version", "latest"); 99 | capabilities.setCapability("platform", "Windows 10"); // If this cap isn't specified, it will just get the any 100 | // available one 101 | capabilities.setCapability("build", "Junit Testing Example"); 102 | capabilities.setCapability("name", "BrowserCache Test"); 103 | capabilities.setCapability("plugin", "git-junit"); 104 | ``` 105 | ### Clearing Browser Cache after Test 106 | ```java 107 | // Clearing browser Cache after Test 108 | driver.manage().deleteAllCookies(); // delete all cookies 109 | Thread.sleep(7000); // wait 7 seconds to clear cookies. 110 | ``` 111 | 112 | ### Executing the Test 113 | 114 | **Step 4:** The tests can be executed in the terminal using the following command. 115 | 116 | ```bash 117 | mvn test -P single 118 | ``` 119 | 120 | 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). 121 | 122 | ## Run Parallel Tests Using JUnit 123 | 124 | Check out the [Parallelized.java](https://github.com/LambdaTest/junit-selenium-sample/blob/master/src/test/java/com/lambdatest/Parallelized.java) class we have used for running our Parallel Tests using JUnit. 125 | 126 | 127 | Check out the [JUnitConcurrentTodo.java](https://github.com/LambdaTest/junit-selenium-sample/blob/master/src/test/java/com/lambdatest/JUnitConcurrentTodo.java) file for executing parallel test using JUnit automation framework. 128 | 129 | ### Executing Parallel Tests Using JUnit 130 | 131 | To run parallel tests using **JUnit**, we would have to execute the below command in the terminal: 132 | 133 | ```bash 134 | mvn test -P parallel 135 | ``` 136 | 137 | ## Testing Locally Hosted Or Privately Hosted Projects 138 | 139 | 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. 140 | 141 | Refer our [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) for more information. 142 | 143 | Here’s how you can establish LambdaTest Tunnel. 144 | 145 | Download the binary file of: 146 | * [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip) 147 | * [LambdaTest Tunnel for macOS](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip) 148 | * [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip) 149 | 150 | Open command prompt and navigate to the binary folder. 151 | 152 | Run the following command: 153 | 154 | ```bash 155 | LT -user {user’s login email} -key {user’s access key} 156 | ``` 157 | So if your user name is lambdatest@example.com and key is 123456, the command would be: 158 | 159 | ```bash 160 | LT -user lambdatest@example.com -key 123456 161 | ``` 162 | Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code shown below : 163 | 164 | **Tunnel Capability** 165 | 166 | ```java 167 | DesiredCapabilities capabilities = new DesiredCapabilities(); 168 | capabilities.setCapability("tunnel", true); 169 | ``` 170 | 171 | ## Tutorials 📙 172 | 173 | Check out our latest tutorials on JUnit automation testing 👇 174 | 175 | * [JUnit 5 vs TestNG: Choosing the Right Frameworks for Selenium Automation Testing](https://www.lambdatest.com/blog/junit-5-vs-testng/) 176 | * [TestNG vs JUnit: Which Testing Framework Should You Choose?](https://www.lambdatest.com/blog/testng-vs-junit-which-testing-framework-should-you-choose/) 177 | * [JUnit With Selenium](https://www.lambdatest.com/blog/automated-testing-with-junit-and-selenium-for-browser-compatibility/) 178 | * [JUnit Environment Setup](https://www.lambdatest.com/blog/setup-junit-environment/) 179 | * [How to Run JUnit Selenium Tests Using TestNG?](https://www.lambdatest.com/blog/test-example-junit-and-testng-in-selenium/) 180 | * [[Complete JUnit 5 Mockito Tutorial for Unit Testing]](https://www.lambdatest.com/blog/junit5-mockito-tutorial/) 181 | * [Parallel Testing with JUnit 5 and Selenium [Tutorial]](https://www.lambdatest.com/blog/parallel-testing-with-junit5-and-selenium/) 182 | * [How to Execute JUnit 4 Tests with JUnit 5 [Tutorial]](https://www.lambdatest.com/blog/execute-junit4-tests-with-junit5/) 183 | * [How to Run JUnit Tests from the Command Line?](https://www.lambdatest.com/blog/run-junit-from-command-line/) 184 | * [How to Minimize Browsers in Selenium WebDriver Using JUnit?](https://www.lambdatest.com/blog/minimize-browsers-in-selenium-webdriver/) 185 | * [How to use @RepeatedTest Annotations in JUnit 5?](https://www.lambdatest.com/blog/repeatedtest-annotation-in-junit-5/) 186 | * [A Comprehensive Guide on JUnit 5 Extensions](https://www.lambdatest.com/blog/junit5-extensions/) 187 | * [How to Run JUnit Selenium Tests Using TestNG?](https://www.lambdatest.com/blog/test-example-junit-and-testng-in-selenium/) 188 | * [JUnit Parameterized Test for Selenium Automation with Examples](https://www.lambdatest.com/blog/junit-parameterized-test-selenium/) 189 | * [JUnit Asserts with Examples](https://www.lambdatest.com/blog/junit-assertions-example-for-selenium-testing/) 190 | * [Tutorial on JUnit Annotations with Examples](https://www.lambdatest.com/blog/tutorial-on-junit-annotations-in-selenium-with-examples/) 191 | * [Automated Testing with JUnit and Selenium for Browser Compatibility](https://www.lambdatest.com/blog/automated-testing-with-junit-and-selenium-for-browser-compatibility/) 192 | 193 | For video tutorials on Selenium JUnit, please refer to our [JUnit Tutorial Playlist](https://www.youtube.com/playlist?list=PLZMWkkQEwOPn68qzCGJl07ZbnI7Ix5zKU). ▶️ 194 | 195 | Subscribe To Our [LambdaTest YouTube Channel 🔔](https://www.youtube.com/c/LambdaTest) and keep up-to-date on the latest video tutorial around software testing world. 196 | 197 | ## Documentation & Resources :books: 198 | 199 | 200 | 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. 201 | 202 | * [LambdaTest Documentation](https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=junit-selenium-sample) 203 | * [LambdaTest Blog](https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=junit-selenium-sample) 204 | * [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=junit-selenium-sample) 205 | 206 | ## LambdaTest Community :busts_in_silhouette: 207 | 208 | The [LambdaTest Community](https://community.lambdatest.com/) 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 🌎 209 | 210 | ## What's New At LambdaTest ❓ 211 | 212 | To stay updated with the latest features and product add-ons, visit [Changelog](https://changelog.lambdatest.com/) 213 | 214 | ## About LambdaTest 215 | 216 | [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=junit-selenium-sample) 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. 217 | 218 | ### Features 219 | 220 | * Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments. 221 | * Real-time cross browser testing on 3000+ environments. 222 | * Test on Real device cloud 223 | * Blazing fast test automation with HyperExecute 224 | * Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale. 225 | * Smart Visual Regression Testing on cloud 226 | * 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more. 227 | * Automated Screenshot testing across multiple browsers in a single click. 228 | * Local testing of web and mobile apps. 229 | * Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems. 230 | * Geolocation testing of web and mobile apps across 53+ countries. 231 | * LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports 232 | 233 | 234 | [](https://accounts.lambdatest.com/register) 235 | 236 | 237 | ## We are here to help you :headphones: 238 | 239 | * Got a query? we are available 24x7 to help. [Contact Us](support@lambdatest.com) 240 | * For more info, visit - [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=junit-selenium-sample) 241 | -------------------------------------------------------------------------------- /Screenshot from 2022-01-29 13-26-03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/praveenLT/junit-selenium-browser-cache-clear/ebd94e6fd106afe133ac96b3221058a5b4fb4a0b/Screenshot from 2022-01-29 13-26-03.png -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.lambdatest 6 | lambdatest-junit-sample 7 | 1.0-SNAPSHOT 8 | 9 | 10 | 11 | UTF-8 12 | 2.19.1 13 | 14 | 15 | default 16 | 17 | 18 | 19 | 20 | junit 21 | junit 22 | 4.13.1 23 | test 24 | 25 | 26 | commons-io 27 | commons-io 28 | 2.7 29 | test 30 | 31 | 32 | org.seleniumhq.selenium 33 | selenium-java 34 | 3.141.59 35 | test 36 | 37 | 38 | com.googlecode.json-simple 39 | json-simple 40 | 1.1.1 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | maven-compiler-plugin 51 | 3.0 52 | 53 | 1.8 54 | 1.8 55 | 56 | 57 | 58 | 59 | 60 | org.apache.maven.plugins 61 | maven-surefire-plugin 62 | 2.12.4 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | parallel 71 | 72 | 73 | 74 | org.apache.maven.plugins 75 | maven-surefire-plugin 76 | 77 | 78 | com/lambdatest/JUnitConcurrentTodo.java 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | single 88 | 89 | 90 | 91 | org.apache.maven.plugins 92 | maven-surefire-plugin 93 | 94 | 95 | com/lambdatest/BrowserCache.java 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/BrowserCache.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import org.junit.After; 4 | import org.junit.Assert; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import org.openqa.selenium.remote.DesiredCapabilities; 8 | import org.openqa.selenium.remote.RemoteWebDriver; 9 | 10 | import org.openqa.selenium.By; 11 | import org.openqa.selenium.Cookie; 12 | import org.openqa.selenium.WebElement; 13 | import org.openqa.selenium.chrome.ChromeOptions; 14 | 15 | import java.io.File; 16 | import java.net.MalformedURLException; 17 | import java.net.URL; 18 | import java.util.Set; 19 | 20 | public class BrowserCache { 21 | String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); 22 | String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); 23 | public static RemoteWebDriver driver = null; 24 | public String gridURL = "@hub.lambdatest.com/wd/hub"; 25 | public String status = "failed"; 26 | 27 | @Before 28 | public void setUp() throws Exception { 29 | DesiredCapabilities capabilities = new DesiredCapabilities(); 30 | capabilities.setCapability("browserName", "chrome"); 31 | capabilities.setCapability("version", "latest"); 32 | capabilities.setCapability("platform", "Windows 10"); // If this cap isn't specified, it will just get the any 33 | // available one 34 | capabilities.setCapability("build", "Junit Testing Example"); 35 | capabilities.setCapability("name", "BrowserCache Test"); 36 | capabilities.setCapability("plugin", "git-junit"); 37 | 38 | try { 39 | driver = new RemoteWebDriver(new URL("https://" + username + ":" + accessKey + gridURL), capabilities); 40 | } catch (MalformedURLException e) { 41 | System.out.println("Invalid grid URL"); 42 | } catch (Exception e) { 43 | System.out.println(e.getMessage()); 44 | } 45 | } 46 | 47 | @Test 48 | public void browserCacheTest() throws InterruptedException { 49 | String spanText; 50 | System.out.println("Loading Url"); 51 | 52 | driver.get("https://lambdatest.github.io/sample-todo-app/"); 53 | 54 | System.out.println("Checking Box"); 55 | driver.findElement(By.name("li1")).click(); 56 | 57 | System.out.println("Checking Another Box"); 58 | driver.findElement(By.name("li2")).click(); 59 | 60 | System.out.println("Checking Box"); 61 | driver.findElement(By.name("li3")).click(); 62 | 63 | System.out.println("Checking Another Box"); 64 | driver.findElement(By.name("li4")).click(); 65 | 66 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6"); 67 | driver.findElement(By.id("addbutton")).click(); 68 | 69 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7"); 70 | driver.findElement(By.id("addbutton")).click(); 71 | 72 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8"); 73 | driver.findElement(By.id("addbutton")).click(); 74 | 75 | System.out.println("Checking Another Box"); 76 | driver.findElement(By.name("li1")).click(); 77 | 78 | System.out.println("Checking Another Box"); 79 | driver.findElement(By.name("li3")).click(); 80 | 81 | System.out.println("Checking Another Box"); 82 | driver.findElement(By.name("li7")).click(); 83 | 84 | System.out.println("Checking Another Box"); 85 | driver.findElement(By.name("li8")).click(); 86 | Thread.sleep(300); 87 | 88 | System.out.println("Entering Text"); 89 | driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It"); 90 | 91 | driver.findElement(By.id("addbutton")).click(); 92 | 93 | System.out.println("Checking Another Box"); 94 | driver.findElement(By.name("li9")).click(); 95 | 96 | // Let's also assert that the todo we added is present in the list. 97 | 98 | spanText = driver.findElementByXPath("/html/body/div/div/div/ul/li[9]/span").getText(); 99 | Assert.assertEquals("Get Taste of Lambda and Stick to It", spanText); 100 | status = "passed"; 101 | Thread.sleep(150); 102 | 103 | // Clearing browser Cache after Test 104 | driver.manage().deleteAllCookies(); // delete all cookies 105 | Thread.sleep(7000); // wait 7 seconds to clear cookies. 106 | 107 | System.out.println("TestFinished"); 108 | 109 | } 110 | 111 | @After 112 | public void tearDown() throws Exception { 113 | if (driver != null) { 114 | driver.executeScript("lambda-status=" + status); 115 | driver.quit(); 116 | } 117 | } 118 | } 119 | --------------------------------------------------------------------------------