Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize current browser window in Selenium WebDriver with Java?

I need to resize the browser as 300x400 during executing the Selenium automated tests. How can I resize the browser window in Selenium WebDriver (aka, Selenium 2) with Java?

[Note: It needs to resize the browser window for testing Responsive Web Design (RWD)]

like image 394
Ripon Al Wasim Avatar asked May 21 '13 07:05

Ripon Al Wasim


People also ask

How do I resize my browser window to a specific size?

Press Alt+Space to bring up the window menu, press S to choose the Size option, use the arrow keys to resize the window, and lastly Enter to confirm. Click the Maximize button in the top right corner of the window. Click the title bar and drag the window to the left, top, or right edge of the desktop.

How do I change the current window in Selenium?

Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.

Which of the following is used to set the browser window size to 600 by 400 in Selenium?

Selenium provides a direct command to maximize the browser window. The following command is used to maximize the browser window. int width = 600; int height = 400; Dimension dimension = new Dimension(width, height); driver. manage().


3 Answers

You can get a reference to the current window with driver.manage().window(). And the window has a setSize() method, so you could try

Dimension dimension = new Dimension(800, 600);
driver.manage().window().setSize(dimension)
like image 77
NilsH Avatar answered Nov 14 '22 06:11

NilsH


For Responsive Design it's better if you don't use fixed data, in this case screen resolution - users need most often (cause it's much less prone to errors) :

_driver.manage().window().maximize();

or

_driver.manage().window().fullScreen();
like image 32
Philip Avatar answered Nov 14 '22 06:11

Philip


If nothing works (to make tab in full screen only) for you, go for it:

driver.maximize_window()

where

driver = webdriver.Chrome(executable_path="D:\softwares\chromedriver.exe")

or could be any other webdriver locations.

like image 32
NAVNEET CHANDAN Avatar answered Nov 14 '22 06:11

NAVNEET CHANDAN