Using WatiN to automate Sitecore

As a tester, one aspect of my job that takes up a lot of my time is regression testing. This becomes even more of a problem when we have to upgrade Sitecore to a new version. This can often take days, so a way to automate Sitecore to complete basic tasks such as editing, saving and publishing content would save me a lot of time.

I had been using Selenium to automate all of my tests and most of the time I get exactly what I need from it, however when I tried to use Selenium to automate Sitecore I hit a brick wall.

The Selenium IDE uses CSS ID and class selectors to find elements on the web page. Sitecore generates different IDs for elements of its desktop every time you launch it so the Selenium selectors do not work as expected when you use the Selenium IDE recording functionality. This was really frustrating because I felt that automating Sitecore could save me a lot of time and effort, so after some googling I found out about WatiN, a powerful testing tool which enables you to automate your tests with Internet Explorer and Firefox using .Net.

After a bit research I finally found out how to write a test using the WatiN library to automate Sitecore. The test involves editing a content item, publishing it and then checking that the changes have been made on the front end.

To create your own WatiN test scripts you will need the following:

WatiN (http://watin.org/) – Inspired by Watir development of WatiN started in December 2005 to make a similar kind of Web Application Testing possible for the .Net languages. Since then WatiN has grown into an easy to use, feature rich and stable framework.

There is a handy video on the WatiN website which will give you a step by step guide to setting up and creating your first WatiN test script.

NuGet (http://nuget.org) – NuGet is a Visual Studio extension that makes it easy to install and update third-party libraries and tools in Visual Studio.

NUnit (http://www.nunit.org/) – NUnit is a unit-testing framework for all .Net languages. Initially ported from JUnit, the current production release, version 2.6, is the seventh major release of this xUnit based unit testing tool for Microsoft .NET. It is written entirely in C# and has been completely redesigned to take advantage of many .NET language features, for example custom attributes and other reflection related capabilities. NUnit brings xUnit to all .NET languages.

Here is an example of how to create a test script that will automate the Sitecore login process.

The first thing that we need to do is create a new project in Visual Studio and add the WatiN and NUnit packages using NuGet. We then need to add a reference to the WatiN.Core and the NUnit.Framework namespaces.

Now we have our test setup let’s write some code to demonstrate the API’s usage.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;
using NUnit.Framework;

namespace Test
{
    //These attributes mark the class as a NUnit test fixture
    [TestFixture]
    [RequiresSTA]

    public class EditContent
    {
        [Test]
        public void Sitecore()
        {   
            //Create a new instance of IE and navigates to a specified URL
            using (var browser = new IE("http://www.test.com/sitecore"))
            {
                //Finds the username field and types the text specified

                browser.TextField(Find.ByName("Login$UserName")).TypeText("username");

                //Finds the password field and types the text specified

                browser.TextField(Find.ByName("Login$Password")).TypeText("password");

                //Finds and clicks the options link

                browser.Link(Find.ByText("Options")).Click();

                //Finds and clicks the desktop button

                browser.Button(Find.ById("AdvancedDesktop")).Click();

                //Finds and clicks the login buton

                browser.Button(Find.ByName("Login$Login")).Click();
            }
        }
    }
}

The example code above opens an Internet Explorer instance and points it to your specified URL, it then uses the method ”Find.ByName” to search for the text field with the name ‘Login$UserName”. Finally we add the “TypeText” command so that it types the text “username”. It does the same again for the “Login$Password” field but this time types the text “password”.

We then find the desktop and login buttons using “Find.ById” and then we perform a “Click()” event. At this point if you try to run this test using NUnit, the test will fail. This is because the file Interop.SHDocVw.dll is not found, we need to make a change to the properties of the refence in the solution so that this file is created. To do this find and select the reference, then set ‘Embed Interop Types’ to ‘False’.

If you want more information on this please visit: http://stackoverflow.com/questions/5433990/watin-system-io-filenotfoundexception-interop-shdocvw

Now we can build the solution and run our test in NUnit successfully!

2 thoughts on “Using WatiN to automate Sitecore

  1. Nick August 14, 2012 / 7:53 am

    Hi Luke,

    I have also been using Selenium for automated testing of Sitecore with a reasonable degree of success, however the one area where Selenium seems to fall down with Sitecore is with Dialog windows, it seems to be impossible with the current release of Selenium to get a handle on these windows, which are obviously used frequently within Sitecore.

    I was wandering if you have written any WatiN tests that successfully open and interoperate with Dialogs in Sitecore?

    Thanks

    Nick

  2. lukeinmantc August 20, 2012 / 10:48 am

    Hi Nick,

    That was one of the problems we faced when trying to use Selenium to automate Sitecore. In WatiN I have been able to handle dialogs, please see the example code below:

    //Handle Dialog
    ConfirmDialogHandler handler = new ConfirmDialogHandler();
    using (new UseDialogOnce(browser.DialogWatcher, handler))
    {
    content.Link(Find.ByTitle(“Publish the item in all languages to all publishing targets.”)).
    ClickNoWait(); //The action that triggers the dialog
    handler.WaitUntilExists(60);
    handler.OKButton.Click();
    }

    I hope this helps!

    Thanks

    Luke

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s