Design Selenium Framework with C# – Part 5

Now since we have created the basic framework and page classes. Lets look at NUnit framework to create tests.

NUnit 3.0 supports parallel testing, and prior to that it supports the testing multiple iterations with different parameters.

Here if you see TestFixture attribute allows the test class to take the constructor parameter. I.E parameters you are defining in the testfixture will be passed to constructor of the test class if matching definition is found. This is very useful when we need to execute same tests with different browsers, we can define BrowserType to achieve the same purpose. The test class will be initiated and tests will be executed for each of those test fixtures.

Also Prallalizable (ParllelScope.Self) will run the test methods in parallel. More information can be found in below link.  Nunit documentation

If you look at the below code, constructor of the test class has browsertype as parameter which is matching with the parameter defined the test fixture. So the test class will run for each browser type.

NUnitTestFixure

And the SetUp attribute is similar to TestSetup in MSTest framework or BeforeTest in TestNG. This will sets up the preconditions for each test.

If you look at the test execution flow

  • Starts at calling DriverFactory.InitDriver(browserType)
  • This will invoke and create corresponding BrowserDriver class object
  • Driver property will be set in the DriverFactory Class with the type of the driver
  • Using the DriverFactory.GetDriver() method we are getting driver to the thread which has initiated it.
  • It will be passed to the Page Classes to do the rest of the test flow.DriverGet

If you look at the test explorer it identifies the tests based on the TestFixture types.

TestExplorer

When you click on run it will run all tests in parallel. You can see the results in the test explorer for each iteration Results

IterationResults

This is the simplest explanation I could give quickly for the Selenium with C# implementation with interface design pattern.

I have not touched any Reporting/Logging part for the framework.

As It can be implemented with ILogger interface with different logging methods so that each logging type (Excel logging, Text, Word/pdf, html) can be implemented easily.

Having said this, it will conclude our discussion on creating the framework.

Please feel free to follow and write to me if you need help in understanding any of these items.

Advertisement

Design Selenium Framework with C# – Part 3

In this section lets look at creating thread safe driver factory and how we can use it in the tests.

As we have seen in part 2, we have created driver classes and each class is having initDriver method which will creates the driver.

Now lets start with an enum with browser types, and in the driverfactory class which we are going create uses the browsertype enum and creates and stores the corresponding driver.

enum

And lets create thread safe object  using ThreadLocal class, thread local class stores the object initiated by the execution thread, so that when we want to use it we can retrieve it. Think of it as a thread id and driver object pair, each driver object has its thread id attached to it and based on the execution thread, ThreadLocal class will provide the corresponding Driver object.

DriverFactory

If you look at the second method, the GetDriver<DriverType> is generic method with DriverType as generic type of the webdriver.

Since we have declared the ThreadLocal type as ‘Object’ instead of standard class, it will not result in any cast errors.

In the init method based in the browser type we can create the browser object.

init

Here if you observe, the DriverStored property is being set in the InitDriver method.

And the DriverStored object is returned using GetDriver<BroweserType> generic method.

Now we have everything ready, and lets create a simple test and look at the where we are going start our driver initiation and how will we set the flow.

For now lets imagine, all page classes contain UI object creation and action methods.

(In the latest selenium 3.14 creating page objects using findsby is getting deprecated, so I am using POM concept but using it as more generic approach rather than using the regular init elements.)

Design Selenium Framework with C# – Part 4