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.
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.
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.
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
One thought on “Design Selenium Framework with C# – Part 3”
[…] Design Selenium Framework with C# – Part 3 […]