Design Selenium Framework with C# – Part 4

In this part lets look at creating the page objects in most generic way.

As discussed in the last post the selenium support of page object initiation is getting deprecated from next releases of the selenium. So lets create page objects in most generic format.

HomePage

In the above class, we are declaring the all const string or identification strings may be xpath or ids or css selectors as const strings.

And we are assuming that driver object is being sent to the class. We are creating class property as driver and assigning the sent driver while creating the page class object from test.  So the constructor of the page class takes one parameter driver, also if you look at it it take parameter type IWebDriver that means, any type of driver which implements IWebDriver is good enough, this will make class independent of the driver type.

HomePage2

In the above code we have created the web elements as properties of the class

And the actions as methods. We can create the elements in the methods as well. But to make differentiation between actions and elements, its good practice to keep the elements as properties.

I am not creating utility class as of now to keep the common methods in there, also there is no BasePage which has global objects of the application. We can very well do that, but for the simplicity , I have not added additional classes which makes it bit complex to understand the execution flow.

Similar to the above the class, we can create search results page and validation of the search results page in another class called GoogleSearchResults.

SearchResultsPage

And verification method for verifying the returned search result.

SearchResults2

Now that we have every thing ready we are good to create NUnit test and understand the execution flow. I will discuss it in next section.

Design Selenium Framework with C# – Part 5

 

 

Advertisement

Design Selenium Framework with C# – Part 1

In the current Selenium space there are lot of developments are happening. One of the most asked questions in the internet is how to design extensible and most easy frame work that can be suited for different kinds of testing platforms such as desktop, mobile.

Selenium with C# is not that common in the current freeware market era. But its worth knowing about how it can be implemented so that we can future ready. C# is now becoming language for building all kinds of apps including mobile. Having knowledge on C# selenium will bring us more opportunities for a devops engineer.

With that preface, I will try to create a logical flow of learning. Lets look at following areas one by one to achieve our goal of learning.

I typically go with bare minimum requirements to create the framework assuming readers know the basics of any object oriented programming language. So I am starting with concepts familiar but implemented little different in C#.

  • Basics of C# interfaces and setters and getters.
  • Basics of Interface design pattern for framework creation.
  • MS visual studio nuget packages required and drivers required for selenium
  • Driver factory or thread safe driver creation
  • NUnit testing framework bare minimum to Create basic google search test with Nunit

In this blog lets start with first section.

C# provides the important feature of setting and getting the class properties. There are auto properties which are really useful and takes care of the coding.

Setters sets the properties and getter will get you the properties. Following is the example of setters and getters. This is widely used and need to be understood while working with C#

public class test
{
private string _anyproperty;
public string AnyProperty
{
get{return _anyproperty;}
set{ _anyproperty = value;}
}
}

In the above example we can declare private variable and use it to assign and return. This is same as setting auto properties like below in C#.

public class test
{
public string AnyProperty
{
get;set;
}
}

Class properties can be set inside class or out side the class using the class object.
The properties are quite useful in designing the framework. Usually anything we want to set for the entire class such as file path for the excel operations class, file path for the browser driver class etc, this same path can be used in the class methods.

In the next section we will see how interfaces can be used to create portable and readable code.

Interface is generally known as contract agreement. It signs the contract with implementing classes. I.E it enforces the method definitions for implementing class.

The underlying code can differ with different class implementations.

In view of our selenium, let’s take the example of Browser driver, the IWebDriver interface holds the method definitions and all driver classes such as ChromeDriver, FireFoxDriver , IEDriver classes implements the methods in IWebDriver interface.

To understand how we can make use of interfaces to create clean code while designing the C# frame work, lets look at browser object creation.

In C# selenium browser object can be initialized similar to Java. To initialize browser object we need two objects

  • Browser Service
  • Browser Options or Capabilities.
    • IWebDriver ChromeDriver = new ChromeDriver ((ChromeDriverService)service, (ChromeOpions)capabilities, timeout)
    • IWebDriver FireFoxDriver = new FireFox ((FireFoxDriverService)service, (FireFoxOpions)capabilities, timeout)
    • IWebDriver InternetExplorerDriver = new InternetExplorerDriver((InternetExplorerDriverService)service, (InternetExplorerOptions)options, timeout)

If we look at all three initialization, in fact all driver classes implementing IWebDriver have these three parameters two of which are class objects.

It makes sense for us to utilize the interface concept here and create IDriver interface which have two methods (Service object creation and capabilities/option object creation)

This will enforce for all types of driver objects to implement these methods.

Lets end the first part here, and in the next part, will dive deep and create framework using MS visual studio.

Design Selenium Framework with C# – Part 2