How to Handle Cookies with Selenium Java on LambdaTest?

How to Handle Cookies with Selenium Java on LambdaTest?

Hi Ana!

In order to handle Cookies for Java automation testing with Selenium, you can use the below code snippet -

        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

Check out the below GitHub repo for the same -