Using Selenium webdriver with Java, how to get dimension of a browser and screen resolution of the device that scripts are running ?
Free HD video link- https://www.youtube.com/watch?v=thNTpCloC1o&
Based on testing requirements or based on the invisibility of elements , sometimes we need to change the dimension of a browser .
Also test scripts are normally run on predefined screen resolutions , so that user expected device screen resolution must be tested . In order to see that scripts were ran in the same device resolution we need to have the ability to log the actual screen resolution of the device when scripts are running .
To get browser dimension, we can use org.openqa.selenium.Dimension.
To get device’s resolution we can use java.awt.Dimension
Note that screen resolution and browser is different.
* Webdriver interacts with browsers and these get method will retrieve the height and width of the browser window.
So this only return browser dimensions , not the screen resolution of the device
Sample browser sizes are available in https://browsersize.com/
What is height and width is explained in below image .
Screen resolution is mentioned below image
The code -
package com.webdriver.blogs;import java.awt.Toolkit;import java.util.concurrent.TimeUnit;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;import org.testng.annotations.AfterClass;import org.testng.annotations.Test;import org.openqa.selenium.Dimension;public class GetDimentionOfBrowserAndPrintMachineResolutionUsingJava {static WebDriver driver;@Testpublic void StartBrowser() throws InterruptedException{System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\Jar_files\\chromedriver.exe");ChromeOptions options = new ChromeOptions();// To start Chrome in Maximized browser windowoptions.addArguments("start-maximized");// To remove Chrome is being controlled by automated test softwareoptions.addArguments("disable-infobars");driver = new ChromeDriver(options);driver.manage().timeouts().implicitlyWait(29, TimeUnit.SECONDS);//wait until 60 seconds URL to load,unless go to timeoutdriver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);//Maximize windowdriver.manage().window().maximize();//Delete all cookiesdriver.manage().deleteAllCookies();}/** screen resolution and browser is different.* Webdriver interacts with browsers and these get method will retrieve the height and width of the browser window.* So this only return browser dimensions , not the screen resolution of the device*/@Test(priority=1,enabled=true,description="Get Dimension of the browser and change it ")public void verifyDimensionOfBrowser() {driver.get("https://browsersize.com/");Dimension initial_size = driver.manage().window().getSize();int height = initial_size.getHeight();int width = initial_size.getWidth();System.out.println("Default Height "+height +" Default width "+ width);driver.manage().window().setSize(new Dimension(800,600));initial_size = driver.manage().window().getSize();height = initial_size.getHeight();width = initial_size.getWidth();System.out.println(" Changed width "+ width+" Changed Height "+height );}@Test(priority=2,enabled=true,description="Get screenresolution of the machine that script is running ")public void getScreenResolutionOfTheDevice() {//Since Im using org.openqa.selenium.Dimension it's auto suggested to give java.awt.Dimensiojava.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();double width = screenSize.getWidth();double height = screenSize.getHeight();System.out.println("Resoultion of the deive : Screen width "+ width+" Screen Height "+height );}@AfterClass()public void shutDown() {driver.close();driver.quit();}}
Output -
Default Height 696 Default width 1296Changed width 800 Changed Height 600Resoultion of the deive : Screen width 1920.0 Screen Height 1080.0