Generic testcases for selenium 1 The extension methods

Written by Adit Shah on September 07, 2016

Generic testcases for selenium 1 The extension methods_image

Selenium is a great tool to automate UI testing but the code it generates is difficult to read and definitely not easy to maintain. The conclusion was that it was just a good code sample. Moreover, there was a huge scope of not writing code for many of the routine checks like javascript validation of data entered by user and messages.

Most selenium test cases would constitute of filling up form fields , click a button and check for error messages or their absence. Since selenium depends on strings selectors for identifying and manipulating controls, the idea was to have selectors for all sorts of form fields and populate them with random values. This would constitute the laziest testing technique but effective for checking for 500 status code or page crashes caused by not handling empty fields, not checking for data types and lengths. Taking screenshots will help notice layout issues quickly as well.

But before we start, we need to add a couple of methods to make our life easier. Selenium has a ready method GetAllFields() for getting all form inputs but it skips the select and textarea for some reason.Moreover, it does not help us to get specific controls like checkboxes to manipulate.For example checkbox can be checked using Check() and Write() is used to add text to the textboxes. This complicates the code a bit more.
To make it easier and more natural to code, we added extension methods to get each type of control.

private static string[] GetControls(ISelenium selenium, string strXPathLocator){
int cntControl = Convert.ToInt32(selenium.GetXpathCount (strXPathLocator));
string[] strControls = new string[cntControl];

     if (cntControl > 0){
for (int i = 1; i <= cntControl; i++){
strControls[i – 1] = selenium.GetAttribute(strXPathLocator + “[“ + i + “]@id”);

      return strControls;


The above method uses selenium’s general method of getting elements using XPath. It takes an instance of selenium and the XPath expression as parameter. It is private so that public methods can pass the necessary expression and allowing the test case writer to forget about Xpath. It is also in line with the GetAllFields() method provided by selenium. Unfortunately, there is no other way to get the string array of control ids yet.

Next we add a method to get specific types of inputs/controls.

public static string[] GetAllCheckBoxes(this ISelenium selenium){

  return GetControls(selenium, “//input[@type=’checkbox’]”);           


The above method is specific for the checkboxes. There will be similar ones for radiobuttons, textarea and password fields. There are a few more extensions that were added for the sake of convenience. You can download SeleniumExtender.cs for all of them.

With these extensions, we are ready to go to the next step of actually writing our testcase. This is the subject of the next post.

Kaira Software Pvt. Ltd. Is One Of The Leading Enterprise Web & Mobile Apps Development Company In India With An Industry Experience Of More Than A Decade.


Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha *