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.
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:
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:
driver.manage().addCookie(new Cookie("myCookie", "12345"));
To get all cookies:
Set<Cookie> allcookies = driver.manage().getCookies();
To delete all cookies:
driver.manage().deleteAllCookies();