How to compare two images/screenshot in Selenium?

Поділитися
Вставка
  • Опубліковано 22 жов 2024
  • In this video I have shown how to compare two screenshots(expected vs actual images) in selenium using Ashot() api.
    • How to take screenshot...
    • Selenium - How to take...
    • How to take CSS Select...
    • How to write xpath in ...
    • Selenium - How to take...
    • How to take multiple s...
    • How to take locators(x...
    I hope you like this video. For any questions, suggestions or appreciation please contact us at: programmerworl... or email at: programmerworld1990@gmail.com
    programmerworl...
    Code:
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import ru.yandex.qatools.ashot.AShot;
    import ru.yandex.qatools.ashot.Screenshot;
    import ru.yandex.qatools.ashot.comparison.ImageDiff;
    import ru.yandex.qatools.ashot.comparison.ImageDiffer;
    import ru.yandex.qatools.ashot.coordinates.WebDriverCoordsProvider;
    import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
    public class compareScreenshot {
    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", "Path of chromedriver exe\\chromedriver.exe");
    ChromeOptions op = new ChromeOptions();
    op.setBinary("Path of Chrome exe\\chrome.exe");
    op.addArguments("--remote-allow-origins=*");
    WebDriver driver = new ChromeDriver(op);
    driver.get("programmerworl...");
    BufferedImage expectedImage = ImageIO.read(new File("Path of expected image\\ExpectedImage.jpg"));
    WebElement actualImageLocation = driver.findElement(By.xpath("//img[@class='custom-logo']"));
    Screenshot scrShot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(500)).coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver, actualImageLocation);
    ImageIO.write(scrShot.getImage(), "jpg", new File("Path of actual image to be saved\\ActualImage.jpg"));
    BufferedImage actualImage = ImageIO.read(new File("Path of actual image\\ActualImage.jpg"));
    ImageDiffer imgdiff =new ImageDiffer();
    ImageDiff diff = imgdiff.makeDiff(expectedImage, actualImage);
    if(diff.hasDiff()) {
    System.out.print("******Images are not same");
    }
    else
    {
    System.out.print("====Images are same");
    }
    }
    }
    --

КОМЕНТАРІ •