在Selenium中,如何比较图像?

时间:2012-02-10 11:18:15

标签: image selenium comparison

我被要求使用Selenium Automation来自动化我们公司的网站。 但我是Selenium工具的新手,但我已经学习了Selenium IDE和RC的基础知识。但我对如何比较实际和原始图像非常困惑,就像我们在其他自动化工具中通常所做的那样。我们如何得出网站中存在错误的结果?它显然通过图像比较,但我不知道硒是非常受欢迎的工具之一,但它没有图像比较选项。另一方面,我怀疑我继续自动化过程的方式是否正确!有人可以帮帮我..

先谢谢!! Sanjay S

5 个答案:

答案 0 :(得分:2)

我有类似的任务。我需要在WebPage上比较超过3000个图像。 首先,我滚动页面加载所有图像:

public void compareImage() throws InterruptedException {
    driver.get(baseUrl);
    driver.manage().window().maximize();

    JavascriptExecutor executor = (JavascriptExecutor) driver;

    Long previousHeight;
    Long currentHeight;

    do {
       previousHeight = (Long) executor.executeScript("return                document.documentElement.scrollHeight");

        executor.executeScript("window.scrollBy(0, document.documentElement.scrollHeight)");
        Thread.sleep(500);
        currentHeight = (Long) executor.executeScript("return        document.documentElement.scrollHeight");
    } while (Long.compare(previousHeight, currentHeight) != 0);

在我将所有图像的大小与第一张图像进行比较后(或者你可以只写大小):

List<WebElement> images = driver.findElements(By.cssSelector("img[class='playable']"));
    List<String> errors = new LinkedList<>();

    int imgWidth, imgHeight, elWidth, elHeight;
    int imgNum = 0;

    imgWidth = images.get(0).getSize().getWidth();
    imgHeight = images.get(0).getSize().getHeight();


    for (WebElement el : images) {
        imgNum++;

        elWidth = el.getSize().getWidth();
        elHeight = el.getSize().getHeight();

        if (imgWidth != elWidth || imgHeight != elHeight) {
            errors.add(String.format("Picture # %d has incorrect size (%d : %d) px"
                    , imgNum, elWidth, elHeight));
        }
    }

    for (String str : errors)
        System.out.println(str);

    if (errors.size() == 0)
        System.out.println("All images have the same size");
}

答案 1 :(得分:0)

由于您提到了有关Selenium RC的知识,因此您可以使用所选编程语言的库轻松扩展Selenium的功能。例如,在Java中,您可以使用PixelGrabber类来比较两个图像并断言它们的匹配。

答案 2 :(得分:0)

imagemagick和imagediff也是用于图像匹配的两个好工具。您需要Selenium RC和编程语言知识才能使用它。

答案 3 :(得分:0)

C#上的图像比较。为了获得准确的结果,我建议在拍摄屏幕截图之前禁用抗锯齿浏览器功能,否则每次像素都会略有不同。例如HTML canvas元素options.AddArgument(“disable-canvas-aa”);

private static bool ImageCompare(Bitmap bmp1, Bitmap bmp2, Double TolerasnceInPercent)
    {
        bool equals = true;
        bool flag = true;  //Inner loop isn't broken

        //Test to see if we have the same size of image
        if (bmp1.Size == bmp2.Size)
        {
            for (int x = 0; x < bmp1.Width; ++x)
            {
                for (int y = 0; y < bmp1.Height; ++y)
                {
                    Color Bitmap1 = bmp1.GetPixel(x, y);
                    Color Bitmap2 = bmp2.GetPixel(x, y);

                    if (Bitmap1.A != Bitmap2.A)
                    {
                        if (!CalculateTolerance(Bitmap1.A, Bitmap2.A, TolerasnceInPercent))
                        {
                            flag = false;
                            equals = false;
                            break;
                        }
                    }
                    if (Bitmap1.R != Bitmap2.R)
                    {
                        if (!CalculateTolerance(Bitmap1.R, Bitmap2.R, TolerasnceInPercent))
                        {
                            flag = false;
                            equals = false;
                            break;
                        }
                    }
                    if (Bitmap1.G != Bitmap2.G)
                    {
                        if (!CalculateTolerance(Bitmap1.G, Bitmap2.G, TolerasnceInPercent))
                        {
                            flag = false;
                            equals = false;
                            break;
                        }
                    }
                    if (Bitmap1.B != Bitmap2.B)
                    {
                        if (!CalculateTolerance(Bitmap1.B, Bitmap2.B, TolerasnceInPercent))
                        {
                            flag = false;
                            equals = false;
                            break;
                        }
                    }

                }
                if (!flag)
                {
                    break;
                }
            }
        }
        else
        {
            equals = false;
        }
        return equals;
    }

答案 4 :(得分:0)

此C#函数计算容差

private static bool CalculateTolerance(Byte FirstImagePixel, Byte SecondImagePixel, Double TolerasnceInPercent)
    {
        double OneHundredPercent;
        double DifferencesInPix;
        double DifferencesPercentage;


        if (FirstImagePixel > SecondImagePixel)
        {
            OneHundredPercent = FirstImagePixel;
        }
        else
        {
            OneHundredPercent = SecondImagePixel;
        }

        if (FirstImagePixel > SecondImagePixel)
        {
            DifferencesInPix = FirstImagePixel - SecondImagePixel;
        }
        else
        {
            DifferencesInPix = SecondImagePixel - FirstImagePixel;
        }

        DifferencesPercentage = (DifferencesInPix * 100) / OneHundredPercent;

        DifferencesPercentage = Math.Round(DifferencesPercentage, 2);

        if (DifferencesPercentage > TolerasnceInPercent)
        {
            return false;                
        }

        return true;
    }