Working with cookies
A cookie is a small piece of data that is sent from a website and stored in your computer. Cookies are mostly used to recognise the user and load the stored information.
WebDriver API provides a way to interact with cookies with built-in methods:
Add Cookie
It is used to add a cookie to the current browsing context. Add Cookie only accepts a set of defined serializable JSON object. Here is the link to the list of accepted JSON key values
First of all, you need to be on the domain that the cookie will be valid for. If you are trying to preset cookies before you start interacting with a site and your homepage is large / takes a while to load an alternative is to find a smaller page on the site (typically the 404 page is small, e.g. http://example.com/some404page)
Get Named Cookie
It returns the serialized cookie data matching with the cookie name among all associated cookies.
Get All Cookies
It returns a ‘successful serialized cookie data’ for current browsing context. If browser is no longer available it returns error.
Delete Cookie
It deletes the cookie data matching with the provided cookie name.
Delete All Cookies
It deletes all the cookies of the current browsing context.
Same-Site Cookie Attribute
It allows a user to instruct browsers to control whether cookies are sent along with the request initiated by third party sites. It is introduced to prevent CSRF (Cross-Site Request Forgery) attacks.
Same-Site cookie attribute accepts two parameters as instructions
Strict:
When the sameSite attribute is set as Strict, the cookie will not be sent along with requests initiated by third party websites.
Lax:
When you set a cookie sameSite attribute to Lax, the cookie will be sent along with the GET request initiated by third party website.
Note: As of now this feature is landed in chrome(80+version), Firefox(79+version) and works with Selenium 4 and later versions.