0

In order to perform visual tests under Selenium, I performed the image comparison tests ( 2 images only). I use the file size to see if there is a difference or not. However, nothing tells me where I have this difference and I would like to be able to display the difference(s) shown on the image.

I was thinking of a comparison by color instead of size. It seems complex to me especially since I would like to have an image output showing the difference (with a crop that specifies the area) or by extracting the pixels impacted by this difference. Do you think it's possible to do it under selenium in C#? For the moment, i tried by size.

public static void TestComapre()
{
    string imgPath1 = <//PATHNAME >
    string imgPath2 = <//PATHNAME >

    const int size = 1000;
    var len = new FileInfo(imgPath1).Length;
    if (len != new FileInfo(imgPath2).Length)

    var s1 = File.OpenRead(imgPath1);
    var s2 = File.OpenRead(imgPath2);

    var buf1 = new byte[size];
    var buf2 = new byte[size];

    for (int i = 0; i < len / size; i++)
    {
        s1.Read(buf1, 0, size);
        s2.Read(buf2, 0, size);
        if (CompareBuffers(buf1, buf2) == false)

            Assert.Fail();
    }
    Assert.True(true);
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
GioOps
  • 23
  • 5
  • Possible duplicate of [Algorithm to compare two images in C#](https://stackoverflow.com/questions/35151067/algorithm-to-compare-two-images-in-c-sharp) – JeffC Apr 26 '19 at 19:25
  • BTW, this has nothing to do with Selenium. This is just how to compare two images, etc. A simple google search will get you a LOT of potential solutions. I've dup'd this question to one of them. – JeffC Apr 26 '19 at 19:26
  • [Here](https://stackoverflow.com/questions/26224095/how-to-find-the-difference-between-two-images/26225153?r=SearchResults&s=1|50.0788#26225153) is another solution.. – TaW Apr 26 '19 at 19:44
  • Thank you for your help. I appriciate that. – GioOps Apr 29 '19 at 09:24

1 Answers1

0

I have a custom made image comparer in C#.

It compares 2 images, ignores magenta pixels (you can use magenta as a MASK for areas to ignore when comparing) and marks different pixels as blue in a new image

////////////////////// VARIABLES //////////////////////////

    private string pathReferenceImg;
    private string pathTestImg;
    private FileInfo fReferenceFile;
    private FileInfo fTestFile;
    private Bitmap referenceImage;
    private Bitmap testImage;
    private int areaToCompareWidth;
    private int areaToCompareHeight;
    public int xMinAreaToCompare = 0;
    public int yMinAreaToCompare = 0;
    public int pixelDifferenceQuantity = 0;
    public List<Point> differentPixelsList = new List<Point>();
    private int[] rgbArrayTestImgWithReferenceImgPink;
    private int tolerance = 15;
    public bool result = false;

////////////////////// CODE //////////////////////////

    public void compareFiles(string pathReferenceImg, string pathTestImg)
    {
        fReferenceFile = new FileInfo(pathReferenceImg);
        fTestFile = new FileInfo(pathTestImg);
        referenceImage = new Bitmap(pathReferenceImg);
        testImage = new Bitmap(pathTestImg);
        areaToCompareWidth = referenceImage.Width;
        areaToCompareHeight = referenceImage.Height;
            while (xMinAreaToCompare < areaToCompareWidth)
            {
                Color colorRef = referenceImage.GetPixel(xMinAreaToCompare, yMinAreaToCompare);
                Color colorTest = testImage.GetPixel(xMinAreaToCompare, yMinAreaToCompare);
                //Magenta = 255R,255B,0G
                if (colorRef.ToArgb() != Color.Magenta.ToArgb())
                {
                    if (colorRef != colorTest)
                    {
                        pixelDifferenceQuantity++;
                        differentPixelsList.Add(new Point(xMinAreaToCompare, yMinAreaToCompare));
                    }
                }

                yMinAreaToCompare ++;
                if (yMinAreaToCompare == areaToCompareHeight)
                {
                    xMinAreaToCompare ++;
                    yMinAreaToCompare = 1;
                }
            }
            if (pixelDifferenceQuantity >= tolerance)
            {
                Bitmap resultImage = new Bitmap(testImage);
                foreach (Point pixel in differentPixelsList)
                {
                    resultImage.SetPixel(pixel.X, pixel.Y, Color.Blue);
                }
                resultImage.Save(pathTestImg.Replace("TestFolder", "ResultFolder"));
            }
            else
            {
                result = true;
            }
    }

hope it helps.

Valga
  • 459
  • 2
  • 7
  • 1
    Valga thank you so much. It works very well. I custom my code and it's perfect. Thanks a lot. – GioOps Apr 29 '19 at 09:25