After learning the basics of the interface design pattern, lets look at actual implementation in step by step manner.
Lets first discuss about how to set up the required nuget packages for the selenium.
Open visual studio and create a unit test project by selecting the project type unit testing.
Once the project is created, lets install the required Nuget packages. This Nuget package is similar to maven repository in Java. It contains all binaries required for the purpose.
We will download and map to the current project just enough to create some simple selenium tests.
The Nuget packages can be downloaded from different sources, lets download packages from default location https://www.nuget.org/
We need following packages for the purpose of creating the selenium tests.
- Selenium 3.14 (or higher) (https://www.nuget.org/packages/Selenium.WebDriver/)
- Selenium support(https://www.nuget.org/packages/Selenium.Support/)
- NUnit 3.0(https://www.nuget.org/packages/NUnit/)
Once we download and attach it to the project we need to do download another set of install-able files meaning that driver exes to steer the web browsers.
- geckodriver (firefox dirver)
Now our project setup is ready for creating the Framework and implementing the simple selenium tests.
Before we dive into creating classes, lets create folder structure so that we can partition our classes into different directories like below. Will discuss one by one while we go through each of the items. Lets create following folders in the solution.
- This will contain the driver executable files we have downloaded
- Contract definitions for creating the WebDrivers
- Actual web driver classes implementing the IDrivers interface.
As we mentioned in the part one, let us first create the IDrivers interface to make browser object as described below.
In the above Interface, we are defining the objects required fro web driver creation and also using the ‘Driver’ property we are setting and getting the driver back from each implementation.
In this step lets create the configuration class which defines the driver executable locations, timeouts and other browser configuration details we have just defined in the interface.
Lets look at each of the web driver class, how it is done. I have kept all the code in git repository for reference.
Lets create filepath property to set and get the file path. Also see how we can set the driver services and desired capabilities/options as class properties.
After creating these properties, next step is to initialize the driver with these options
If you look at below code the folder path is set by config driver exe folder whereas DesiredServices and DesiredCapabilites/Options properties in config class are set by chrome driver class.
I will try to explain the logical flow once we complete the entire post.
As of now the summary is, all driver classes (ChromeDriver, FireFox driver class, and IEdriver class) have all the three methods.
The link to the whole repository from git is given below for reference.
It will be much more straight forward if you already well versed with the basic idea of properties, methods, and oops concepts.
Now lets discuss about how to create these browser instances as separate threads so that we can run tests in parallel in next part.