What is Selenium and how can it help me test SharePoint?

I don’t want to give a full blow account of what Selenium is because it’s very well documented at Selenium HQ, however, I do want to give a quick insight into how it can help you test the UI of a SharePoint application.

Quick overview of Selenium

Firstly, Selenium is an open source testing framework that allows you to emulate the actions of a user when navigating and interacting with a web site through standard browsers (such as IE, Firefox or Chrome). When your scripts are running your chosen browser pops up and can be watched as your steps are replayed back at you. It’s just like watching a user use the application in exactly the same way every time.

Does Selenium have a GUI?

Yes. Selenium has a GUI called the Selenium IDE and will actually record you interacting with a web site. Once you have completed those actions they can then be exported to your chosen programming language. In a SharePoint persons case, this is likely to be C# however you can choose what you want.

Do I use the GUI?

Not much – but it’s very cool. However, very quickly you will find that the GUI approach (in SharePoint) just wont cut it for the more complicated pages. The scripts quite often don’t replay how you want them to, and the XPath statements it produces are too brittle. What do I mean by brittle? Instead of identifying a particular HTML element (TD, Div, Table, etc) via name, id or some more stable selector, the GUI will often derive it based on XPath statements using indexes. If you are going to use Selenium I would strongly recommend you dive in and learn how to programmatically configure it. It’s NOT hard to understand – I picked it up in a day. The GUI is very useful when you are first learning Selenium  as you can record something then see what it outputs.

Does Selenium support SharePoint?

Yes and no. Selenium can test any HTML based web site and as SharePoint is based on HTML this also means you can use Selenium with it. (Although, it won’t help you test any Silverlight components such as the Organization Browser).

Even though Selenium has no problems dealing with SharePoint’s pages and elements it’s a bit of a pain. Most of the major SharePoint pages and objects such as the Document Library page, Ribbon control, Site Actions Menu and Web Part picker use Javascript heavily. You need to know what you are doing and make sure you are clicking on the correct elements at the correct time. The good news is, once someone has been through the pain of ‘mapping’ the elements of SharePoint pages into something that can be tested – we can all benefit. This is where the ShareTest idea came from.

ShareTest and SharePoint

ShareTest is a lightweight framework written in C# that follows the Page Object pattern. The main goal for ShareTest is to provide a ‘page object’ for each commonly used SharePoint page. In the example below, we are returning a list of documents from a SharePoint document library called ‘Healthcare Documents’. Once they are returned (as a list of documents) we are then simply asserting that various values exist. The  ‘SPTDocumentLibraryPage’ has been developed so you can use it with any standard document library.

If we didn’t use page objects, then you would find yourself writing many tests that ‘speak HTML’ making them less readable and more error prone when mark-up changes. Doing it this way – with page objects – we are able to map the page onceand supply a nice abstracted page object to the tester.

[Test]
public void Test_Documents_Are_Displayed()
{
    // defines the path to an Auto It exe that logins.
    this.UserLoginExeName = TestHelper.GetFileNameFromUserType(TypeOfUser.DocReadAdmin);

    // Get all the documents from the library on first page
    SPTDocumentLibraryPage<DocReadDocItem> documentLibraryPage =
        new SPTDocumentLibraryPage<DocReadDocItem>(
            this.driver,
            Urls.Default.SiteUrl + Urls.Default.HealthCareDemoDocuments,
            null,
            this.UserLoginExeName);

    // Returns a list of documents (you can also supply your own entity).
    List<DocReadDocItem> documents = documentLibraryPage.GetDocuments();

    // Normal NUnit test assertions - but now we are testing strongly typed objects
    Assert.That(documents[0].Title == "Alcohol and drug abuse policy");
    Assert.That(documents[2].Editor == @"AD2008R2\administrator");
    Assert.That(documents.Count == 10);
}

Want to contribute to ShareTest?

ShareTest is still young and needs support from others to provide support for other types of pages and objects in SharePoint. Once we get the big hitters well covered, I truly believe that automating UI testing in SharePoint is well and truly on the cards.

Leave a Reply 0 comments

Leave a Reply: