Skip to content

A sample repo to help you handle cookies for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

Notifications You must be signed in to change notification settings

praveenLT/java-selenium-cookie-handling

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to handle cookies for automation test in Java-selenium on LambdaTest

Java

Prerequisites

  1. Install and set environment variable for java.
     install maven
    

Managing cookies

To manage cookies, the following code can be used:

        driver.manage().addCookie(new Cookie("cookieName", "lambdatest")); // Creates and adds the cookie

        Set<Cookie> cookiesSet = driver.manage().getCookies(); // Returns the List of all Cookies

        for (Cookie itemCookie : cookiesSet) {
            System.out.println((itemCookie.getName() + ";" + itemCookie.getValue() + ";" + itemCookie.getDomain() + ";"
                    + itemCookie.getPath() + ";" + itemCookie.getExpiry() + ";" + itemCookie.isSecure()));
        }

        driver.manage().getCookieNamed("cookieName"); // Returns the specific cookie according to name

        driver.manage().deleteCookie(driver.manage().getCookieNamed("cookieName")); // Deletes the specific cookie
        driver.manage().deleteCookieNamed("cookieName"); // Deletes the specific cookie according to the Name
        driver.manage().deleteAllCookies(); // Deletes all the cookies

Run your First Test

  1. Clone the Java-Selenium-Sample repository.
git clone https://github.com/LambdaTest/java-selenium-sample.git
  1. Next get into Java-Selenium-Sample folder, and import Lamabdatest Credentials. You can get these from lambdatest automation dashboard.

    For Linux/macOS::

export LT_USERNAME="YOUR_USERNAME"
export LT_ACCESS_KEY="YOUR ACCESS KEY"

For Windows:

set LT_USERNAME="YOUR_USERNAME"
set LT_ACCESS_KEY="YOUR ACCESS KEY"

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.

 mvn versions:display-dependency-updates

Step 4. Run single test.

mvn clean install exec:java -Dexec.mainClass="com.lambdatest.Cookies" -Dexec.classpathScope=test -e

About LambdaTest

LambdaTest 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 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.