Wednesday, 3 July 2013

Maximize browser window using webdriver



There are times when we want to to maximize the browser window during the execution of our script .For this purpose webdriver providers a built-in method and here is the syntax :

  1. WebDriver driver;  
  2. driver.manage().window().maximize();  
Here is the sample code using TestNG framework. :
  1. import java.awt.Toolkit;  
  2. import java.io.IOException;  
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.firefox.FirefoxDriver;  
  5. import org.testng.Reporter;  
  6. import org.testng.annotations.BeforeTest;  
  7. import org.testng.annotations.Test;  
  8.   
  9. public class MaximizeWindow {  
  10.   
  11. WebDriver driver;  
  12.   
  13. @BeforeTest  
  14. public void setUpDriver() {  
  15. driver = new FirefoxDriver();  
  16.       }  
  17.     
  18.   
  19. @Test  
  20. public void maximize() {  
  21.  //declare varibales for windows  
  22. org.openqa.selenium.Dimension defaultDim;  
  23. org.openqa.selenium.Dimension maximizeDim;  
  24. //Load google website on browser  
  25. driver.get("http://google.com");  
  26. //Display the current screen dimensions  
  27. defaultDim=driver.manage().window().getSize();  
  28. System.out.println("screenHeight before maximizing"+defaultDim.getHeight());  
  29. System.out.println("screenWidth before maximizing"+defaultDim.getWidth());  
  30. //maximize the window using webdriver method  
  31. driver.manage().window().maximize();  
  32. //Display the maximized window dimensions  
  33. maximizeDim=driver.manage().window().getSize();  
  34. System.out.println("screenHeight after maximizing:"+maximizeDim.getHeight());  
  35. System.out.println("screenWidth after maximizing:"+maximizeDim.getWidth());  
  36.   
  37.       }  
  38.   
  39.   
  40.   }  

No comments:

Post a Comment

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...