Archive for the ‘Test’ Category

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 :

  1. Add reference as the documentation of 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 see this and for NUnit see this
  3. 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

User ScrumPad for your Agile based projects.

Hello all

Today I will show you how to do unit test of a .NET project with NUnit. NUnit is a great tool to do unit test of any .NET project.As its a third party tool so you have to download it from here . I assume that you all are familiar with unit test.For the simplicity I will show you a simple example,Once when you are habituated with this took you will find yourself in many complex test script.

first I opened an new class project named Calculation.Core.

then I added two method which will be tested through NUnit.

The class file’s is "Calculation" and those two methods are DoAdd & DoSub which will add two numbers and subtract two numbers.Both’s return type and parameters type are integer .

namespace Calculation.Core
{
    public class Calculation
  
{

        public int DoAdd(int firstNumber, int secondNumber)
        {
            return firstNumber + secondNumber;
        }

        public int DoSub(int firstNumber, int secondNumber)
        {
            return firstNumber – secondNumber;
        }
    }
}

now I added another class project named Calculation.Test and Add Reference of the Calculation.Core project. and also make Add DLL Reference of  NUnit’s "nunit.framework.dll" from my NUnit’s installed location "C:\Program Files (x86)\NUnit 2.4.8\bin\nunit.framework.dll" .This will vary according to your installation of NUnit.

now I have to include namespace in the file by typing :

using NUnit.Framework;

now we have to add the "[TestFixture]" attribute to the CalculationTest class and "[Test]" attribute to the DoAddTest ,DoFailTest & DoSubTest methods.

using NUnit.Framework;
namespace Calculation.Test
{
   [TestFixture]
    public class CalculationTest
    {
       [Test]
       public void DoAddTest()
       {

           Calculation.Core.Calculation sub = new  Calculation.Core.Calculation();
           int result = sub.DoSub(3,1);
           Assert.Greater(3, result);
       }

       [Test]
       public void DoSubTest()
       {
           Calculation.Core.Calculation sub = new Calculation.Core.Calculation();
           int result = sub.DoSub(3, 1);
           Assert.Greater(3, result);
       }

       [Test]
       public void DoFailTest()
       {
           Calculation.Core.Calculation sub = new Calculation.Core.Calculation();
           int result = sub.DoSub(3, 1);
           Assert.Greater(1, result);
       }
    }
}

Now we have to select the Calculation.Test project as Starup project and select the "Properties" from the Solution Explorer .

Now form the Calculation.Test tab we have to select Debug then select  Start External Program and set the location of NUnit.exe then select Command line argument and write the dll name of the test project .

now when we will run the application then NUnit will automatically start and we have to press run button to show the unit test result.

In the result we will see that the two method of our test script has passed but one fail as I didn’t give the proper assert method .

in this way we can do unit test with NUnit. There are also many useful method of NUnit’s to test the script.

BYE