Selenium
How to Scroll Down or UP a Page in Selenium Webdriver
What is a Scrollbar? A Scrollbar is a lets you move around screen in horizontal or vertical...
In this tutorial, you will learn how to maximize, minimize or resize the browser using selenium Webdriver. Explained through different scenarios using maximize() method and dimensions for resizing the browser.
Here is what we cover:
Elements on the web application may not be recognized by the selenium if the browser is not maximized and thereby making framework fail. Hence, Maximize the browser is very important part of selenium framework. It is good practice to maximize the browser while automating any web application. When the user executes the selenium framework or any script, browser may not be in the full screen state and you need to maximize the browser to view all the elements of the web application. It is good to maximize the browser at the start of the script, so that the script gets executed successfully without any error.
To maximize a browser window, you need to call the maximize() method of the Window interface of the driver class.
void maximize() – This method is used to maximize the current browser.
You can customize the size of the browser according to the requirement of the scenario. Selenium webdriver does not provide any method for minimizing the browser, there is no such direct method. You need to use resize method to minimize the browser.
void setSize() – This method is used to set the size of the current browser. Dimension getSize() – This method is used to get the size of the browser in height and width. It returns the dimension of the browser. Point setPosition() – This method is used to set the position of the current browser.
Script Description : In the below Selenium script shown the resize of the browser using testNG framework , steps of the scenario are :
import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Resize { public static void main(String args[]) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe"); driver= new ChromeDriver(); // Launch the application driver.get("https://www.guru99.com/"); Dimension d = new Dimension(300,1080); //Resize current window to the set dimension driver.manage().window().setSize(d); //To Delay execution for 10 sec. as to view the resize browser Thread.sleep(10000); //Close the browser driver.quit(); } }
Opened the chrome browser , resized the browser, wait for a few seconds and closed the browser.
Script Description : In the below Selenium script shown the maximize of the browser using testNG framework , steps of the scenario are :
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Maximize { public static void main(String args[]) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe"); driver= new ChromeDriver(); // Launch the application driver.get("https://www.guru99.com/"); //Resize current window to the set dimension driver.manage().window().maximize(); //To Delay execution for 10 sec. as to view the maximize browser Thread.sleep(10000); //Close the browser driver.quit(); } }
Opened the chrome browser , maximized the browser, wait for a few seconds and closed the browser.
Script Description : In the below Selenium script shown the minimize of the browser using testNG framework , steps of the scenario are :
import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Minimize { public static void main(String args[]) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","E://Selenium//Selenium_Jars//chromedriver.exe"); driver= new ChromeDriver(); // Launch the application driver.get("https://www.guru99.com/"); Point p = new Point(0,3000); //Minimize the current window to the set position driver.manage().window().setPosition(p); //To Delay execution for 10 sec. as to view the minimize browser //you can view in the taskbar below of the screen. Thread.sleep(10000); //Close the browser driver.quit(); } }
Note: If the user wants to use Firefox browser, then user needs to set property of FirefoxDriver and create FirefoxDriver object instead of ChromeDriver in all above 3 scenarios scripts as given below:
System.setProperty("webdriver.gecko.driver","E://Selenium//Selenium_Jars//geckodriver.exe "); driver= new FirefoxDriver();
Opened the chrome browser , minimized the browser, wait for a few seconds and closed the browser.
Dimension d = new Dimension(300,1080); driver.manage().window().setSize(d);
driver.manage().window().maximize();
Point p = new Point(0,3000); driver.manage().window().setPosition(p);
What is a Scrollbar? A Scrollbar is a lets you move around screen in horizontal or vertical...
What is AutoIt? AutoIt is a freeware scripting language designed for automating windows GUI and...
Selenium Web driver is a web automation tool which enables you to run the tests against different...
In this tutorial, you will learn how to integrate Cucumber with Selenium Webdriver. What is...
In this tutorial, we will learn How to deal with file uploads and downloads. Uploading Files For...
Robot Class Robot Class in Selenium is used to enable automated testing for implementations of Java...