Maximize Browser Window Using Selenium WebDriver And By Using Abstract Window Toolkit
Maximizing window in Selenium WebDriver is really quite easy but sometimes you find it really hard to maximize the window while using Selenium Grid.
Many time you get stuck to such a scenario where scripts start failing on remote machine because your previously written code does not work. The reason could be anything, Might be
1- Your browser on the remote machine could possibly be updated and still you are using old Selenium Standalone jar
2- Might be your code is unable to maximize your browser instance. Due to this few of the WebElement seems eclipsed due to the size of the browser window.
So let’s see how you can maximize your browser window using Selenium WebDriver along with Abstract Window Toolkit. here is the list of few methods that can help you to get the requisite result.
1- Using the traditional way to Selenium WebDriver maximize() method
So why not we are going to write one script then:
Step 1: Create one class name MaximizeBrowser but before that would suggest adding Selenium Standalone jar in classpath. To read how to do this read Downloading and Configuring Selenium Webdriver in Eclipse with TestNG plugin installation.
Step 2: Copy and past complete code and replace everything in MaximizeBrowser class except package
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.junit.Test; import org.openqa.selenium.firefox.FirefoxDriver; public class MaximizeBrowser { @Test //@Test annotation of junit is used public void test() { //Currently using firefox but we can change it to IE and Chrome WebDriver driver = new FirefoxDriver(); //Maximizing brwoser window using maximize() method driver.manage().window().maximize(); driver.get("http://abodeqa.com"); } }
If you want to experiment with Chrome Browser then follow Launching Chrome Browser using WebDriver.
2- This option was totally for Chrome browser and in this script, we were using Chrome Option to maximize chrome browser (Still looking to perform this action with DesiredCapability for IE and Firefox)
So again let’s see one more example but this time we are going to take the example of Chrome Browser.
1- Create one class MaximizeChromeBrowser
2- Copy and paste the following code and replace everything except package in MaximizeChromeBrowser class
import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; public class MaximizeChromeBrowser { @Test public void test() { ChromeOptions co = new ChromeOptions(); //here "--start-maximized" argument is responsible to maximize chrome browser co.addArguments("--start-maximized"); //Chromedriver.exe is place in the same directory System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); WebDriver driver = new ChromeDriver(co); driver.get("http://abodeqa.com"); driver.quit(); } }
So by using the above code we can maximize chrome browser but be assured that the above code is not going to work on IE and Firefox browser. So why not we move to one more method like this because this is going to work across the browser.
//Maximizing brwoser window using maximize() method driver.manage().window().maximize();
3- By using Toolkit utility, we can maximize the browser window as per the resolution of your computer screen resolution. So let’s see one more example.
Step 1: Create one class MaximizeBrowser
Step 2: Copy and paste the following code and replace everything except package in your class and run it
import java.awt.Toolkit; import org.junit.Test; import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class MaximizeBrowser { @Test public void test() { //To launch Firefox browser pass parameter "Firefox" and to Launch Chrome browser pass parameter "Chrome". WebDriver driver = openBrowser("IE"); Toolkit toolkit = Toolkit.getDefaultToolkit(); int Width = (int) toolkit.getScreenSize().getWidth(); int Height = (int)toolkit.getScreenSize().getHeight(); //For Dimension class, Import following library "org.openqa.selenium.Dimension" driver.manage().window().setSize(new Dimension(Width,Height)); driver.get("http://abodeqa.com"); driver.quit(); } /* * Utility method to launch browser * */ public WebDriver openBrowser(String nameOfBrowser) { WebDriver driver = null; if(nameOfBrowser.equalsIgnoreCase("Firefox")) { driver = new FirefoxDriver(); } else if (nameOfBrowser.equalsIgnoreCase("Chrome")) { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); driver = new ChromeDriver(); } else if (nameOfBrowser.equalsIgnoreCase("IE")) { DesiredCapabilities dc = DesiredCapabilities.internetExplorer(); dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); System.setProperty("webdriver.ie.driver", "IEDriverServer.exe"); driver = new InternetExplorerDriver(dc); } return driver; } }
4- All the above code can help you to maximize browser, But what if you need to run your test script in a custom size of the browser then might be this code would help you a lot.
driver.manage().window().setSize(new Dimension(Width,Height));
So if you are putting Width =300 and Height=600 then your browser size will be turned in the same size.
This is a fantastic blog from which people can learn a lot. It is very informative and is explained in…
Thanks for sharing such knowledgeable Content on Automation testing, to know how to enhance the performance of ERP's like Oracle…
Thanks.. this list is much needed.
This one is also good to use. Thanks for sharing this.. Might be we can also add it in list…
How about youtube-dl?