Cross Browser Test Automation Framework Using Selenium and C#

Selenium is one of the popular functional test automation tools. Many people use selenium as just record and play back tool but few are aware that selenium can use used along with any of popular programming language. Selenium can be used along with anyone of .Net, Java, Perl, Python, PHP and Ruby. Being a .Net guy, I would give a introduction of how to create a cross browser test automation framework.

To create such a framework you basically need following things:

1. Selenium RC

2. Microsoft Visual studio

3. Any of browsers (IE, Firefox, Safari)

4. Java

Selenium RC is a server which automatically launches and kills browser and acts as HTTP proxy for web requests from them also a client library for various languages mentioned above. Download the Selenium RC in C:\ folder, you can find the client libraries for different languages along with one folder selenium-server-0.9.2 which contains a file called selenium-server.jar. This server behaves as HTTP proxy server and we need java to run this jar file. Here we are not using Nunit but creating a simple console application to show how selenium can be used with C#.

Steps:

  1. Create a C# console application from Microsoft Visual studio.
  2. Add ThoughtWorks.Selenium.Core.dll as a reference from C:\ selenium RC\selenium-remote-control-0.9.2\selenium-dotnet-client-driver-0.9.2\ThoughtWorks.Selenium.Core.dll
  3. Copy paste the code given below:

using System;

using System.Collections.Generic;

using System.Text;

using Selenium;

namespace test

{

class Program

{

static ISelenium Se;

static void Start()

{

Se = new DefaultSelenium("localhost", 4444, "*firefox","http://www.google.com");

Se.Start();

}

static void Stop()

{

Se.Stop();

}

static void Run()

{

Se.Open("/search?hl=en&q=test&btnG=Google+Search&meta=");

}

static void Main(string[] args)

{

Start();

Run();

Stop();

}

}

}

  1. The code has 3 functions, start(): which launches the browser, stop(): which kills the browser and Run() is a sample test case which searches for term test on google.com
  2. To run this program properly you need to have selenium server up and running for which you need to run

java -jar "selenium-server.jar" where selenium-server.jar is file in seleniumRC folder.

6. Now just run the program and see that it searches for test on google.

Now if we want this to be run on say I explorer its simple you just need to change the firefox in above code to iexplore. This way you can write the test script once which can be run on all the 3 popular browsers (IE, Firefox, Safari)

No comments:

Post a Comment