selenium tutorial testing tools

Set browser width and height in Selenium Webdriver

Selenium WebDriver allows resizing and maximizing window natively from its API. We use 'Dimension' class to resize the window.

Lets take the help of Selenium WebDrivers Dimension Class and declare object say 'd' by initializing it with width and height as 420X600 as shown below:

Remember, We need to import the statement 'import org.openqa.selenium.Dimension'

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class browser {
	
	@Test
	public void openBrowserwithGivenDimension()
	{

		WebDriver driver = new FirefoxDriver();
		driver.navigate().to("http://google.co.in");
		System.out.println(driver.manage().window().getSize());
		Dimension d = new Dimension(420,600);
		//Resize the current window to the given dimension
		driver.manage().window().setSize(d);
	}
}

We can set the size and perform testing with lower resolution. We can also perform testing on Responsive sites which automatically get adjust based on the browser size.

Selenium Tutorials: 

Comments

It was really useful, thanks !

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.