Reputation: 1790
I am having an issue using chrome and selenium 2 webdriver. The issue is that when I start my browser session using the chromedriver it always starts in a window that is less than half the size of the available screen width. Because I am doing tests on a page that changes when the screen size changes, my tests fail because I'm trying to drag an element from the top of the page and drop it to an element that is at the bottom of the page. I get a scrolling error. But if the window is maximized, then I don't get this error. But the problem is, every time chrome starts a new session via chrome driver it always starts in a small window. I have explored many different options to get the browser to start maximized:
When I try: driver.manage().window().setSize() I get this exception:
INFO: Executing: [93debf43cf70ad3557442a7e1aee4620, setWindowSize {"windowHandle":"current","width":2560,"height":1440}]
org.openqa.selenium.UnsupportedCommandException: [GET, HEAD, DELETE]
Command duration or timeout: 16 milliseconds
Build info: version: '2.15.0', revision: '15105', time: '2011-12-08 09:56:25'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.7.2', java.version: '1.6.0_29'
Driver info: driver.version: RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:147)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:113)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:424)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteWindow.setSize(RemoteWebDriver.java:578)
at com.domo.automation.framework.utility.WebDriverUtil.startWebDriver(WebDriverUtil.java:36)
at com.domo.automation.tests.DomoWebDriverTestCase.setUp(DomoWebDriverTestCase.java:45)
at junit.framework.TestCase.runBare(TestCase.java:132)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
What other options could I explore in order to accomplish this? The issue is that the window is too small? What can I do to automaticallly maximize the window every time chrome starts via webdriver on a mac?
Upvotes: 10
Views: 11184
Reputation: 525
I use webdrivermanager and selenide. For maximize the window Configuration.startMaximized = true
import io.github.bonigarcia.wdm.ChromeDriverManager;
import com.codeborne.selenide.Configuration;
import io.github.bonigarcia.wdm.DriverManagerType;
import static com.codeborne.selenide.Selenide.open;
public class Main {
public static void main(String[] args) {
ChromeDriverManager.getInstance(DriverManagerType.CHROME).version("76.0.3809.126").setup();
Configuration.baseUrl = "https://www.google.com/";
Configuration.startMaximized = true;
open("/");
Upvotes: 0
Reputation: 418
Please check following solution, It will work for you.
System.setProperty("webdriver.chrome.driver", driverPath);
ChromeOptions options= new ChromeOptions();
options.addArguments("start-fullscreen");
Upvotes: 0
Reputation: 848
I have same problem on MAC.
I have tried below options but not works:
driver.manage().window().maximize();
Here
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
driver = new ChromeDriver(chromeOptions);
Finally I have found below solution which works on all OS :
ChromeOptions options = new ChromeOptions();
options.addArguments("start-fullscreen");
Let me know If any query.
Upvotes: 0
Reputation: 121
It's very old post but still I faced this issue today and resolved using below code.
java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
org.openqa.selenium.Point position = new Point(0, 0);
driver.manage().window().setPosition(position);
org.openqa.selenium.Dimension maximizedScreenSize =
new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
driver.manage().window().setSize(maximizedScreenSize);
Upvotes: 0
Reputation: 21
The best solution I found until now is the following:
after starting the browser run the following code
Point targetPosition = new Point(0, 0);
driver.manage().window().setPosition(targetPosition);
Dimension targetSize = new Dimension(1920, 1080); //your screen resolution here
driver.manage().window().setSize(targetSize);
I hope it helps. I managed to maximize Chrome under MAC using this solution.
Upvotes: 2
Reputation: 1826
I solved this issue by executing an apple script. This, of course ties you to execution on a Mac, but that was okay for us. Windows doesn't have the same maximization problem for Chrome, so if you care you could do an OS check.
I found the apple script I used here. I copied it below in case the link ever breaks:
tell application (path to frontmost application as string)
to copy (run script "tell application \"Finder\" to desktop's window's bounds")
to bounds of window 1
I used this script (inspired by this question) to activate the browser window first:
tell application "Google Chrome"
activate
end tell
I executed in Java using this command:
Runtime.getRuntime().exec(scriptPath)
Hope this helps someone.
Upvotes: 1
Reputation: 1314
What you described here was also the case for me. Following to this article, you can set preference for chromedriver when starting it.
Ruby:
profile = Selenium::WebDriver::Chrome::Profile.new
profile['browser.window_placement.top'] = 0
profile['browser.window_placement.left'] = 0
profile['browser.window_placement.right'] = 1024
profile['browser.window_placement.bottom'] = 768
Selenium::WebDriver.for :chrome, profile: profile
This worked on both Windows and Mac. On Java though, I couldn't verify the API for setting the preference for chromedriver. Here is a question asked about it but seems no answer has been found yet.
Upvotes: 2
Reputation: 116
driver.manage().window().setSize() will not currently work with ChromeDriver (as of v19). There is an active chromium issue relating to this feature request.
The current workaround generally recommended is to use DesiredCapabilities to set the window size, but you're right that it doesn't seem to work on the Mac at all.
The only solution I've found is to open a new window using JavaScript and resize the new window in the same fashion. It's described deep in this thread on the topic. The relevant workaround is shown in the code below:
JavascriptExecutor js = ((JavascriptExecutor)driver);
js.executeScript("window.open('','testwindow','width=400,height=200')");
driver.close();
driver.switchTo().window("testwindow");
js.executeScript("window.moveTo(0,0);");
js.executeScript("window.resizeTo(1280,800);");
I'm afraid it's quite clunky, but it worked for me locally using chrome on the Mac (Lion).
Upvotes: 4