selenium

WebDriver Factory

WebDriver Factory C#

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;


/// <summary>
/// Factory for creating WebDriver for various browsers.
/// </summary>
public static class WebDriverFactory
{
    /// <summary>
    /// Initilizes IWebDriver base on the given WebBrowser name.
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static IWebDriver CreateWebDriver(WebBrowser name)
    {
        switch (name)
        {
            case WebBrowser.Firefox:
                return new FirefoxDriver();
            case WebBrowser.IE:
            case WebBrowser.InternetExplorer:
                InternetExplorerOptions ieOption = new InternetExplorerOptions();
                ieOption.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
                ieOption.EnsureCleanSession = true;
                ieOption.RequireWindowFocus = true;
                return new InternetExplorerDriver(@"./", ieOption);
            case "safari":
                return new RemoteWebDriver(new Uri("https://mac-ip-address:the-opened-port"), DesiredCapabilities.Safari());
            case WebBrowser.Chrome:
            default:
                ChromeOptions chromeOption = new ChromeOptions();
                string location = @"./";
                chromeOption.AddArguments("--disable-extensions");
                return new ChromeDriver(location, chromeOption);
        }
    }
}

public enum WebBrowser
{
    IE,
    InternetExplorer,
    Firefox,
    Chrome
}


// Usage
var driver = WebDriverFactory.CreateWebDriver(WebBrowser.Chrome);

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow