Selenium
TestNG Tutorial: What is, Annotations & Framework in Selenium
What is TestNG? TestNG is an automation testing framework in which NG stands for "Next...
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.gtupapers.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.gtupapers.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.gtupapers.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 TestNG? TestNG is an automation testing framework in which NG stands for "Next...
During test automation of web-based application, there comes a need for the page to be refreshed...
Using the Java class "myclass" that we created in the previous tutorial, let us try to create a...
Accessing Image Links Image links are the links in web pages represented by an image which when...
What is Log4j? Log4j is a fast, flexible and reliable logging framework (APIS) written in Java...
What is JavaScriptExecutor? JavaScriptExecutor is an Interface that helps to execute JavaScript...