Thursday, 12 April 2012

Maximize browser window in Selenium Web Driver

From Selenium 2.21, we can now maximize the browser window, similar to Selenium RC.

 Direct way:

public static void main(String[] args) {
  WebDriver d = new FirefoxDriver();
  d.get("http://www.google.com");
  d.manage().window().maximize();
 
  System.out.println(d.getTitle());
  d.quit();
 }

Indirect way:

we can also maximize the browser window usingt the toolkit utility which query the native operating system directly and is platform independent. In addition to this, the code below will maximize the browser window according to your system's current resolution.

driver.manage().window().setPosition(new Point(0,0));
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
driver.manage().window().setSize(dim);
Use the above script in your test setup method where you initiate the browser to get a fully maximized brwoser window that fits your monitor's screen size....
 

4 comments:

  1. Thank you so much for the above code, it did worked for me though i searched lot of forums for the same but didn't get this simple information.

    ReplyDelete
  2. Is there isTextPresent() equent method in Webdriver ???????

    ReplyDelete
    Replies
    1. hi boss,
      did u get the answer to your question?
      The equivalent for it will be something like

      assertTrue(driver.getPageSource().contains("sometext"));

      Delete