I am trying to compare two screen shots on a webpage. It is a practice page on saucelab. When you login as a standart_user and click the login button, it takes you to the products page. Then you see the pictures of products. (Backpacks, t-shirts etc.)
When you login as a problem_user, you see puppy pictures instead of products' picture. I want to screen shot the both pages and compare them. I took the shots by using WebdriverWait.
But the shots are taken before the pictures were loaded fully. What should I use to wait until the pictures on products page are fully loaded?
Below is my code.
public void waitElementVisibility(By visibleImage) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(40));
wait.until(ExpectedConditions.visibilityOfElementLocated(visibleImage));
}
public void captureSuccesfulPageScreenShot() {
waitElementVisibility(visibleImage);
TakesScreenshot screenshot = (TakesScreenshot) driver;
File file = screenshot.getScreenshotAs(OutputType.FILE);
File path = new File("images/screenshot.png");
try {
FileUtils.copyFile(file, path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void captureProblemUserScreenShot() {
waitElementVisibility(visibleImage);
TakesScreenshot screenshot = (TakesScreenshot) driver;
File file = screenshot.getScreenshotAs(OutputType.FILE);
File path = new File("images/problemUserScrSh.png");
try {
FileUtils.copyFile(file, path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
I've managed to create a wait method, but it fails on fifth image. Performing the render check take some time and I'm unable to simulate slow rendering.
Code:
Output: