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