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)
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Alastair Montgomery
  • 1,756
  • 5
  • 21
  • 44

3 Answers3

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)
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125
  • Thanks will try it tomorrow :-) – Alastair Montgomery May 25 '11 at 15:16
  • 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]) – adam reed May 26 '11 at 19:11
  • 3
    @adam: watir-webdriver has screen shots built in: `browser.driver.save_screenshot("file_name.png")` – Željko Filipin May 27 '11 at 09:13
  • 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! – adam reed May 31 '11 at 16:45
  • I'm glad I could help. More gems like these will soon be in my Watir book. http://watir.com/book/ :) – Željko Filipin Jun 01 '11 at 08:10
  • @Ž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? – Martin Jul 24 '13 at 07:56
  • @Martin: I will reply at http://stackoverflow.com/questions/17828277/selenium-or-watir-webdriver-how-to-save-resized-screenshot – Željko Filipin Jul 24 '13 at 08:46
  • @Ž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. – Matt Jul 14 '14 at 00:00
  • What about IE? How can I maximize IE browser? – Ripon Al Wasim Dec 24 '14 at 10:01
  • @Ž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. – HelloWorldNoMore Apr 11 '16 at 19:00
  • Works for PhantomJS too! Thanks! – Michael Apr 06 '17 at 10:51
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)
Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
Alexey Klimchuk
  • 129
  • 1
  • 1
7

I did something like this

browser = Watir::Browser.new :firefox, :profile => profile
browser.send_keys :f11
Boon
  • 401
  • 1
  • 6
  • 17