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 currently opened website.

Important: Make sure an application that accept cookies (i.e. a browser) is in focus prior 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

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

For details please refer to the following article: Get all cookies

Selenium functions

To add cookies use:

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

To get all cookies:

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

To delete all cookies:

Copy
driver.manage().deleteAllCookies();