Click here to Skip to main content
15,885,200 members
Articles / Desktop Programming / Windows Forms

UITestBench, a lightweight UI testing library

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
3 Apr 2008CPOL9 min read 53K   226   41  
This article describes how to build a lightweight test bench for testing user interfaces which are written entirely in C#/.NET, using NUnit or any other unit test framework.
/*
 * This file is licensed under the Code Project CPOL License
 * http://www.codeproject.com/info/cpol10.aspx
 * 
 * © Steffen Schütte 2008
 */

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using de.steffenschuette.UITest.Framework;
using System.Threading;
using System.Windows.Forms;



namespace de.steffenschuette.UITest.DemoApp.UITest
{
    [TestFixture]
    public class TestFixture1
    {
        private UITestBench myTestBench;

        [SetUp]
        public void SetUp()
        {
            Console.WriteLine("Setup ThreadId: " + Thread.CurrentThread.ManagedThreadId);

            myTestBench = new UITestBench();
            myTestBench.StartApplication("UITestDemoApp", null);

            //Some some to start the demo app
            Thread.Sleep(2000);
        }

        [TearDown]
        public void TearDown()
        {
            myTestBench.TerminateApplication();
        }

        [Test(Description="TestCase 1")]
        public void TestCase1()
        {
            Console.WriteLine("TC1 ThreadId: " + Thread.CurrentThread.ManagedThreadId);

            myTestBench.PerformClick("Form1", "File/Open text file 1...");
            SendKeys.SendWait("file1.txt");
            SendKeys.SendWait("{ENTER}");
            Thread.Sleep(2000);

            myTestBench.PerformClick("Form1", "File/Open text file 2...");
            SendKeys.SendWait("file2.txt");
            SendKeys.SendWait("{ENTER}");
            Thread.Sleep(2000);

            myTestBench.PerformClick("Form1", "toolStrip1/Merge");
            Thread.Sleep(2000);

            myTestBench.SetText("MergeDlg", "MergeDlg/Demo Textbox", "Text has been set");
            Thread.Sleep(2000);

            myTestBench.PerformClick("MergeDlg", "MergeDlg/MixFilesBtn");
            Thread.Sleep(4000);

            myTestBench.SetSelectedIndex("MergeDlg", "MergeDlg/ListBox[2]", 1);

            Thread.Sleep(4000);

           
            myTestBench.PerformClick("MergeDlg", "MergeDlg/OK");
            Thread.Sleep(2000);

            myTestBench.PerformClick("Form1", "File/Exit");
        }

        [Test(Description = "TestCase 2")]
        public void TestCase2()
        {
            Console.WriteLine("TC2: ThreadId" + Thread.CurrentThread.ManagedThreadId);

            myTestBench.PerformClick("Form1", "File/Open text file 1...");
            SendKeys.SendWait("file1.txt");
            SendKeys.SendWait("{ENTER}");
            Thread.Sleep(2000);

            myTestBench.PerformClick("Form1", "toolStrip1/Merge");
            Thread.Sleep(2000);
            myTestBench.PerformClick("MergeDlg", "MergeDlg/MixFilesBtn");
            Thread.Sleep(2000);

            //No more testing as this use case forced an error in the aplication under test
            //to show that this does not crash the unit test
        }

     
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
I currently hold the following qualifications

- PhD in Computer Science
- M.Sc. in Software Technology
- Diplom (FH) in Computer Science

Comments and Discussions