Koen about .Net

February 16, 2011

Custom C# formatter for Selenium

Filed under: Development, dotnetmag, Testing — Tags: , , — koenwillemse @ 22:20

In my previous post I mentioned that I had been working on a C# formatter for selenium which uses MSTest as test framework and also FluentAssertions to make the tests more readable. After a few hours last Saturday night I had something working which looks pretty ok for now. I guess I’m not the only one wanting to generate this code, so I wanted to share what I’ve got.

I started off by taking the source of the C# formatter which is installed together with the selenium IDE. Note that there is a bug (maybe more, but I ran into this one Winking smile) in the source, which results in a lack of options to set in the IDE. The problem is regarding the configForm setting, which is done in the options. This is pretty strange if you look at it, because that configForm is used to generate the UI for the options (which options are available can be found here). The configForm should be set outside the options. I tried to modify the source, but that’s not possible, it’s read-only. If you still want to use the NUnit formatting as done in the formatter, you can add another formatter and add the modified source. You can download the modified source here.

Now to the formatter I wrote. I wanted a few things:

  • MSTest as test framework
  • use FluentAssertions for readability
  • Start the selenium server when starting test run (and close it when tests finished)

There is a description here on how to start creating a custom formatter. However, I was lazy, so I copied the source from the existing C# formatter and modified it to accommodate my needs. First of all I fixed the problem with the options, which I described above.
Next I changed the assertion methods to use the FluentAssertions syntax.
I updated the generated header and footer, since I don’t want all the initialization of a Selenium instance etc. to be done on every TestInitialize since it takes several seconds. I changed it to be done on a central location.

The biggest change I made was add formatting for a test suite, which is not available in the default C# formatter. In the code for the test suite, I added methods for AssemblyInitialize and AssemblyCleanup. In these method, I will start and tear down the selenium server process and the selenium instance. This uses a static selenium instance, so this will cause problems if you want to use it in a multi-threaded environment, but for now, it matches my needs. The complete source of the formatter can be downloaded here.

I hope some of you find this helpful also. If you have some good suggestions for the formatter, then please let me know.

Edit 22-02-2011: The links to the sources were incorrect. I updated the links now, so you can download the files from my skydrive.

About these ads

7 Comments »

  1. […] Custom C# formatter for Selenium details a bit of the journey and links to a formatter that uses Fluent Assertions. […]

    Pingback by A Smattering of Selenium #42 « Official Selenium Blog — February 21, 2011 @ 13:51

    • Thibaud, thanks for pointing me at my mistake. I took the wrong url’s to my skydrive. Links are updated now and you can download the source.

      Comment by koenwillemse — February 22, 2011 @ 12:09

  2. great but how do I install the plugin ?

    Comment by Joe — September 20, 2012 @ 14:39

    • Do you mean selenium? It can be downloaded here: http://seleniumhq.org/download/

      Comment by koenwillemse — September 24, 2012 @ 10:29

      • no I mean how I install your formatter in selenium. The docs of selenium are confusing as to what is a plugin and what not. There are obviously different ways to customize.

        Thanks for your help

        Joe

        Comment by Joe — September 24, 2012 @ 16:11

  3. ok I got the script now added to the IDE. But where is the test suite ? I added my own with a static class, but the usual C# examples use the IWebDriver interface, so where are your methods coming from ? (I have driver.Navigate().GoToUrl.. instead of selenium.Open

    Comment by Joe — October 30, 2012 @ 15:27


RSS feed for comments on this post. TrackBack URI

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

The Silver is the New Black Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: