Cookies in selenium:
What is Cookie:
- Cookie is a small amount of website and saved by your web browser.
- Its purpose is to remember information about you, similar to the preference file created by the software application.
- The most common purpose is to store login information for a specific site.
- Some sites save both your username and password in a cookie, while others will only save your username.
- Whenever you say, “Remember me on this computer,” check a box, then the website will successfully generate a login cookie after logging in.
- Cookies are a way of remembering users and archiving the information in a cookie file as adding key values to their conversation with the site.
- HTTP cookie is also known as a web cookie, a browser cookie or Internet cookie.
- A text file exists in the client machine or PC’s web browser, which stores a bit of information.
Why Handle Cookies in Selenium?
- Each cookie is associated with a name, value, domain, path, termination, and status of whether it is safe or not. To validate the client, a cookie parses all these values into a cookie.
- When testing a web application using the Selenium Webdriver, you may need to create, update or delete a cookie.
- For example, when automating online shopping apps, you need to automate many people such as test capture orders, order carts, payment information, order confirmation, etc.
Package:
1 | import org.openqa.selenium.Cookie; |
Selenium WebDriver Commands For Handle Cookies:
Get Cookies: Using this statement is used to return the list of all cookies stored in the web browser.
1 | manage().getCookies(); |
Get Cookie by name: Using this statement is used to return the specific cookie according to its name.
1 | manage().getCookieNamed(arg0); |
Add Cookie: Using this statement is used to create and add the cookie.
1 | manage().addCookie(arg0); |
Delete Cookie: Using this statement is used to delete a specific cookie.
1 | manage().deleteCookie(arg0); |
Delete Cookie by name: Using this statement is used to delete a cookie according to its name.
1 | manage().deleteCookieNamed(arg0); |
Delete All Cookie: Using this statement is used to delete all cookies.
1 | manage().deleteAllCookies(); |
Example of Cookies in selenium:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.io.IOException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class DC_Scroll { public static void main(String[] args) throws IOException { // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Go to URL driver.get("https://www.stqatools.com/"); // Return The List of all Cookies driver.manage().getCookies(); // Return specific cookie according to name driver.manage().getCookieNamed(""); // Create and add the cookie driver.manage().addCookie(null); // Delete specific cookie driver.manage().deleteCookie(null); // Delete specific cookie according Name driver.manage().deleteCookieNamed(""); // Delete all cookies driver.manage().deleteAllCookies(); } } |
Cookies Handling in Selenium WebDriver and How to handle Cookies in Selenium WebDriver and How to Add, Retrieve, and Delete Cookies in WebDriver