If you want it to be truly maximized (i.e. not merely resized to the
screen height/width), here's a little bit of black magic that I came
up with:
[Test]
public void Maximize(int browser)
{
IWebDriver driver = GetDriver(browser);
string script;
string name;
/* Some black magic to maximize Chrome. --Kris */
if (browser == Globals.CHROME)
{
name = "ed47cd2a4fcb5534a49f6eeb3bfcc564 - Google
Search";
driver.Navigate().GoToUrl("
http://www.google.com/
search?q=ed47cd2a4fcb5534a49f6eeb3bfcc564");
}
else
{
/* Just in case the black magic below doesn't
work.... --Kris */
script = "window.moveTo( 0, 1 ); ";
script += "window.resizeTo( screen.width,
screen.height );";
((IJavaScriptExecutor)driver).ExecuteScript(script);
name = "ed47cd2a4fcb5534a49f6eeb3bfcc564";
script = "document.title='" + name + "';";
((IJavaScriptExecutor)driver).ExecuteScript(script);
}
/* This is some real voodoo magic here! Muaa ha ha ha!!
--Kris */
IntPtr hWnd = FindWindow(null, name + " - " +
Globals.BrowserPIDName(browser));
if (!hWnd.Equals(IntPtr.Zero))
{
ShowWindowAsync(hWnd, 3); // 3 = Maximize! --Kris
}
}
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int
nCmdShow);
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string sClassName,
string sAppName);
You can probably clean out the JS stuff. Also, I wasn't aware of the
--start-maximized Chrome switch when I wrote this, so I would replace
the Chrome block to what Jason suggested. =)
--Kris
> On Thu, Sep 29, 2011 at 10:45 AM, Jason Leyba <
jmle...@gmail.com> wrote:
> > On Thu, Sep 29, 2011 at 7:55 AM, Moises Siles <
moises.si...@gmail.com>wrote:
>
> >> I'm doing this
>
> >> ((IJavaScriptExecutor)webDriver).ExecuteScript("if
> >> (window.screen){window.moveTo(0,
> >> 0);window.resizeTo(window.screen.availWidth,window.screen.availHeight);};");
>
> > ^ Yup, this is the way to do it. Just a heads up though, the JavaScript
> > functions window.moveTo and winodw.resizeTo have no effect in Chrome. If
> > you want to maximize a chrome browser, you'll have to start the browser
> > maximized using DesiredCapabilities. In Java:
>
> > DesiredCapabilities capabilities = DesiredCapabilities.chrome();
> > capabilities.setCapability("chrome.switches",
> > Arrays.asList("--start-maximized"));
> > WebDriver driver = new ChromeDriver(capabilities);
>
> > -- Jason
>