Handle Cookies in Selenium Automation Testing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Welcome to our ADM Lucid automation testing Channel we'll have a series of talk about selenium automation testing which will help you understand the basics of selenium and also guide you to build your own selenium automation project you may access our test project and guide in the description below feel free to subscribe to our channel for any new videos or updates in today's video we will talk about selenium automation testing with cookies so automated testing of web applications with selenium often involves interactions with cookies which are small pieces of data stored in the user's web browser cookies are commonly used for various purposes including session management user authentication and tracking user behavior in selenium you can interact with cookies using the web driver's built-in methods here's how you can work with cookies in selenium automation testing so first accessing cookies to access cookies you can use the get cookies method provided by the web driver this method returns a set of cookies objects next adding cookies you can add cookies to the browser session using the add cookie method this is useful for setting up specific scenarios such as logging in as a user with a particular session next is deleting cookies to delete cookies you can use the delete cookie method you can delete a specific Cookie by specifying its name or you can delete all cookies next is getting a specific cookie you can retrieve a specific Cookie by its name using the get cookie named method and finally um there is verifying cookie attributes you can also check the attributes of a cookie such as its name value domain path and expiration dat date next I will show you the code implementation of how to work with cookies in selenium Java so first right here we have a controller class called cookies and in this class I'm going to direct your attention to this first uh first method called add cookie and in this method uh I'm just going to walk through what we do so the goal of this method is to add cookies into your browser so we can look and we see that first right here we create two cookies we do um a cookie that has the name test two and a and a cookie that has the name test one both with the values uh with cookie two and cookie one and basically this creates the two cookies next we do um driver manager. getcookies and we set it equal to this array and these are basically a set of cookies that we get um and this set of cookies not only includes the two cookies that we made earlier but also includes all the cookies that we had on this website ADM lucid.com dglf after that we can look at some stuff regarding the cookies for example here we can get the cookie name for test two so we can look at the cookie named test two and so this is the ad cookie method and what I'm going to do is I'm going to go to our test class here and I'm just going to run this one method so I'll copy this at cookie save that okay and so we made it so that after it finishes running it doesn't close the browser uh the reason we did this is because we want to show you something I want to go here I want to go settings or actually I want to go to more tools and then I'm going to go to developer tools I'm going to click on network and right now there's nothing here so all you have to do is you just have to refresh and everything will show up I'm going to click golf one time and I'm going to scroll down to cookies and right here we have cookies and we see that it has all these cookies that are native to the website that we have ADM lu. golf uh but we also have the two cookies that we added through our selenium code so Test 2 is equal to cookie two and test one is equal to cookie one so that just shows that the method works and um I just want to show you that we also have all our um our runs so that it exports a file here and right here we have the the output from our runs so we see that we created two cookies um and both of them uh were created successfully and we're able to retrieve all the cookies so I'm going to go ahead and close this XM report here and this time I'm going to move our attention to the next method in our class so the next method in our class is delete cookie and the goal of this method as the name implies is we want to delete cookies so in this class what we first do is we add two cookies into our web page as well as we get all the cookies for ADM lucid.com so this is the homepage and we see here we're getting all the cookies and then I want to show you right here we use do delete all cookies and this deletes all the cookies so I'm just going to show you how this works so I'm going to go down back to our test class I'm going to uncomment this and I'm going to go ahead and run and like last time I'm going to go to more tools developer tools Network and I'm going to refresh the page click ADM lucid and we scroll all the way down and this time we see that we can't even find a row for cookies and the reason that is is because we used the method delete all cookies so this just verifies that what we did worked correctly and we can further verify it by going to the extend report opening it up and we see that we created the two cookies and then we deleted all the cookies afterwards now finally I want to direct your attention to the final method so I'm go ahead and comment this out first go back to cookies here and the final method is called verify cookie attribute and basically what this does is we want to verify different attributes of the cookie like the name applies so again right here um we are going to this website and we added a cookie called test two cookie two and then we grabed um the attributes for this specific cookie that we created and we see right here we are grabbing each value so we can grab its name its value its domain its path its expiry and we can test uh to see if that cookie properties is what we expect it to be and so what I'm going to do is go ahead and uncomment in the test class this and I'm going to run this again and this time we're not going to be using developer tools so we just made it so the browser closes automatically and uh we can go all the way down here or actually we can just go to the exam report and we can see that we were able to grab all these different values for the cookie so it's expiry its path its domain its value and its name and basically uh with these basic functions regarding cookies you're able to work with cookies in your own solum code for whatever purpose you want to use it for so I hope this video was a little helpful in introducing some of the basic functions and methods that we can use to deal with cookies if you enjoyed the video and you found it helpful please give this video a like And subscribe to our Channel and we uh and stay tuned to any new videos that we'll be posting thank you
Info
Channel: Test Automation 101
Views: 706
Rating: undefined out of 5
Keywords: Selenium automation, Cookie management, Selenium cookies, Cookie handling, Automation testing
Id: JW1uArD11Ig
Channel Id: undefined
Length: 8min 42sec (522 seconds)
Published: Fri Oct 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.