Open mialeska opened 3 years ago
Example of CustomBrowserFactory was in tests for usecases https://github.com/aquality-automation/aquality-selenium-dotnet/blob/master/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/Usecases/CustomBrowserFactoryTests.cs , but could look like that:
using Aquality.Selenium.Browsers;
using Aquality.Selenium.Configurations;
using Aquality.Selenium.Core.Localization;
using Aquality.Selenium.Core.Utilities;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using System;
using System.IO;
using System.Reflection;
namespace Web.Autotests.Framework.BrowserUtilities
{
public class CustomBrowserFactory : BrowserFactory
{
public CustomBrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browserProfile, ITimeoutConfiguration timeoutConfiguration, ILocalizedLogger localizedLogger)
: base(actionRetrier, browserProfile, timeoutConfiguration, localizedLogger)
{
}
protected override RemoteWebDriver Driver
{
get
{
var commandTimeout = TimeoutConfiguration.Command;
var browserName = BrowserProfile.BrowserName;
var driverSettings = BrowserProfile.DriverSettings;
RemoteWebDriver driver;
switch (browserName)
{
case BrowserName.IExplorer:
driver = GetDriver<InternetExplorerDriver>(InternetExplorerDriverService.CreateDefaultService(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location)),
(InternetExplorerOptions)driverSettings.DriverOptions, commandTimeout);
break;
case BrowserName.Chrome:
driver = GetDriver<ChromeDriver>(ChromeDriverService.CreateDefaultService(),
(ChromeOptions)driverSettings.DriverOptions, commandTimeout);
break;
default:
throw new NotSupportedException($"Browser [{browserName}] is not supported.");
}
return driver;
}
}
private RemoteWebDriver GetDriver<T>(DriverService driverService, DriverOptions driverOptions, TimeSpan commandTimeout) where T : RemoteWebDriver
{
return (T)Activator.CreateInstance(typeof(T), driverService, driverOptions, commandTimeout);
}
}
}
Instruction example in case of our SpecFlow-based template: 1) Add CustomBrowserFactory 2) Add respective nuget package of driver e.g. Selenium.WebDriver.ChromeDriver 3) Add hook to be executed before tests: [BeforeTestRun(Order = 0)] public static void ConfigureBrowserFactory() { AqualityServices.BrowserFactory = new CustomBrowserFactory( AqualityServices.Get(),
AqualityServices.Get(),
AqualityServices.Get(),
AqualityServices.LocalizedLogger);
}