How to Handle Browser Windows and Cookies In Selenium 4?

Reading Time: 3 minutes

Hello People, this article explains How to Handle and resize browser windows and cookies using the Selenium Webdriver. Resizing the browser window and handling cookies is important as it makes testing much easier. We can resize the browser window in Selenium Webdriver which actually helps in searching for the elements easier. In addition to this, this article also explains how to handle cookies like creating, adding cookies, or deleting cookies. Moreover, once you visit any website, cookies are sent to your machine by the website. Since these cookies contain information that helps the website keep track of your visits or activity. 

Use of Selenium other than Software Testing | Netwoven
  • Whenever WebDriver starts the browser, it creates it with default settings. For instance, resizing the browser window, mainly when we are testing for responsive websites because we need to check to see how the different elements on the page render when we resize the browser window.
  • Maximizing the browser is crucial in the selenium framework while automating any web application. Meanwhile executing the selenium framework or any script, the browser may not be in the full-screen state. Thus we need to maximize the browser so that all the elements of the web application are visible.
  • There are several ways to maximize or minimize the browser. Selenium Webdriver does not provide any direct method for minimizing the browser. For minimizing the browser in Selenium Webdriver, we need to use the resize method.

Methods to Resize Browser Window

Void setSize() – This will set the size of the current browser.

Void maximize() – This will maximize the current browser.

Dimension getSize() – This method returns the dimension of the browser in height and width.

Point setPosition() – This will set the position of the current browser.

How to Resize a Browser Window in Selenium Webdriver:

Below, the Selenium script depicts how to resize a browser. Therefore, the script will follow the following steps:

  • Opens the Google Chrome browser.
  • Navigate to the given site URL.
  • Resize the browser window.
  • Close the browser window.

Code Snippet:

public class ResizeBrowser {

public static void main(String args[]) 

{

System.setProperty("webdriver.chrome.driver");

WebDriver driver = new ChromeDriver();
  
driver.get("https://www.google.com/");
       
      Dimension d = new Dimension(200,1070);

      //Resize current window to set dimension

         driver.manage().window().setSize(d);
       
       //Close the browser

       driver.quit();

}

}

How to handle Cookies in Selenium Webdriver

Working with Cookies in Selenium Automated Tests - TestProject

A cookie is a small piece of data sent from a website and gets stored on the user’s computer. Certainly, Cookies are also used to recognize the user if they return to a website, and load the previously held information. Mainly, cookies store the user’s identity and track the user’s journey through the pages of the website.

Moreover, some of the commands used in Selenium for deleting, adding, and creating the list of cookies are listed below:

driver.manage().getCookies(); 

// Returns the List of all Cookies

driver.manage().getCookieNamed(arg0); 

//Returns the specific cookie according 

to name

driver.manage().addCookie(arg0); 

//Creates and adds the cookie

driver.manage().deleteCookie(arg0); 

// Deletes the specific cookie

driver.manage().deleteCookieNamed(arg0); 

// Deletes the specific cookie 

according to the Name

driver.manage().deleteAllCookies(); 

// Deletes all the cookies

How To Pass Cookies From Selenium WebDriver To Rest-Assured

Code Snippet:

import org.openqa.selenium.*;

import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Set;

public class getAllCookies {
    
public static void main(String[] args) {
       
 WebDriver driver = new ChromeDriver();
        
try {
            
driver.get("http://www.example.com");
            
// Add few cookies
            
driver.manage().addCookie(new Cookie("test1", 

"cookie1"));
            
driver.manage().addCookie(new Cookie("test2", 

"cookie2"));

            
// Get All available cookies
           
 Set<Cookie> cookies = 

driver.manage().getCookies();
            
System.out.println(cookies);

// delete a cookie with name 'test1'
            
driver.manage().deleteCookieNamed("test1");

            /*
// deletes all cookies
            
driver.manage().deleteAllCookies();

        } finally {
            
driver.quit();
       
 }
    
}

}
  

To sum up, the article, resizing the browser window helps in the proper execution of test cases. Most importantly it provides better visibility to the QA. Secondly, Resizing ensures that no element is left unidentified. In addition to this, it also explains different syntaxes to handle cookies.

For more Reference Visit:

https://www.selenium.dev/documentation/webdriver/

Leave a Reply