Wednesday, January 21, 2015

Selenium WebDriver with C# - Create Helper Classes (1)

Introduction
In this article we will transcribe the java code into c# for the following links:
The following are used:
- C# ans language
- Visual Studio 2012 as IDE
- Nunit for tests
- Hudson for continuous integration

DriverUtils
First of all we can create a DriverUtils class that will contain functions for starting the web driver, getting the browser, opening the desired url, closing browser, login, logout, functions to be executed before and after all tests are run or after all test from a class are run. Also, we can include here a function that will log, at the beginning of each test, a name and a description.
Let's start!

Start WebDriver
  • Browser is an enum

  • _driver is declared globally
  • _driver is set and get
  • For working with Chrome and IE drivers we need to download the .exe drivers and include them in the project. 
  • Capabilities: describes a series of key/value pairs that encapsulate aspects of a browser. You can find more information about this on the internet
Return Browser

Open url
In order to open your url you can create a function

where the url is define in a ConstantsHelpers class

Close browser
Wait time 
This is a function used to implicitly wait the specified amount of time in milliseconds
You can use it like this:
wait_time(5000);

[SetUp][TearDown]
Depending on your project, you can set some actions to be taken:
- before starting all tests SetUp
- after running all tests TearDown

Here are some examples:

Login, Logout
Supposing you web page has a login page, you can include the Login, Logout function in SetUp and TearDown functions.
Below you have a login, logout examples:
Test Case Description
Another function that we added in this class is the one that, in the test logs, at the beginning of each test, writes a brief description about the test to be run.

Hope you enjoyed this article. Next week we will present more functions translated from java to c#.

Happy testing and make it green becomes a dream!

2 comments:

  1. Hi Raluca, I've ran into your blog searching info about Selenium WebDriver - C# combination (most of the resources found on the net are Java-related).

    I am a beginner in test automation and I try Cucumber -> Specflow -> Selenium Webdriver . If you have experience with such tools, an article would be useful. It is not clear for me how "generic operations" (picking up a certain browser, opening / closing a browser session etc) can be embedded into a Specflow feature file (hooks maybe?)

    Thank you !

    ReplyDelete
  2. Hi DC,

    I haven't used Specflow with Selenium Webdriver in my projects but, coincidentally, today I've just read about it :) and I started to make a Visual Studio project to see how it works. I'm thinking to use it in project that will start very soon, so I'll write an article about how to use it.

    Regards,
    Raluca

    ReplyDelete

Popular Posts