Manage cookies in a script execution

There are several ways to add cookies to a script. Each of them requires a browser to be open and in focus during the test execution.

Limitations

Due to a Safari limitation, do not use the deleteAllCookies() command during a Mac desktop web session on Safari.

Perfecto functions

browser:cookie:add

The domain property is not mandatory. If not present, the cookie will be added for the website currently open.

Important: Make sure an application that accepts cookies (such as a browser) is in focus prior to the execution of the cookie functions.
Copy
HashMap params = new HashMap<>();

params.put("name", "myCookie");
params.put("value", "67890");
params.put("domain", "mydomain.org")
String res = (String) driver.executeScript("mobile:cookie:add", params);

browser:cookies:get

Use this function to get all cookies in the browser.

Copy
String res = (String) driver.executeScript("mobile:cookies:get");

The 'res' variable will hold the string representation of a JSON-formatted array of all cookies currently in the browser.

Use the REST API

To learn more, see Get all cookies.

Selenium functions

To add cookies, use the addCookie function.

Copy
driver.manage().addCookie(new Cookie("myCookie", "12345"));

To get all cookies, use the getCookies function.

Copy
Set<Cookie> allcookies = driver.manage().getCookies();

To delete all cookies, use the deleteAllCookies function.

Copy
driver.manage().deleteAllCookies();