Selenium / JAVA图像比较

时间:2017-08-17 07:28:32

标签: java selenium image-processing

目前,我的代码为某些分辨率拍摄了一些屏幕截图。我想用最后拍摄的图像对所有图像进行comare。

我拍摄截图的代码。现在我想做一些事情,与最后一个分辨率文件夹和当前图像文件夹列表进行比较,并生成图像,程序将标记哪些是不同的。任何帮助都会被评估。

@Test   
public void testResolution() throws InterruptedException 
{
    this.url = "https://www.google.com";
    driver.get(this.url);
    driver.navigate().to(this.url);
    String[] resulutions = { "1366x768" , "360x640" , "768x1024" , "375x667" , "1920x1080" , "320x568" , "1600x900" , "1280x800" , 
            "1440x900" , "1536x864" , "414x736" , "1280x1024" , "1280x720" , "1440x900" , "1680x1050" , "1024x768" , "1920x1080" ,
            "1280x800" , "1024x768" , "1280x800" , "412x732" , "320x534" , "320x570" , "1093x614", };

            // add resolution

    for (String resulution : resulutions) 
    {       
        String[] parts = resulution.split("x");     
        // Screen resolution
        Dimension screenRes = new Dimension(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));                    
        // Set browser resolution
        driver.manage().window().setSize(screenRes);
        driver.navigate().refresh();
        Thread.sleep(3000);
        this.takeScreenShot(resulution);
    }
}

/**
 *
 * @param fileName
 */

private void takeScreenShot(String fileName) 
{       
    File screenShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    try {
        //FileUtils.copyFile(screenShot, new File("Old"+fileName+"/"+fileName+ ".png"));
        //FileUtils.copyFile(screenShot, new File("E:\\Faysal_test/"+fileName+"/"+fileName+ ".png"));
        FileUtils.copyFile(screenShot, new File("D:\\Automation/VRS/vrs-resolution/Screenshot/new/"+fileName+"/"+fileName+ ".png"));

    } catch (IOException ioe) {
        throw new RuntimeException(ioe.getMessage(), ioe);
    }
}

@Test

public void testResolutionold() throws InterruptedException 
{

    this.url = "https://www.rentalhomes.com/property/extended-stay-america-billings-west-end/BC-517037";
    driver.get(this.url);
    driver.navigate().to(this.url);
    String[] resulutions = { "1366x768" , "360x640" , "768x1024" , "375x667" , "1920x1080" , "320x568" , "1600x900" , "1280x800" , 
            "1440x900" , "1536x864" , "414x736" , "1280x1024" , "1280x720" , "1440x900" , "1680x1050" , "1024x768" , "1920x1080" ,
            "1280x800" , "1024x768" , "1280x800" , "412x732" , "320x534" , "320x570" , "1093x614",};
            // add resolution       
    for (String resulution : resulutions) 
    {

        String[] parts = resulution.split("x");

        // Screen resolution
        Dimension screenRes = new Dimension(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));


        // Set browser resolution
        driver.manage().window().setSize(screenRes);
        driver.navigate().refresh();
        Thread.sleep(3000);
        this.takeScreenShotold(resulution);
    }

}

/**
 *
 * @param fileName
 */

private void takeScreenShotold(String fileName) 
{

    File screenShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    try {
        //FileUtils.copyFile(screenShot, new File("Old"+fileName+"/"+fileName+ ".png"));
        //FileUtils.copyFile(screenShot, new File("E:\\Faysal_test/"+fileName+"/"+fileName+ ".png"));
        FileUtils.copyFile(screenShot, new File("D:\\Automation/VRS/vrs-resolution/Screenshot/old/"+fileName+"/"+fileName+ ".png"));

    } catch (IOException ioe) {
        throw new RuntimeException(ioe.getMessage(), ioe);
    }
}

2 个答案:

答案 0 :(得分:0)

查看OpenCV库。你可以找到你需要的东西。请查看here,了解如何比较图像。

答案 1 :(得分:-1)

检查此链接:

  

https://www.frontendtest.org/blog/screenshot-comparison/

这些家伙创建了自己的用于比较图像的库,它具有您正在谈论的功能。

要使用全部内容,几乎不需要重构,但是您可以检查它们如何基于原始图像生成新图像,并用红色矩形标记更改。

  

以下代码在图像坐标上绘制了一个红色矩形:X 100px,Y 100px(从左上角开始);矩形大小:宽度50像素,高度50像素;

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;

    public boolean drawRectangle(BufferedImage img, String pathOut) throws IOException {                            
        imgOut = imageToBufferedImage(ImageIO.read(new File(img)));
        Graphics2D outImgGraphics = imgOut.createGraphics();
        outImgGraphics.setColor(Color.RED);
        outImgGraphics.drawRect(100, 100 , 50, 50);
        saveImage(imgOut,pathOut);
    }

    private BufferedImage imageToBufferedImage(Image img) {
            BufferedImage bi = new BufferedImage(img.getWidth(null),
                img.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bi.createGraphics();
            g2.drawImage(img, null, null);
            return bi;
    }