简体   繁体   中英

Selenium Grid: how to maximize browser window using RemoteWebDriver and ChromeDriver

I would like to know how can I maximize the browser window using Selenium Grid and RemoteWebDriver with the most popular browsers.

This question has not been solved yet in this community, there is another question that could look like this: How do I maximize the browser window in Selenium WebDriver (Selenium 2) using C#? But in that question is not clear how to maximize the browser window in RemoteWebDriver.

On Firefox and IE I guess it is in the same way driver.manage().window().maximize();

In Chrome we have to do:

ChromeOptions options = new ChromeOptions(); options.AddArgument("--start-maximized"); driver = new ChromeDriver(options);

The question is how can I apply that to RemoteWebDriver?

If i understand your question correctly you want to know how to pass driver options to remote driver.In that case where ever you are creating driver object you need to create desire capabilities and use one of the constructor of the remote driver with capability parameter. For example

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

ChromeOptions options = new ChromeOptions(); 
options.addArguments("--start-maximized"); 

capabilities.setCapability(ChromeOptions.CAPABILITY, options);

driver = new RemoteWebDriver(URL, capabilities);
//driver = new ChromeDriver(capabilities);

You also can use QAF which abstraction driver configuration and management outside the code. Where you can set driver capabilities by using properties. As alternate You also can use Driver listener for such purpose. For example:

Using Properties:

Following two properties will do the needful for chrome driver:

drive.name=chromeDriver
chrome.additional.capabilities={"chromeOptions":{"args":["--start-maximized"]}}

For remote driver:

remote.server=<remote server or grid url>
drive.name=chromeRemoteDriver
chrome.additional.capabilities={"chromeOptions":{"args":["--start-maximized"]}}

Using Listener:

void beforeInitialize(Capabilities capabilities){
    if(capabilities.getBrowserName().equalIgnorCase("chrome"){
        ChromeOptions options = new ChromeOptions(); 
        options.addArguments("--start-maximized"); 
        ((DesiredCapabilities)capabilities).setCapability(ChromeOptions.CAPABILITY, options);
    }
}

void onInitialize(QAFExtendedWebDriver driver){
   //for browser other than chrome...
   driver.manage().window().maximize();
}

In Java you could do it like this:

ChromeOptions options = new ChromeOptions(); 
options.addArguments("--start-maximized"); 
RemoteWebDriver driver = new ChromeDriver(options); 

It was already hinted at in the comments but to point it out: You can assign a ChromeDriver instance to a RemoteWebDriver type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM