Sunday, April 10, 2011

Generating c# scripts for Selenium 2.0 from the 1.0.10 Selenium IDE

For more information about why Selenium is now my favorite testing tool listen to this episode of Hanselminutes.

Anyway, I spent days looking for a WebDriver formatter for C# for the Selenium IDE.

Why, you might ask, do I want such a thing? Well, I don't need to set up and run the full Selenium server and client. I can test my ASP.Net applications directly from NUnit, thanks to the new WebDriver interface that has been added to the Selenium 2.0 beta.

When I couldn't find a formatter for the IDE, I started to write my own. As I was doing my research I found a "magical" class call WebDriverBackedSelenium. Hmmm, could this be something useful?

It turns out that not only was it useful, it was what I'd been looking for.

10 minutes later and I was in business.

Here's what you need to do to migrate your generated scripts from Se 1.0 to Se 2.0.

1. Download the Selenium Client Drivers for c# (Selenium WebDriver) from http://SeleniumHQ.org/download/.
2. Extract the drivers to an appropriate directory.
3. Follow the directions at http://seleniumhq.org/docs/appendix_installing_dotnet_driver_client.html with one exception. Include ALL of the .dlls that are in the driver directory.
4. Download and install NUnit http://www.nunit.org/?p=download (I've tested to 2.5.9)
5. Add a reference to the NUnit dll to your project
6. From the Selenium IDE, record your test. Click on the source tab. Select Options-->Format-->C# (Remote Control)
7. Copy the generated code and paste it into your test project.
8. Modify the code as follows
a. Add
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
after
using Selenium;


b. replace
private ISelenium selenium;

with
private IWebDriver driver = new FirefoxDriver();
private Selenium.WebDriverBackedSelenium selenium = null;

c. In the SetupTest method, replace
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://..../");

with
selenium = new WebDriverBackedSelenium(driver, "http://..../");

9. Compile and run through NUnit

4 comments:

  1. I'm just getting started with Selenium and this post was a huge help. Thank you!

    ReplyDelete
  2. In the end of this steps i had a problem with built DLL (VS 2010). When i optn it in NUnit i see an error : "This assembly was not built with any known testing framework". Do you have any idea how can i fix it?

    ReplyDelete
  3. I kept getting a "notsupportedexception" for all of my selenium functions after I used web driver like mentioned. I used the 2.9 C# version.

    ReplyDelete
  4. Do you think it is possible to write a program that doesn't need the IDE but uses its classes for code creation?
    So that there's a standalone program that captures the users actions using Selenium WebDriver and writes the C#-Test-code, using the Selenium IDE - classes?

    ReplyDelete