Skip to content
May 31, 2011 / pauldundon

Using Selenium with Firefox 4 and Microsoft AJAX

Selenium provides a framework for browser-based testing which can be integrated with unit test frameworks such as NUnit or unit test projects in Visual Studio. Part of the Selenium product set is the Selenium IDE, which allows you to record the actions required for a test using Firefox, and produce C# code to reproduce them in your unit tests. Although using the IDE seems to be the less preferred approach to using Selenium, it is still supported.

The Selenium IDE is not flagged as compatible with FF4, but does work with it. To use it, first install the Firefox Add-on Compatibility Reporter to disable add-on version checking.

Next install the IDE add-on.

To launch the IDE, select Selenium IDE from the Tools menu in Firefox (if the tools menu isn’t visible, hit the Alt key). You’re now ready to start recording the actions for your test. You can copy this to the clipboard as a C# code fragment.

To make use of this, you will need to download the .NET client files for Selenium. Unzip the download and create a project with references to the dlls inside. You can now incorporate the C# code generated by the IDE into this project. You will need the following imports:

using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using Selenium;

The generated code relies on a local variable with the name selenium; the following code will provide this:

// You may use any WebDriver implementation. Firefox is used here as an example
IWebDriver driver = new FirefoxDriver();
// A "base url", used by selenium to resolve relative URLs
String baseUrl = "http://localhost"
// Create the Selenium implementation
ISelenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

If your website uses Microsoft AJAX, you will need to add code to pause the progress of your test during asynchronous updates (Selenium will automatically insert code to wait for full page GETs or POSTS). The following code provides this:

        private bool JSEval(IWebDriver driver, string script) {
            object result=((IJavaScriptExecutor)driver).ExecuteScript(script);
            if (result==null) {
                return false;
            }else {
                return (bool)result;

        private void JSWait(IWebDriver driver, string script, long timeout)
            int interval = 500;
            long waited = 0;
            while (!JSEval(driver, script) && waited < timeout)
                waited += interval;

        private void ClickAndWait(ISelenium selenium,IWebDriver driver, string control)
            JSWait(driver, "return Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();", 1000);
            JSWait(driver, "return !(Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack());", 50000);          

The first two functions duplicate the functionality of a Selenium method called WaitForCondition. The current implementation of WaitForCondition appears to have a bug in it which means it times out after 500ms, but hopefully future some future release will address this so that the calls to JSWait in the above code could be replaced.

The “control” parameter to ClickAndWait is the ID of the control to be clicked. The IDE will create a call to selenium.Click with this ID, so it’s simply a matter of calling ClickAndWait instead where the click prompts an Ajax postback.


Leave a Reply

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

You are commenting using your 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

%d bloggers like this: