临时文档存放处,如果想围观我的话请移步到本人独立博客——http://besteric.com

Watin Testing Pattern

上一篇 / 下一篇  2009-09-15 15:10:08 / 个人分类:蛋疼的自动化

原文地址:http://averyblog.com/net/watin-testing-pattern/


On my current project we have zero unit tests. I inherited the application and the existing unit tests where such a mess it was better to just kill em and start over. I would normally start from the data layer and work up, but since we are working toward a release in a couple weeks I need the best bang for my buck. So I decided to start with some UI functional tests, these will also be a major asset once we start really refactoring the application.

I have used Watir in the past, but since this is a .NET project I think it makes more sense to go with Watin. Before getting started with Watin I did some research to see what the "best practices" were. The watin site has some good samples, but not much in the way of patterns or best practices. The site does link to this blog post from Richard Griffin which outlines a design pattern, but the pattern is a little too procedural for my liking. Perhaps it is all the ruby lately, but more and more I want clean object oriented solutions whenever possible.

So I came up with my own pattern. I wanted to completely hide the IE object and make writing tests as intuitive and easy as possible, while following DRY and KISS. So I decided to start with an object for each page, here is what I started with.

publicclassHomePage : IE
{
publicconststringHomePageURL ="default.aspx";

publicHomePage() :base(ConfigurationManager.Host
+ HomePageURL) { }
}

Instead of playing around with passing the IE object around, I decided to just inherit from it. In your test if you want to test the homepage you just have to say

HomPage home =newHomePage();

and use the home object in the exact way you would the IE object. Then I include properties for the various textboxes and such on the page:

publicTextField UserIDField {
get{returnTextField(Find.ByName("txtUsername")); } }
publicTextField PasswordField {
get {returnTextField(Find.ByName("txtPassword")); } }
publicButton ContinueButton {
get {returnButton(Find.ByName("btnContinue")); } }

and also methods for the main actions on the page:

internalvoidLogin(stringusername,stringpassword)
{
UserIDField.TypeText(username);
PasswordField.TypeText(password);
ContinueButton.Click();
}

Now if I want to write a test to test logging in my test looks like this:

[Test]
publicvoidLogin()
{
HomePage home =newHomePage();
home.Login("james","password");
Assert.IsTrue(home.ContainsText("My Stuff"));
//more assertions here
home.Close();
}

The only other thing I found I needed was a way to change what page object I am working with, so I added another constructor that lets you "switch" the page you are on:

publicHomePage(IE instance) :
base(instance.InternetExplorer) {}

This way I can add methods that return a different page like this one:

internalUserRegistrationPage GoToUserRegistration()
{
UserRegistrationLink.Click();
returnnewUserRegistrationPage(this);
}

You would only need this for tests that move from page to page, otherwise you could just create a new page object. So far it has been working great, I will post an update if I encounter any issues or make any major changes. If I find the time I am going to try and add a feature to the Watin recorder to generate these page objects, it wouldn't be too hard and it would make it

WatinFiles-1.zip

-James


TAG:

 

评分:0

我来说两句

Open Toolbar