Thursday, June 27, 2024

Cookies Handling

 

Cookies Handling :

 

What is a Cookie?

A cookie is a piece of information stored in the browser.

For example, while filling out forms or adding products to a cart, information such as first name, last name, address, mobile number, etc., can be stored in cookies.

Viewing Cookies in Chrome Browser:

  1. Open udemy.com.
  2. Right-click on any element and select Inspect.
  3. Open the Application tab.
  4. Under Storage, click on Cookies.
  5. Select udemy.com.
  6. All cookies will be displayed in a tabular format.

Each cookie has the following attributes:

  • Name
  • Value
  • Domain
  • Path
  • Expiration Date
  • Security flag

Common Cookie Operations

  • Get cookies count
  • Delete cookies

 

package Utilities;

 

import java.util.Set;

 

import org.openqa.selenium.Cookie;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class GetCookiesCount {

 

            public static void main(String[] args) {

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();

                        // get("url ") - to open the url in chrome browser

                        driver.get("https://www.udemy.com/");

 

                        // get cookies cnt

                        Set<Cookie> allCookies =  driver.manage().getCookies();

                       

                         int cookiesCnt =  allCookies.size();

                         System.out.println("cookiesCnt=" + cookiesCnt);// cookiesCnt=19

 

 

                        // get all  cookies names from set ref var using for each looop

                        // get cookie name    

                        // get Cookie value

                        // Each cookie has some name , value.., Domain, expiry

                        // read cookie info using for each loop

                         for(Cookie eachCk :allCookies)

                         {

                                    // cookieName

                                     System.out.println("Cookie="+eachCk.getName());

                                    //cookieValue

 

                                     System.out.println("ckVal="+ eachCk.getValue());

                                     

                         }

                                   

                                                // HW get cookie Expiredate

                                     

 

                                                //  HW get cookie is  http

 

                                                //  HW  cookie is secured

                                     

 

                        //  HW use iterator()   use while loop to gel all cookies  from set object

 

 

                        // HW  use listIterartor() -- check

 

 

 

 

 

            }

 

}

 

           

o.p:

----

Oct 05, 2023 9:30:56 AM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch

WARNING: Unable to find an exact match for CDP version 117, so returning the closest version found: 116

cookiesCnt=18

cookieName=ud_cache_campaign_code

ckVal=ST20MT100423

cookieName=ud_cache_brand

ckVal=INen_US

cookieName=ud_cache_price_country

ckVal=IN

cookieName=__cfruid

ckVal=2ae9039a745025bf464804267a5d13bfc4f1c28a-1696478458

cookieName=ud_cache_language

ckVal=en

cookieName=__cf_bm

ckVal=CsE.7RpSGJ4xzFC3I56XPTjIKSX44FXF8.lLqBeZyUo-1696478458-0-AZKuS9lHk5eksT95qwibVXHb3+V0u8h9RyGgE2WqANfzC29CA18zRVwkeFpOg9BjyZ2G/2YGB59Qtj3cEHDyiVY=

cookieName=ud_cache_device

ckVal=None

cookieName=ud_rule_vars

ckVal="eJx9jUEOgyAURK9i2LaaDyooZyEhX0BLakoK6MZ495K0Tbrqcibz5h0kY1xcdlbvPvkcorS8s4xxAEpph9ZMHATjyAchnBXMShPC3TsiK3IoMvuY8pvVFrNTpVeEAWtrCjX0FXQSQPaiEQIYHS8lAChyLasVC5rDZm46R5xnb3QKWzRO7xg9TuvnLcQFH978QNE9N5f-G4dGjG0P_dd4kvMFdBVHjw==:1qoFXm:gLM33M1wX00NDtr6IMO-mNBbm0Y"

cookieName=__udmy_2_v57r

ckVal=d64d226001114adcb60726a6877ed72d

cookieName=ud_cache_marketplace_country

ckVal=IN

cookieName=ud_cache_logged_in

ckVal=0

cookieName=__udmy_1_a12z_c24t

ckVal=VGhlIGFuc3dlciB0byBsaWZlLCB0aGUgdW5pdmVyc2UsIGFuZCBldmVyeXRoaW5nIGlzIDQy

cookieName=evi

ckVal="3@8vSYOjNRtYbPlG4EDyr68Gvdl6sfDn7hIpuv0nEbjKdlG4jlmz-sCpur"

cookieName=ud_cache_user

ckVal=""

cookieName=ud_cache_release

ckVal=02f02c2b2df8d998efd1

cookieName=_dd_s

ckVal=rum=0&expire=1696479359098

cookieName=csrftoken

ckVal=2dR7PWcXkUGF7WJe2944uYt4TfFNFdYJ6UbgzDvVGT8pYF1DQmJXqsghmRy5Jbej

cookieName=ud_cache_version

ckVal=1

 

           

ex2:  Deleting all cookies

package Utilities;

 

import java.util.Set;

 

import org.openqa.selenium.Cookie;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class GetCookiesCount {

 

            public static void main(String[] args) {

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();

                        // get("url ") - to open the url in chrome browser

                        driver.get("https://www.udemy.com/");

 

                        // get cookies cnt

                        Set<Cookie> allCookies =  driver.manage().getCookies();

                       

                         int cookiesCnt =  allCookies.size();

                         System.out.println("cookiesCnt=" + cookiesCnt);// cookiesCnt=19

 

                         // Delete all cookies

                         driver.manage().deleteAllCookies();

                         

                         cookiesCnt =  allCookies.size();

                         System.out.println("cookiesCnt=" + cookiesCnt);// cookiesCnt=19

                         

            }

 

}

 

//  HW open facebook.com  and get all cookie count, cookie names  and values  and delete all cookies?

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...