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

UI test with WatiN for TDD in .NET

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

原文地址:http://blog.actcode.com/2008/11/ui-test-with-watin-for-tdd-in-net.html

Hello all

Today I will show how to test UI ( User Interfaces ) in .NET withWatiN. WatiN is UI test framework. Though its a very simple but i have felt the documentation is not rich enough with example. So i have taken an initiative to make it more userfriendly for the developers.

I will not emphasize on how to configure WatiN as its already well documented in the following links. What i will try to do is make familiar with HTML controls that's needed to be validated .

Follow accordingly to configure WatiN :

  1. Add reference as thedocumentationof WatiN, you dont have to follow the example which is documented there just add the reference.
  2. If you don't know how to add reference please seethisand for NUnit seethis
  3. Visitthis linkfor a good CodeProject's example.

One of the common mistake we do is, open a class file in our website project and start the writing the test code in it, but what we have to do is make a new Class Project ,add reference in that project & select as startup project for NUnit test.

usingWatiN.Core;
usingNUnit.Framework;
usingWatiN.Core.DialogHandlers;

namespaceTestStie
{
[TestFixture]
public classAddArticleTest
{

[TestFixtureSetUpAttribute]
public voidSetUp()
{
}

[Test]
public voidTestAddArticle()
{
using(IE ie =newIE("http://localhost:2456/testsite/login.aspx"))
{

/*
* For textbox and textarea we have to use TextField() method and to search by
* id of the control we have to use Find.ById() method & to fill the textbox
* we have to use TypeText() method as following.
*/

ie.TextField(Find.ById("ctl00_UserName")).TypeText("maxpain");
ie.TextField(Find.ById("ctl00_Password")).TypeText("c0de71~123");

/*
* For make a check box cheked we have to do as following.
*/
ie.CheckBox(Find.ById("ctl00_chkActive")).Checked =true;

/*
* To upload a file we have to do as bellow.
*/
ie.FileUpload(Find.ById("ctl00_LogoFile")).Set(@"C:\calligraphy.jpg");

/*
* To selct a value form. a dropdown list we have to follow as bellow.
* In the exmaple "Software Engineer" is selected item's value.
*/
ie.SelectList("ctl00_BodyPlaceHolder_ddlSkillCategory").Select("Software Engineer");

/*
* For make a radio button cheked we have to do as following.
*/
ie.RadioButton(Find.ByName("ctl00$BodyPlaceHolder$rbPublish")).Checked =true;

/*
* To click a link we have to do as bello
*/
ie.Link(Find.ByUrl("http://localhost:2456/testsite/AddArticle.aspx?id=3")).Click();

/*
* To click a button we have to do as bellow.
*/
ie.Button(Find.ById("ctl00_LoginButton")).Click();

/*
* Suppose we have to make confirm a delect action through JS confirmation box when we click
* a link, then we have to do as follow.
*/
AlertDialogHandler alertDialogHandler =newAlertDialogHandler();
ConfirmDialogHandler confirm =newConfirmDialogHandler();
using(newUseDialogOnce(ie.DialogWatcher, confirm))
{
Link link = ie.Link(Find.ByUrl("http://localhost:2456/testsite/AllCaseStudy.aspx?id=5"));

link.ClickNoWait();
confirm.WaitUntilExists();
confirm.OKButton.Click();
}
ie.WaitForComplete();

/*
* Now to check whether our operatin is successful or not we have to do assert test
* as we do in unit test. Here ContainsText("xxxx") method returns true when it found
* the "xxxx" string in the page appear after any event perform.(generally we show
* a confirmation message after any successful event performed.)
*/
Assert.IsTrue(ie.ContainsText("News Deleted successfuly"),"Deleted id not found.");
}
}

[TestFixtureTearDownAttribute]
public voidTearDown()
{
}
}
}

Thats all for today.

BYE

UserScrumPadfor your Agile based projects.



TAG:

引用 删除 zhong_z82   /   2010-07-19 14:42:01
-5
 

评分:0

我来说两句

Open Toolbar