Setting browser window size in Watir-webdriver
Asked Answered
P

3

21

How can you specify the size of the browser window opened when you call the following with watir-webdriver?

browser = Watir::Browser.new(:firefox)
Plumber answered 25/5, 2011 at 14:44 Comment(0)
D
33

This works only for Firefox at the moment:

browser.window.resize_to(800, 600)

and you can move the browser, too:

browser.window.move_to(0, 0)
Danielson answered 25/5, 2011 at 15:12 Comment(11)
Thanks will try it tomorrow :-)Plumber
Thanks, this was the answer I didn't know I needed, too. It really helped with Jarmo's Win32Screenshot capability, too: Win32::Screenshot::Take.of(:desktop, :area => [0,0,1024,768])Alhambra
@adam: watir-webdriver has screen shots built in: browser.driver.save_screenshot("file_name.png")Typo
Make that two answers I didn't know I needed. I was experimenting with vanilla watir & Jarmo's library first before pulling it into the latest webdriver script - looks like I would be better-served with this one. Thanks!Alhambra
I'm glad I could help. More gems like these will soon be in my Watir book. watir.com/book :)Typo
@ŽeljkoFilipin When using b.window.resize_to(400,400) and b.driver.save_screenshot("screenshot.jpg") the screenshot is not saved with the 400x400 and keep the original browser size. Any idea why?Interregnum
@Martin: I will reply at #17828777Typo
@ŽeljkoFilipin Is there a way to size the window proportionally to the screen or is that outside the scope of Watir? I'd like to have the browser take up half the screen.Trembly
What about IE? How can I maximize IE browser?Cartoon
@ŽeljkoFilipin - Is Watir the same as Watir-Webdriver ? Thanks. PS - I don't want to make a question for this because it will be downvoted.Tynan
Works for PhantomJS too! Thanks!Barling
E
8

I'm using ruby+watir-webdriver and this code works for both FF and IE browsers (I have not checked in others browsers)

screen_width = browser.execute_script("return screen.width;")
screen_height = browser.execute_script("return screen.height;")
browser.driver.manage.window.resize_to(screen_width,screen_height)
browser.driver.manage.window.move_to(0,0)
Evered answered 24/1, 2012 at 11:52 Comment(0)
M
7

I did something like this

browser = Watir::Browser.new :firefox, :profile => profile
browser.send_keys :f11
Macleod answered 28/11, 2012 at 16:27 Comment(1)
@DavidWest Seems like a hack, but does the job ;).. thanks for the upvote mate :)Macleod

© 2022 - 2024 — McMap. All rights reserved.