In our previous articles, I have explained the execution on Selenium C# scripts on different browsers like Chrome, Firefox and Internet Explorer using their driver exe files. Find these articles below:
- Adding Chrome Driver to execute Selenium C# scripts on Chrome Browser
- Adding Gecko Driver to execute Selenium C# scripts on Firefox Browser
- Adding Internet Explorer Driver to execute Selenium C# scripts on Internet Explorer Browser
In the above three articles, we had to follow a very hectic procedure for downloading the driver exe files and then configuring them in our Selenium Projects.
To avoid the hectic procedure by using the driver exe files for different browsers, we can go with a simple solution known as WebDriverManager.
In this article, I am going to explain the steps for configuring the WebDriverManager for avoiding the hectic procedure in driver exe files.
Let’s get started.
Selenium C# – WebDriverManager Configuration
The following are the steps for configuring the WebDriverManager:
1) Launch Visual Studio, open the Project in which Selenium is already installed (Refer previous article) as shown below:
2) Select ‘Tools’ > ‘NuGet Package Manager’ > ‘Manage NuGet Packages for Solution’ as shown below:
3) Type ‘WebDriverManager’ in the Search box field of Browser tab, Press ‘Enter’ key on your keyboard to search, click on the ‘WebDriverManager’ option displayed in the search results, select Project check box option and click on ‘Install’ button as shown below:
4) Click on the ‘OK’ button on the displayed ‘Preview Changes’ dialog as shown below:
5) Observe that WebDriverManager will be installed and then switch to program.cs file as shown below:
6) As WebDriverManager is now installed in our project, we don’t have to download any exe file for each browser driver. And we don’t have to worry about the browser driver .exe files compatibility with the browser versions. i.e. WebDriverManager automatically takes care of the browser versions and download the .exe accordingly during the run time.
We can simply add the below line of WebDriverManager code, before creating an object for Chrome Driver.
new WebDrivereManager.DriverManager().SetUpDriver(new ChromeConfig());
i.e. We will have the below code for launching a blank chrome browser using the automation script:
Lets resolve the error displayed in the above screenshot using the below step.
7) Hover the mouse on the ChromeConfig and select the option to import ‘using WebDriverManager.DriverConfigs.Impl;’ as shown below:
8) Now create an object for ChromeDriver class, import ChromeDriver as explained in previous posts and then Save & click on ‘Start’ button to execute the Automation script as shown below:
9) Observe that the above automation script got executed without the need for downloading .exe driver file and a blank chrome browser got launched by the script as expected with the help of WebDriverManager as shown below:
10) In the similar way we can modify the code as shown below to execute the automation script on different browser like Firefox and IE as shown below:
Firefox Browser code:
new WebDriverManager.DriverManager().SetUpDriver(new FirefoxConfig());
FirefoxDriver driver = new FirefoxDriver();
IE Browser Code:
new WebDriverManager.DriverManager().SetUpDriver(new InternetExplorerConfig());
InternetExplorerDriver driver = new InternetExplorerDriver();
Hence using WebDriverManager configuration, we can avoid downloading & configuring individual driver exe file for different browsers.
In the next article I will explain the Browser factory program to demonstrate the importance of using IWebDriver interface in the Selenium C# automation scripts.
- > To learn more about Selenium C#, continue to the next post (Click on Next Post Link Below)
- > Check complete Selenium C# Tutorial Contents here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning 🙂
On a mission to contribute to the Software Testing Community in all possible ways.