diff --git a/vaadin-testbench-core/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java b/vaadin-testbench-core/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java index 706500a56..d44f67a99 100644 --- a/vaadin-testbench-core/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java +++ b/vaadin-testbench-core/src/main/java/com/vaadin/testbench/commands/TestBenchCommandExecutor.java @@ -292,33 +292,48 @@ public WebDriver getDriver() { @Override public void resizeViewPortTo(final int desiredWidth, final int desiredHeight) throws UnsupportedOperationException { + final int MAX_RESIZE_ATTEMPTS = 5; try { - actualDriver.manage().window().setPosition(new Point(0, 0)); + getDriver().manage().window().setPosition(new Point(0, 0)); + // Start with the desired dimensions; the loop will adjust for + // browser chrome (title bar, borders, etc.) + getDriver().manage().window() + .setSize(new Dimension(desiredWidth, desiredHeight)); + + for (int attempt = 0; attempt < MAX_RESIZE_ATTEMPTS; attempt++) { + int actualWidth = detectViewportWidth(); + int actualHeight = detectViewportHeight(); + + if (actualWidth == desiredWidth + && actualHeight == desiredHeight) { + return; + } - // first try with mac FF, these will change from plat to plat and - // browser setup to another - int extrah = 106; - int extraw = 0; - actualDriver.manage().window().setSize(new Dimension( - desiredWidth + extraw, desiredHeight + extrah)); + int diffW = desiredWidth - actualWidth; + int diffH = desiredHeight - actualHeight; + Dimension currentSize = getDriver().manage().window().getSize(); + getLogger().fine( + "resizeViewPortTo: attempt " + (attempt + 1) + ", " + + "desired=" + desiredWidth + "x" + desiredHeight + ", " + + "actual=" + actualWidth + "x" + actualHeight + ", " + + "adjusting by " + diffW + "x" + diffH); + getDriver().manage().window() + .setSize(new Dimension(currentSize.getWidth() + diffW, + currentSize.getHeight() + diffH)); + } + // Final check after all attempts int actualWidth = detectViewportWidth(); int actualHeight = detectViewportHeight(); - - int diffW = desiredWidth - actualWidth; - int diffH = desiredHeight - actualHeight; - - if (diffH != 0 || diffW != 0) { - actualDriver.manage().window() - .setSize(new Dimension(desiredWidth + extraw + diffW, - desiredHeight + extrah + diffH)); - } - actualWidth = detectViewportWidth(); - actualHeight = detectViewportHeight(); - if (desiredWidth != actualWidth || desiredHeight != actualHeight) { - throw new Exception( - "Viewport size couldn't be set to desired."); + if (actualWidth != desiredWidth || actualHeight != desiredHeight) { + throw new UnsupportedOperationException( + "Viewport size couldn't be set to the desired '" + + desiredWidth + "," + desiredHeight + "' got '" + + actualWidth + "," + actualHeight + "' after " + + MAX_RESIZE_ATTEMPTS + " attempts."); } + } catch (UnsupportedOperationException e) { + throw e; } catch (Exception e) { throw new UnsupportedOperationException( "Viewport couldn't be adjusted.", e);