February 9, 2022

How to use WebDriver manager for efficient management of browser drivers ( Selenium + .Net )

  February 9, 2022

Managing WebDrivers in sync with browser versions is always a problem for an automation tester. 

If we can't control the auto-update of the browser, then the problem becomes worse.

So what if there is a one-point solution for this, in which the automation scripts themselves will download the latest version of the driver for all the browsers.

The solution is WebDriver Manager Nuget in Visual Studio.

Using this we can decide the version of the driver to be downloaded or we can set it to automatically download the latest version on the execution of the script.

When using web driver manager, we don't need to specify the path of the driver. Since it is automatically downloaded to the bin folder.

A complete browser control method using WebDriver Manager is given below.

public static WebDriver _driver;
private readonly IObjectContainer container;

public void DriverManager(string browserValue)
{
	//Checking browser name
	string browserValue = Settings.BrowserType.ToString().ToLower();

		switch (browserValue)
		{
			case "firefox":

			new WebDriverManager.DriverManager().SetUpDriver(new FirefoxConfig());
			FirefoxOptions FirefxOptions = new FirefoxOptions();

				if (browserValue.Contains("headless"))
				{
					FirefxOptions.AddArguments("--headless");
					FirefxOptions.AddArguments("disable-gpu");
					FirefxOptions.AddArguments("window-size=1920,1080");
				}


			_driver = new FirefoxDriver(FirefxOptions);

				break;

			case "ie":
			//Ignore Protected mode settings in IE                               
			new WebDriverManager.DriverManager().SetUpDriver(new InternetExplorerConfig());
			var IEoptions = new InternetExplorerOptions
				{
					IntroduceInstabilityByIgnoringProtectedModeSettings = true,
					// ForceCreateProcessApi = true,
					EnableNativeEvents = true,
					RequireWindowFocus = true,
					IgnoreZoomLevel = true
				};
				_driver = new InternetExplorerDriver(IEoptions);

				break;

			case "chrome":

				new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());
				ChromeOptions ChrOptions = new ChromeOptions();

				var loggedInUser = Environment.UserName;
				ChrOptions.AddAdditionalOption("useAutomationExtension", false);

				if (browserValue.Contains("headless"))
					{
						ChrOptions.AddArgument("--headless");
						ChrOptions.AddArguments("disable-gpu");
						ChrOptions.AddArguments("window-size=1920,1080");
					}

			_driver = new ChromeDriver (ChrOptions);
			_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);

			break;

			case "edge":
			
				new WebDriverManager.DriverManager().SetUpDriver(new EdgeConfig());
				EdgeOptions Edgoptions = new EdgeOptions();
				Edgoptions.AcceptInsecureCertificates = true;
				_driver = new EdgeDriver(Edgoptions);

				break;

			default:

				Console.Out.WriteLine("Wrong browser type Entered : " + Settings.BrowserType + ", Please select from Chrome/Chrome+headless/Firefox/Firefox+headless/IE");

				break;

		}
		Console.Out.WriteLine("Launched Browser " + Settings.BrowserType);
	
	_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
	_driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15);
	_driver.Manage().Window.Maximize();
	container.RegisterInstanceAs<IWebDriver>(_driver);
}

This method can be customized to handle any browser with any version from our test scripts.

We can use the below code if we want to specify the version of the driver to download.
new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig(), "97.0.4692.99");
ChromeOptions ChrOptions = new ChromeOptions();
logoblog

Thanks for reading How to use WebDriver manager for efficient management of browser drivers ( Selenium + .Net )

Previous
« Prev Post

No comments:

Post a Comment

How to send email from current Outlook instance using Java + VBScript

If we are using .Net, we can easily access outlook and send emails using its own libraries. But what if we are using Java? Here we can seek ...