Automated Web Testing (2) Using Selenium

Selenium is another automated web application testing framework. Unlike WatiN, which has only 3 developers, Selenium is developed by a team of programmers and testers in ThoughtWorks, so that it could be more powerful:

  • The tests can be written as HTML tables or coded in a number of popular programming languages, including C#, Perl, Java, PHP, Python, and Ruby;
  • The tests can be run in most modern web browsers, including IE, Firefox, Safari, Opera, Chrome;
  • Selenium can be deployed on Windows, Linux, and Macintosh.

Last year, I was using Selenium 1.0 beta1 with a lot of issues found, and sometimes the Java source code has to be manually modified. Now the beta2 is released, working much better than before, with a few issues in Safari.

Preparation

Selenium has a couple of components. The remote control is going to be used.

In the downloaded package, two folders are needed:

  • selenium-server-1.0-beta-2 is the remote control, which is a server written in Java. It simulates a web server to drive the tested page in order to satisfies the JavaScript same-origin policy. And the commands for the tested page are received via Http;
  • selenium-dotnet-client-driver-1.0-beta-2 is the client driver containing the .NET assemblies we are going to refer.

webtest-selenium1

Extract these 2 folders to somewhere, like E:\Software\Selenium.

Since the remote control is written in Java. We also need to install JRE from here.

Getting started

First of all, start up server, the remote control. We can create a batch file like this:

java -jar E:\Software\selenium\selenium-server-1.0-beta-2\selenium-server.jar

Sometimes the java command does not work in Windows Server 2008. To resolve this, please:

  • use the full path of your java.exe, like C:\Program Files\Java\jre6\bin\java;
  • or add the folder path of java.exe to your PATH environment variable.

After the server is started, we can see its information:

webtest-selenium2

Then in Visual Studio, add a reference to selenium-dotnet-client-driver-1.0-beta-2\ ThoughtWorks.Selenium.Core.dll, and add a test class “SeleniumTest.cs” to our test project. So that we can write a test:

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Selenium;

[TestClass]
public class SeleniumTest
{
    private DefaultSelenium _firefox;

    [ClassInitialize]
    public void StartSelenium(TestContext testContext)
    {
        this._firefox = new DefaultSelenium("localhost", 4444, @"*firefox", "http://localhost:4444");
        this._firefox.Start();
    }

    [TestMethod]
    public void GoogleTest()
    {
        bool hasText;

        this._firefox.Open("http://www.google.com");
        this._firefox.Type("name=q", "Dixin");
        this._firefox.Click("name=btnG");
        this._firefox.WaitForPageToLoad("10000");
        hasText = this._firefox.IsTextPresent("Dixin");

        Assert.IsTrue(hasText, @"The search result does not contain text ""Dixin"".");
    }

    [TestCleanup]
    public void CloseBrowser()
    {
        this._firefox.Close();
    }

    [ClassCleanup]
    public void StopSelenium()
    {
        this._firefox.Stop();
    }
}

Please notice the port used by the remote control is 4444.

If the browser does not work, add the path of its folder to the PATH environment viarable, and restart the server.

You can watch how the remote control and the browser work.

webtest-selenium3

More about Selenium

Selenium works very differently from WatiN. Here is the detailed introduction of Selenium, and here is the document for the client .NET library.

Here is another article on writing unit test for Silverlight using Selenium.

Tools for Selenium

webtest-selenium4

Selenium IDE can be used to record, edit, and debug tests. Selenium IDE includes the entire Selenium Core, can be used to easily record and play back tests in the actual environment that they will run. Selenium IDE is not only recording tool, but a complete IDE.

webtest-selenium5

It can be downloaded from here.

WatiN vs. Selenium

WatiN is easy to use and lightweight enough for .NET developers, while its current problem is to cross browsers. But for Selenium, a Java server has to be used. Here is a detailed table for the comparison.

No Comments