Regression testing for styling and layout of web applications

前端 未结 5 417
-上瘾入骨i
-上瘾入骨i 2021-02-04 10:55

I realise this is a non-trivial task, but is there a way to regression test the styling and rendered layout of a web application?

I\'ve found it straightforward to unit t

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-04 11:23

    Actually, one of the hidden gems of Selenium is that you can use it to take screen captures of the browser. Then using a technique as described in Find differences between images using C#, you can highlight the differences between previous snapshots.

    This example shows how to grab a screenshot of the homepage and then create a difference image. With a little extra tinkering, you can use the same technique to count the pixels that are different.

    [Test]
    public void CompareHomePageToPrevious()
    {
        string fileName = Path.Combine(Environment.CurrentDirectory, "homepage.bmp");
        var selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://mywebsite");
        selenium.Start();
        selenium.Open("/");
        selenium.CaptureEntirePageScreenshot(fileName, "");
    
        // Load current and previous captures
        var current  = new Bitmap(filename);
        var previous = new Bitmap(_previousHomepageImage);
    
        // Find all pixels that are the same and make them pink
        Bitmap diff = ImageTool.GetDifferenceImage(current,previous,Color.Pink);
        diff.MakeTransparent(Color.Pink);
    
        // Save the image for viewing
        // or count the number of different
    }
    

    Selenium is a really interesting choice because it's cross-platform and cross-browser, meaning that you can capture screens of different browsers. You can use this technique to compare snapshots between builds or to compare between browsers.

提交回复
热议问题