Hello all
Today I will show how to test UI ( User Interfaces ) in .NET with WatiN . 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 :
- Add reference as the documentation of WatiN, you dont have to follow the example which is documented there just add the reference.
- If you don’t know how to add reference please see this and for NUnit see this.
- Visit this link for 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.
using WatiN.Core;using NUnit.Framework;using WatiN.Core.DialogHandlers; namespace TestStie{ [TestFixture] public class AddArticleTest { [TestFixtureSetUpAttribute] public void SetUp() { } [Test] public void TestAddArticle() { using (IE ie = new IE("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 = new AlertDialogHandler(); ConfirmDialogHandler confirm = new ConfirmDialogHandler(); using (new UseDialogOnce(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 void TearDown() { } }}
Thats all for today.
BYE
nice article. Watin is a nice UI testing tool though it recorder is not so robust. But it is very easy to install. Just add two reference watin.core.dll & watin.core.unittests.dll
visit My Blog