├── .gitignore
├── .gitpod.yml
├── README.md
├── pom.xml
└── src
└── test
└── com
└── lambdatest
└── BrowserCache.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 | /target/
4 | .vscode/
5 |
6 | # Log file
7 | *.log
8 |
9 | # BlueJ files
10 | *.ctxt
11 |
12 | # Mobile Tools for Java (J2ME)
13 | .mtj.tmp/
14 |
15 | # Package Files #
16 | *.jar
17 | *.war
18 | *.nar
19 | *.ear
20 | *.zip
21 | *.tar.gz
22 | *.rar
23 |
24 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
25 | hs_err_pid*
26 |
--------------------------------------------------------------------------------
/.gitpod.yml:
--------------------------------------------------------------------------------
1 | # List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/
2 | tasks:
3 | - init: echo 'init script' # runs during prebuild
4 | command: mvn clean install exec:java -Dexec.mainClass="com.lambdatest.BrowserCache" -Dexec.classpathScope=test -e
5 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # How to clear browser cache with Selenium 4 Java on LambdaTest cloud
2 | 
3 |
4 | ### Prerequisites
5 | 1. Install and set environment variable for java.
6 | * Windows - https://www.oracle.com/java/technologies/downloads/
7 | * Linux - ``` sudo apt-get install openjdk-8-jre ```
8 | * MacOS - Java should already be present on Mac OS X by default.
9 | 2 Install and set environment varibale for Maven.
10 | * Windows - https://maven.apache.org/install.html
11 | * Linux/ MacOS - [Homebrew](http://brew.sh/) (Easier)
12 | ```
13 | install maven
14 | ```
15 | ### Clearing browser cache
16 | To clear browser cache, the following line of code can be used:
17 |
18 | ```java
19 | // Clearing browser Cache after Test
20 | driver.manage().deleteAllCookies(); // delete all cookies
21 | Thread.sleep(7000); // wait 7 seconds to clear cookies.
22 | ```
23 | ### Run your First Test
24 | 1. Clone the Java-Selenium-Sample repository.
25 | ```
26 | git clone https://github.com/LambdaTest/java-selenium-sample.git
27 | ```
28 | 2. Next get into Java-Selenium-Sample folder, and import Lamabdatest Credentials. You can get these from lambdatest automation dashboard.
29 |
30 | For Linux/macOS::
31 |
32 | ```
33 | export LT_USERNAME="YOUR_USERNAME"
34 | export LT_ACCESS_KEY="YOUR ACCESS KEY"
35 | ```
36 |
37 | For Windows:
38 |
39 | ```
40 | set LT_USERNAME="YOUR_USERNAME"
41 | set LT_ACCESS_KEY="YOUR ACCESS KEY"
42 | ```
43 | Step 3. You may also want to run the command below to check for outdated dependencies. Please be sure to verify and review updates before editing your pom.xml file as they may not be compatible with your code.
44 | ```
45 | mvn versions:display-dependency-updates
46 | ```
47 | Step 4. Run single test.
48 | ```
49 | mvn clean install exec:java -Dexec.mainClass="com.lambdatest.BrowserCache" -Dexec.classpathScope=test -e
50 | ```
51 |
52 | ## About LambdaTest
53 |
54 | [LambdaTest](https://www.lambdatest.com/) is a cloud based selenium grid infrastructure that can help you run automated cross browser compatibility tests on 2000+ different browser and operating system environments. LambdaTest supports all programming languages and frameworks that are supported with Selenium, and have easy integrations with all popular CI/CD platforms. It's a perfect solution to bring your [selenium automation testing](https://www.lambdatest.com/selenium-automation) to cloud based infrastructure that not only helps you increase your test coverage over multiple desktop and mobile browsers, but also allows you to cut down your test execution time by running tests on parallel.
55 |
56 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | java-selenium-sample
4 | java-selenium-sample
5 | 0.0.1
6 |
7 | src/test
8 |
9 |
10 | src/test
11 |
12 | **/*.java
13 |
14 |
15 |
16 |
17 |
18 | maven-compiler-plugin
19 | 3.7.0
20 |
21 | 10
22 |
23 |
24 |
25 | org.apache.maven.plugins
26 | maven-surefire-plugin
27 | 3.0.0-M5
28 |
29 |
30 |
31 |
32 |
33 | org.seleniumhq.selenium
34 | selenium-java
35 | 4.1.0
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/src/test/com/lambdatest/BrowserCache.java:
--------------------------------------------------------------------------------
1 | package com.lambdatest;
2 |
3 | import java.net.MalformedURLException;
4 | import java.net.URL;
5 |
6 |
7 | import org.openqa.selenium.By;
8 | import org.openqa.selenium.remote.DesiredCapabilities;
9 | import org.openqa.selenium.remote.RemoteWebDriver;
10 |
11 | public class BrowserCache {
12 |
13 | private RemoteWebDriver driver;
14 | private String Status = "failed";
15 |
16 | public void setup() throws MalformedURLException {
17 | String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
18 | String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
19 | ;
20 | String hub = "@hub.lambdatest.com/wd/hub";
21 |
22 | DesiredCapabilities caps = new DesiredCapabilities();
23 | caps.setCapability("platform", "Windows 10");
24 | caps.setCapability("browserName", "Chrome");
25 | caps.setCapability("version", "latest");
26 | caps.setCapability("build", "Extension Test");
27 | caps.setCapability("name", this.getClass().getName());
28 | caps.setCapability("plugin", "git-testng");
29 |
30 | String[] Tags = new String[] { "Feature", "Falcon", "Severe" };
31 | caps.setCapability("tags", Tags);
32 |
33 | driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), caps);
34 |
35 | }
36 |
37 | public void browserCacheTest() throws InterruptedException {
38 | String spanText;
39 | System.out.println("Loading Url");
40 |
41 | driver.get("https://lambdatest.github.io/sample-todo-app/");
42 |
43 | System.out.println("Checking Box");
44 | driver.findElement(By.name("li1")).click();
45 |
46 | System.out.println("Checking Another Box");
47 | driver.findElement(By.name("li2")).click();
48 |
49 | System.out.println("Checking Box");
50 | driver.findElement(By.name("li3")).click();
51 |
52 | System.out.println("Checking Another Box");
53 | driver.findElement(By.name("li4")).click();
54 |
55 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6");
56 | driver.findElement(By.id("addbutton")).click();
57 |
58 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7");
59 | driver.findElement(By.id("addbutton")).click();
60 |
61 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8");
62 | driver.findElement(By.id("addbutton")).click();
63 |
64 | System.out.println("Checking Another Box");
65 | driver.findElement(By.name("li1")).click();
66 |
67 | System.out.println("Checking Another Box");
68 | driver.findElement(By.name("li3")).click();
69 |
70 | System.out.println("Checking Another Box");
71 | driver.findElement(By.name("li7")).click();
72 |
73 | System.out.println("Checking Another Box");
74 | driver.findElement(By.name("li8")).click();
75 | Thread.sleep(300);
76 |
77 | System.out.println("Entering Text");
78 | driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It");
79 |
80 | driver.findElement(By.id("addbutton")).click();
81 |
82 | System.out.println("Checking Another Box");
83 | driver.findElement(By.name("li9")).click();
84 |
85 | // Let's also assert that the todo we added is present in the list.
86 |
87 | spanText = driver.findElement(By.xpath("/html/body/div/div/div/ul/li[9]/span")).getText();
88 | "Get Taste of Lambda and Stick to It".equalsIgnoreCase(spanText);
89 | Status = "passed";
90 | Thread.sleep(150);
91 |
92 | // Clearing browser Cache after Test
93 | driver.manage().deleteAllCookies(); // delete all cookies
94 | Thread.sleep(7000); // wait 7 seconds to clear cookies.
95 |
96 | System.out.println("TestFinished");
97 |
98 | }
99 |
100 | public void tearDown() {
101 | driver.executeScript("lambda-status=" + Status);
102 | driver.quit();
103 | }
104 |
105 |
106 | public static void main(String[] args) throws MalformedURLException, InterruptedException {
107 | BrowserCache test = new BrowserCache();
108 | test.setup();
109 | test.browserCacheTest();
110 | test.tearDown();
111 | }
112 |
113 | }
--------------------------------------------------------------------------------