├── .gitignore ├── .gitpod.yml ├── README.md ├── pom.xml └── src └── test └── com └── lambdatest └── Cookies.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.Cookies" -Dexec.classpathScope=test -e 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to handle cookies for automation test in Java-selenium on LambdaTest 2 | ![Java](https://www.lambdatest.com/support/assets/images/og-images/Java-with-Selenium-1-1.jpg) 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 | ### Managing cookies 16 | 17 | To manage cookies, the following code can be used: 18 | ```java 19 | driver.manage().addCookie(new Cookie("cookieName", "lambdatest")); // Creates and adds the cookie 20 | 21 | Set cookiesSet = driver.manage().getCookies(); // Returns the List of all Cookies 22 | 23 | for (Cookie itemCookie : cookiesSet) { 24 | System.out.println((itemCookie.getName() + ";" + itemCookie.getValue() + ";" + itemCookie.getDomain() + ";" 25 | + itemCookie.getPath() + ";" + itemCookie.getExpiry() + ";" + itemCookie.isSecure())); 26 | } 27 | 28 | driver.manage().getCookieNamed("cookieName"); // Returns the specific cookie according to name 29 | 30 | driver.manage().deleteCookie(driver.manage().getCookieNamed("cookieName")); // Deletes the specific cookie 31 | driver.manage().deleteCookieNamed("cookieName"); // Deletes the specific cookie according to the Name 32 | driver.manage().deleteAllCookies(); // Deletes all the cookies 33 | ``` 34 | ### Run your First Test 35 | 1. Clone the Java-Selenium-Sample repository. 36 | ``` 37 | git clone https://github.com/LambdaTest/java-selenium-sample.git 38 | ``` 39 | 2. Next get into Java-Selenium-Sample folder, and import Lamabdatest Credentials. You can get these from lambdatest automation dashboard. 40 |

41 | For Linux/macOS:: 42 | 43 | ``` 44 | export LT_USERNAME="YOUR_USERNAME" 45 | export LT_ACCESS_KEY="YOUR ACCESS KEY" 46 | ``` 47 |

48 | For Windows: 49 | 50 | ``` 51 | set LT_USERNAME="YOUR_USERNAME" 52 | set LT_ACCESS_KEY="YOUR ACCESS KEY" 53 | ``` 54 | 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. 55 | ``` 56 | mvn versions:display-dependency-updates 57 | ``` 58 | Step 4. Run single test. 59 | ``` 60 | mvn clean install exec:java -Dexec.mainClass="com.lambdatest.Cookies" -Dexec.classpathScope=test -e 61 | ``` 62 | 63 | ## About LambdaTest 64 | 65 | [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. 66 | 67 | -------------------------------------------------------------------------------- /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/Cookies.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import java.net.MalformedURLException; 4 | import java.net.URL; 5 | import java.util.Set; 6 | 7 | import org.openqa.selenium.By; 8 | import org.openqa.selenium.Cookie; 9 | import org.openqa.selenium.remote.DesiredCapabilities; 10 | import org.openqa.selenium.remote.RemoteWebDriver; 11 | 12 | public class Cookies { 13 | 14 | private RemoteWebDriver driver; 15 | private String Status = "failed"; 16 | 17 | 18 | public void setup() throws MalformedURLException { 19 | String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); 20 | String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); 21 | ; 22 | String hub = "@hub.lambdatest.com/wd/hub"; 23 | 24 | DesiredCapabilities caps = new DesiredCapabilities(); 25 | caps.setCapability("platform", "Windows 10"); 26 | caps.setCapability("browserName", "Chrome"); 27 | caps.setCapability("version", "latest"); 28 | caps.setCapability("build", "Cookies Test"); 29 | caps.setCapability("name", this.getClass().getName()); 30 | caps.setCapability("plugin", "git-testng"); 31 | 32 | String[] Tags = new String[] { "Feature", "Falcon", "Severe" }; 33 | caps.setCapability("tags", Tags); 34 | 35 | driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), caps); 36 | 37 | } 38 | 39 | public void cookiesTest() throws InterruptedException { 40 | 41 | String spanText; 42 | System.out.println("Loading Url"); 43 | 44 | driver.get("https://lambdatest.github.io/sample-todo-app/"); 45 | 46 | driver.manage().addCookie(new Cookie("cookieName", "lambdatest")); // Creates and adds the cookie 47 | 48 | Set cookiesSet = driver.manage().getCookies(); // Returns the List of all Cookies 49 | 50 | for (Cookie itemCookie : cookiesSet) { 51 | System.out.println((itemCookie.getName() + ";" + itemCookie.getValue() + ";" + itemCookie.getDomain() + ";" 52 | + itemCookie.getPath() + ";" + itemCookie.getExpiry() + ";" + itemCookie.isSecure())); 53 | } 54 | 55 | driver.manage().getCookieNamed("cookieName"); // Returns the specific cookie according to name 56 | 57 | driver.manage().deleteCookie(driver.manage().getCookieNamed("cookieName")); // Deletes the specific cookie 58 | driver.manage().deleteCookieNamed("cookieName"); // Deletes the specific cookie according to the Name 59 | driver.manage().deleteAllCookies(); // Deletes all the cookies 60 | 61 | System.out.println("Checking Box"); 62 | driver.findElement(By.name("li1")).click(); 63 | 64 | System.out.println("Checking Another Box"); 65 | driver.findElement(By.name("li2")).click(); 66 | 67 | System.out.println("Checking Box"); 68 | driver.findElement(By.name("li3")).click(); 69 | 70 | System.out.println("Checking Another Box"); 71 | driver.findElement(By.name("li4")).click(); 72 | 73 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6"); 74 | driver.findElement(By.id("addbutton")).click(); 75 | 76 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7"); 77 | driver.findElement(By.id("addbutton")).click(); 78 | 79 | driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8"); 80 | driver.findElement(By.id("addbutton")).click(); 81 | 82 | System.out.println("Checking Another Box"); 83 | driver.findElement(By.name("li1")).click(); 84 | 85 | System.out.println("Checking Another Box"); 86 | driver.findElement(By.name("li3")).click(); 87 | 88 | System.out.println("Checking Another Box"); 89 | driver.findElement(By.name("li7")).click(); 90 | 91 | System.out.println("Checking Another Box"); 92 | driver.findElement(By.name("li8")).click(); 93 | Thread.sleep(300); 94 | 95 | System.out.println("Entering Text"); 96 | driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It"); 97 | 98 | driver.findElement(By.id("addbutton")).click(); 99 | 100 | System.out.println("Checking Another Box"); 101 | driver.findElement(By.name("li9")).click(); 102 | 103 | // Let's also assert that the todo we added is present in the list. 104 | 105 | spanText = driver.findElement(By.xpath("/html/body/div/div/div/ul/li[9]/span")).getText(); 106 | "Get Taste of Lambda and Stick to It".equalsIgnoreCase(spanText); 107 | Status = "passed"; 108 | Thread.sleep(150); 109 | 110 | System.out.println("TestFinished"); 111 | 112 | } 113 | 114 | public void tearDown() { 115 | driver.executeScript("lambda-status=" + Status); 116 | driver.quit(); 117 | } 118 | 119 | public static void main(String[] args) throws MalformedURLException, InterruptedException { 120 | Cookies test = new Cookies(); 121 | test.setup(); 122 | test.cookiesTest(); 123 | test.tearDown(); 124 | } 125 | 126 | } --------------------------------------------------------------------------------