Web 应用程序样式和布局的回归测试

发布于 2024-10-19 14:30:02 字数 254 浏览 10 评论 0原文

我意识到这是一项不平凡的任务,但是有没有办法对 Web 应用程序的样式和渲染布局进行回归测试?

我发现服务器端和客户端的单元测试和回归测试功能都很简单。

然而,我遇到的一个令人沮丧的问题是,为解决一个布局问题而进行的 CSS 更改会破坏另一页面上的布局和样式。我知道如何检测这些的唯一方法是手动查看应用程序中的每个页面,并将其与我自己的期望进行比较,但显然,当应用程序可以有数十个页面时,这可能会很繁重且昂贵。是否有任何研究使用图像处理或其他技术来自动检测此类问题?

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 test and regression test functionality, both server-side and client-side.

However, a frustrating issue I've run into are CSS changes made to fix one layout issue that break the layout and styling on a different page. The only way I know how to detect these is to manually view every page in an application, and compare it with my own expectations, but obviously this can be burdensome and expensive when an application can have dozens of pages. Has there been any research using image processing or other techniques to detect these kinds of problems automatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜深人未静 2024-10-26 14:30:02

实际上,Selenium 的隐藏瑰宝之一是您可以使用它来捕获浏览器的屏幕截图。然后使用中描述的技术查找差异使用 C# 在图像之间进行比较,您可以突出显示以前快照之间的差异。

此示例演示如何抓取主页的屏幕截图,然后创建差异图像。通过一些额外的修改,您可以使用相同的技术来计算不同的像素。

[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 是一个非常有趣的选择,因为它是跨平台和跨浏览器的,这意味着您可以捕获不同浏览器的屏幕。您可以使用此技术来比较构建之间的快照或浏览器之间的比较。

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.

瀟灑尐姊 2024-10-26 14:30:02

有一种方法可以使用 Galen Framework 测试 Web 应用程序的布局。这个工具有自己的语言,非常容易学习和理解。它是基于 Selenium 的,如果您想测试您的产品,您可以在 Selenium Grid (Sauce Labs) 中运行测试不同浏览器中的应用程序。

该工具获取页面上指定元素的位置并检查它们之间的相对关系。

示例:如果您想检查菜单窗格是否位于标题下方并拉伸至浏览器的宽度且高度为 50 像素,您可以这样做:

menu
    below: header 5px
    width: 100% of screen/width
    height: 50px

此工具也可用于测试响应式设计。

您可以在官方网站 http://galenframework.com 上找到完整的文档。

最好的部分是您甚至可以创建 Java 测试。 Galen JavaScript API 也可与 GitHub 上的示例项目一起使用。

同样,编写一次的测试可以在应用程序生命周期的多个阶段使用。

There is a way to test the layout of a web application using Galen Framework. This tool has its own language and is very easy to learn and understand. It is a Selenium-based and you can run tests in Selenium Grid, (Sauce Labs) if you want to test your application in different browsers.

This tool gets the location of a specified element on the page and check them relatively to each other.

Example: if you want to check that a menu pane is below the header and stretches to the width of a browser and has 50 pixels height, you can do it like this:

menu
    below: header 5px
    width: 100% of screen/width
    height: 50px

This tool can also be used to test responsive designs.

You can find complete documentation on the official website, http://galenframework.com.

The best part is you can even create Java tests. The Galen JavaScript API is also available along with the sample projects on GitHub.

Again, test(s) written once can be used at multiple phases of the application life cycle.

梦里南柯 2024-10-26 14:30:02

我想您可以生成网络应用程序中每个页面的“理想”镜头以用作参考。

然后在 CSS 更新后生成每个页面的新镜头,并与之前的页面进行比较。如果您确保始终保持相同的分辨率等,则 1:1 比较应该没问题。

您甚至可以这样做,以便您可以告诉它,如果页面不同,则另一个页面实际上比示例“更好”,并且使用“其他”作为接下来运行的示例。

这样,当您修复某些内容时,您可以看到一个页面的差异,并将其标记为更好,然后修复另一个页面上的错误,同时确保您不会回归最初尝试修复的错误。

I guess you could generate an 'ideal' shot of each page in your web application to use as reference.

Then generate a new shot of every page after a CSS update and compare with the previous ones. A 1:1 comparison should be okay if you make sure you always keep the same resolution, etc.

You could even make it so that you can tell it that if the page differs, the other page is actually 'better' than the example and to use the 'other' as the example for the next runs.

That way, when you fix something you can see the difference for one page, and mark it as being better, and then fix a bug on another page while making sure you don't regress the bug you were trying to fix in the first place.

韶华倾负 2024-10-26 14:30:02

您应该查看网络一致性测试作为进行回归测试的方法。

You should check out Web Consistency Testing as an approach to do your regression testing.

鹿童谣 2024-10-26 14:30:02

如果您使用 Ruby 进行开发,请查看 rspec-page-regression宝石。它是一个 RSpec 插件,可让您将请求的页面快照与预期图像进行比较。它与 Poltergeist 配合使用,这是一个 Capybara 驱动程序,使用 PhantomJS 并支持渲染页面快照。

Rspec-page-regression 提供了一个 RSPec 匹配器,可以让您执行类似的操作

context "user page" do
  before(:each) do
    visit user_path
  end

  it { page.should match_expected }

  context "popup help" do
    before(:each) do
      click_button "Help"
    end

    it { page.should match_expected }
  end
end

,并且它提供了一种管理期望图像的机制。

免责声明:我是宝石的作者。

If you're developing in Ruby, take a look at the rspec-page-regression gem. It's an RSpec plugin that lets you compare requested page snapshots against an expected image. It works with Poltergeist, which is a Capybara driver that uses PhantomJS and supports rendering page snapshots.

Rspec-page-regression provides an RSPec matcher that lets you do things like

context "user page" do
  before(:each) do
    visit user_path
  end

  it { page.should match_expected }

  context "popup help" do
    before(:each) do
      click_button "Help"
    end

    it { page.should match_expected }
  end
end

And it provides a mechanism to manage the expectation images.

Disclaimer: I'm the author of the gem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文