Page对象的模式和替代品 [英] Page Object pattern and alternatives

查看:145
本文介绍了Page对象的模式和替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您自动化的Web UI测试要组织你的测试,使他们维护和code复制被最小化成为可能。上要走的路是页对象模式

When you automate web UI testing you want to organize your tests so that they are maintainable and code duplication is minimized as possible. On of the way to go is Page Object pattern.

你有没有尝试它在现实世界中的项目?是否有其它方法吗?你如何建模复杂的网站(几嵌套母版页,弹出窗口,大疯狂的验证形式)?我感兴趣的一般模式,以及在特定情况下(硒/ ASP.NET MVC / NUnit的)。

Did you try it in real world projects? Are there any alternatives? How do you model complex sites (few nested master pages, popups, large forms with crazy validations)? I'm interested in general patterns as well as in specific cases (Selenium/ASP.NET MVC/NUnit).

推荐答案

您已经问了很多问题,我会尽量回答他们几个。

You've asked quite a lot of questions, I'll try to answer a few of them.

我已经使用Selenium使用页面对象的Web应用程序,并在桌面WinForms应用程序(而严格不是页面对象,我用它以同样的方式 - 视图对象,也许)。我的结论是,它的伟大工程,我肯定推荐它。

I've used Page Object in a web app using Selenium and also in a desktop WinForms app (while that's not strictly Page Object, I've used it in the same way -- View Object, perhaps?). My verdict is that it works great and I'd definately recommend it.

下面是一个什么样的测试可能看起来像,我们写的方式很短的例子:

Here's a short example of what a test might look like, the way we wrote it:

[Test]
public void AccountPageNameIsLoggedInUsersName()
{
    FirstPage() // Returns FirstPage
        .LoginAs("tobbe", "s3cr3t") // Returns LoggedInPage
        .ClickOnMyAccount() // Returns MyAccountPage
        .AssertThat(p => p.Name, Is.EqualTo("tobbe")); // p is of type MyAccountPage
}

在这里,硒魔术放在第一页()方法和页面中。这样,您就可以隐藏所有从测试不必要的实现细节。我想你可以找出方法是如何实现的。

Here, the Selenium magic is placed inside the FirstPage() method and the pages. This way you hide all the unnecessary implementation details from the test. I guess you can figure out how the methods are implemented.

这是隐藏在网页里面的东西硒奖金是,你可以在不改变测试,将其转换成如其中PageObject重新presents视图模型 - 视图 - presenter测试(这是类似于我在WinForms应用程序一样)。

A bonus from hiding the Selenium stuff inside the pages is that you could, without changing the test, convert it into e.g. a Model-View-Presenter test where the PageObject represents the view (that's similar to what I did in the WinForms app).

关于母版页,我们所做的事情是,我们的装饰与接口的页面和创建扩展方法对这些接口:

Regarding master pages, what we did is that we decorated the pages with an interface and created extension methods on those interfaces:

public class LoggedInPage : Page<LoggedInPage>, IMainMenuHolder { ... }

public static class MainMenuHolderExtensions
{
    public static MyAccountPage ClickOnMyAccount(this IMainMenuHoder me) { ... }
}

这篇关于Page对象的模式和替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆