WebAii (http://www.artoftest.com/) is a free, though not open source test framework for testing web applications. Like Watir/Watin/Watij, it works by driving the browser, either IE or Firefox. Like Watin, it allows writing your tests in C#. It can be run under NUnit, Visual Studio Team Test, or your own application.

In this description, I'm using Windows XP Pro SP2, Microsoft Visual C# 2008 Express Edition version 9.0.21022.8 RTM, NUnit version 2.4.4, WebAii version 1.0 RC2.

Installation & Hello Worl^H^H^H^H^H Google

  1. Set things up similarly to that described in VisualStudioCheatSheet. In this case, I set the project to output type of Class Library and run it within an externally launched NUnit.

  2. Add the ArtOfTest WebAii .dlls to the project references.

  3. Copy ArtOfTest.WebAii.NUnitExtension.dll from C:\Program Files\ArtOfTest\WebAii 1.0 (RC2) to C:\Program Files\NUnit 2.4.4\bin\addins

    1. You also have to add a reference (Project -> Add Reference...) in your test project to the addin .DLL for it to load properly and work.

    2. In NUnit, under Tools -> Addins..., you should see WebAiiTestCaseBuilder. If it shows a status of Error, then perhaps your project doesn't have the reference or you haven't rebuilt the project since you added the reference.

      • ??? The docs show WebAiiTestFixtureBuilder, also. I don't see this addin.

  4. Extend BaseNUnitTest for your test fixture. This gives you some convenience methods.

    • According to the docs a template should be installed to do this for you. I didn't find that to be true--I later found them installed under "Visual Studio 2005", instead of the Visual Studio 2008 that is actually installed.

    1. Call Initialize() at the start of your test or in the SetUp method. This is part of BaseNUnitTest and does all the work of initializing the Manager.

    2. Call Manager.LaunchNewBrowser() to prepare a browser that your test will use to execute your tests.

      • This wasn't working for me. It launched the browser, but never "connected." I'm not sure if this is connecting the test runner to the browser instance, or something else. It waited for 60 seconds (the default timeout) before reporting the error.

        I posted a query on the ArtOfTest User Forums. Apparently I was missing the Primary Interop Assemblies. Installing that gave me a working test. I noted that the PIA installation (I think--I hadn't noticed this change before.) also installed new versions of the following:

        c:\WINDOWS\system32\kernel32.dll
        c:\WINDOWS\system32\user32.dll
        c:\WINDOWS\system32\shell32.dll
        c:\WINDOWS\system32\ntoskrnl.exe
    3. Use the ActiveBrowser to navigate to the desired page

    4. Assert things about the page that's returned.

    5. Call this.CleanUp() at the end of your test or in the TearDown method. This is part of BaseNUnitTest and releases all of the resources allocated during Initialize().

At this point, my test class is (I deleted some unused "using" statements):

using System;
using System.Text;

using ArtOfTest.WebAii.Core;
using ArtOfTest.WebAii.ObjectModel;
using ArtOfTest.WebAii.TestTemplates;
using ArtOfTest.WebAii.TestAttributes;
using ArtOfTest.WebAii.Controls.HtmlControls;

using NUnit.Framework;


namespace WebAiiDemo
{
    [TestFixture]
    public class GoogleTests : BaseNUnitTest
    {
        [Test]
        public void TestNavigateToPage()
        {
            Initialize();

            // Launch an instance of the browser
            Manager.LaunchNewBrowser();

            // Navigate to google.com
            ActiveBrowser.NavigateTo("http://www.google.com");

            // verify the title is actually google.
            Assert.AreEqual("Google", ActiveBrowser.Find.ByTagIndex("title", 0).InnerText);
            this.CleanUp();
        }
    }
}

SetUp and TearDown

Let's extract the common stuff in preparation for other expectations on this page:

namespace WebAiiDemo
{
    [TestFixture]
    public class GoogleTests : BaseNUnitTest
    {
        [Test]
        public void TestNavigateToPage()
        {
            // verify the title is actually google.
            Assert.AreEqual("Google", ActiveBrowser.Find.ByTagIndex("title", 0).InnerText);
        }

        [SetUp]
        public void SetUp()
        {
            Initialize();
            // Launch an instance of the browser
            Manager.LaunchNewBrowser();
            // Navigate to google.com
            ActiveBrowser.NavigateTo("http://www.google.com");
        }

        [TearDown]
        public void TearDown()
        {
            this.CleanUp();
        }
    }
}

This allows us to easily create new tests for the page:

        [Test]
        public void VerifyTitleByTagIndex()
        {
            Assert.AreEqual("Google", ActiveBrowser.Find.ByTagIndex("title", 0).InnerText);
        }

        [Test]
        public void VerifyTitleByXpath()
        {
            Assert.AreEqual("Google", ActiveBrowser.Find.ByXPath("//title").InnerText);
        }

        [Test]
        public void SubmitFormByXpath()
        {
            Element form = ActiveBrowser.Find.ByXPath("//form");
            Element queryField = ActiveBrowser.Find.ByXPath("//form//input[@name='q']");
            Assert.IsNotNull(queryField, "looking for Query field");
            ActiveBrowser.Actions.SetText(queryField, "WebAii");
            Element lucky = ActiveBrowser.Find.ByXPath("//form//input[@value=\"I'm Feeling Lucky\"]");
            Assert.IsNotNull(lucky, "looking for Lucky button");
            ActiveBrowser.Actions.Click(lucky);
            String expectedTitle = "ArtOfTest Inc. - Software Testing Made Easy";
            Assert.AreEqual(expectedTitle, ActiveBrowser.Find.ByXPath("//title").InnerText);
        }

Notice that this last test finds page elements using XPath expressions, and manipulates them (entering text and pressing buttons) using the ActiveBrowser. This is a powerful technique, but I'm not happy with duplication I've introduced. Note also the Assert.IsNotNull() calls; these helped me figure out where things when wrong when I had a bad XPath expression. Let's extract a method:

        [Test]
        public void SubmitFormByXpath()
        {
            Element form = ActiveBrowser.Find.ByXPath("//form");
            Element queryField = FindByXPath("//form//input[@name='q']");
            ActiveBrowser.Actions.SetText(queryField, "WebAii");
            Element lucky = FindByXPath("//form//input[@value=\"I'm Feeling Lucky\"]");
            ActiveBrowser.Actions.Click(lucky);
            String expectedTitle = "ArtOfTest Inc. - Software Testing Made Easy";
            Assert.AreEqual(expectedTitle, ActiveBrowser.Find.ByXPath("//title").InnerText);
        }

        private Element FindByXPath(String xpath)
        {
            Element docElement = ActiveBrowser.Find.ByXPath(xpath);
            Assert.IsNotNull(docElement, xpath);
            return docElement;
        }

ToBeWritten ...


CategoryCheatSheet

iDIAcomputing: WebAiiCheatSheet (last edited 2009-07-27 18:25:46 by localhost)