Thursday, 19 March 2009

Parameterized Selenium Tests in NUnit

Selenium is pretty flexible with regards to what browser on what operating system you want to run your tests. The drawback to this is duplication of each test for each environment or defining the environments in the test and duplicating each call to the selenium object. To me neither of these options is very flexible, each time I add a new environment I have to duplicate even more test code and eventually maintaining these tests would become too much.

One solution to this problem was to create dynamic test fixtures that allow the test objects to be parameterized. This can be done with the NUnit addin called Rakija. Unfortunately this addin doesn't work with the current version of NUnit and had to be ported to the newest version (source code for this is available on request). Rakija allows the user to take a list of instantiated classes that contain tests and use these as test fixtures, this means that constructors other than the default can be used to pass more information to the test. Of course this list can be generated at runtime based on a configuration file defining the environments the tests will exist in and some reflection to find the available tests. This way when new tests are added they can automatically become available to NUnit for every defined environement.

To implement a basic version of this solution you must first of all create your Selenium test class which should implement the IDynamicFixture interface from Rakija, this interface only requires that the get method of the name property be implemented. This can be used to return a unique name for the dynamic test fixture. The constructor of this class can be used to setup any member variables with more information such as the browser/environment the test will run on. Normal selenium tests can then be added to this class as well as setup and teardown methods to handle creation of the selenium object.

Next create a class which implements the IDynamicFixtureSpecifier interface from Rakija. This interface has two methods, the first is FixtureType which should return the type of the test class and the second GetUserFixtures returns a list of test objects. The test objects can then take different values for the browser/environment as they are instantiated and added to the list.

Having done all this and installed Rakija to the NUnit addins directory you can now build the test assembly and load it into NUnit. The tests will automatically be picked up and be available for each environment. I'll try to update this post at some point over weekend with some sample source code.

This solution works well with Selnium Grid and parallelizing Selenium tests which I plan to post about in the near future.

No comments:

Post a Comment