Source:
private void resizeBrowserWindow(WebDriver driver, DesiredCapabilities capabilities) { try { Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .pollingEvery(Duration.ofMillis(Configuration.getInt(Parameter.RETRY_INTERVAL))) .withTimeout(Duration.ofSeconds(Configuration.getInt(Parameter.EXPLICIT_TIMEOUT))) .ignoring(WebDriverException.class) .ignoring(NoSuchSessionException.class) .ignoring(TimeoutException.class); if (capabilities.getCapability("resolution") != null) { String resolution = (String) capabilities.getCapability("resolution"); int width = Integer.valueOf(resolution.split("x")[0]); int height = Integer.valueOf(resolution.split("x")[1]); wait.until(new Function<WebDriver, Boolean>(){ public Boolean apply(WebDriver driver ) { driver.manage().window().setPosition(new Point(0, 0)); driver.manage().window().setSize(new Dimension(width, height)); if (driver.manage().window().getSize().getWidth() == width && driver.manage().window().getSize().getHeight() == height) { LOGGER.debug(String.format("Browser window size set to %dx%d", driver.manage().window().getSize().getWidth(), driver.manage().window().getSize().getHeight())); } else { LOGGER.warn(String.format("Browser window size set to %dx%d", driver.manage().window().getSize().getWidth(), driver.manage().window().getSize().getHeight())); } return true; } }); } else { wait.until(new Function<WebDriver, Boolean>(){ public Boolean apply(WebDriver driver ) { driver.manage().window().maximize(); LOGGER.debug("Browser window size was maximized!"); return true; } }); } } catch (Exception e) { LOGGER.error("Unable to resize browser window", e); } }

Target:
private void resizeBrowserWindow(WebDriver driver, DesiredCapabilities capabilities) { try { Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .pollingEvery(Duration.ofMillis(Configuration.getInt(Parameter.RETRY_INTERVAL))) .withTimeout(Duration.ofSeconds(Configuration.getInt(Parameter.EXPLICIT_TIMEOUT))) .ignoring(WebDriverException.class) .ignoring(NoSuchSessionException.class) .ignoring(TimeoutException.class); if (capabilities.getCapability("resolution") != null) { String resolution = (String) capabilities.getCapability("resolution"); int expectedWidth = Integer.valueOf(resolution.split("x")[0]); int expectedHeight = Integer.valueOf(resolution.split("x")[1]); wait.until(new Function<WebDriver, Boolean>(){ public Boolean apply(WebDriver driver ) { driver.manage().window().setPosition(new Point(0, 0)); driver.manage().window().setSize(new Dimension(expectedWidth, expectedHeight)); Dimension actualSize = driver.manage().window().getSize(); if (actualSize.getWidth() == expectedWidth && actualSize.getHeight() == expectedHeight) { LOGGER.debug(String.format("Browser window size set to %dx%d", actualSize.getWidth(), actualSize.getHeight())); } else { LOGGER.warn(String.format("Expected browser window %dx%d, but actual %dx%d", expectedWidth, expectedHeight, actualSize.getWidth(), actualSize.getHeight())); } return true; } }); } else { wait.until(new Function<WebDriver, Boolean>(){ public Boolean apply(WebDriver driver ) { driver.manage().window().maximize(); LOGGER.debug("Browser window size was maximized!"); return true; } }); } } catch (Exception e) { LOGGER.error("Unable to resize browser window", e); } }