示例#1
0
        private static Func <IWebDriver> GenerateBrowserSpecificDriver(Browser browser, TimeSpan commandTimeout)
        {
            string driverPath = string.Empty;

            switch (browser)
            {
            case Browser.InternetExplorer:
                driverPath = EmbeddedResources.UnpackFromAssembly("IEDriverServer32.exe", "IEDriverServer.exe", Assembly.GetAssembly(typeof(SeleniumWebDriver)));
                return(new Func <IWebDriver>(() => new Wrappers.IEDriverWrapper(Path.GetDirectoryName(driverPath), commandTimeout)));

            case Browser.InternetExplorer64:
                driverPath = EmbeddedResources.UnpackFromAssembly("IEDriverServer64.exe", "IEDriverServer.exe", Assembly.GetAssembly(typeof(SeleniumWebDriver)));
                return(new Func <IWebDriver>(() => new Wrappers.IEDriverWrapper(Path.GetDirectoryName(driverPath), commandTimeout)));

            case Browser.Firefox:
                return(new Func <IWebDriver>(() => {
                    var firefoxBinary = new OpenQA.Selenium.Firefox.FirefoxBinary();
                    return new OpenQA.Selenium.Firefox.FirefoxDriver(firefoxBinary, new OpenQA.Selenium.Firefox.FirefoxProfile
                    {
                        EnableNativeEvents = false,
                        AcceptUntrustedCertificates = true,
                        AlwaysLoadNoFocusLibrary = true
                    }, commandTimeout);
                }));

            case Browser.Chrome:
                //Providing an unique name for the chromedriver makes it possible to run multiple instances
                var uniqueName       = string.Format("chromedriver_{0}.exe", Guid.NewGuid());
                var chromeDriverPath = ConfigReader.GetSetting("ChromeDriverPath");
                if (!string.IsNullOrEmpty(chromeDriverPath))
                {
                    driverPath = chromeDriverPath;
                    uniqueName = "chromedriver.exe";
                }
                else
                {
                    driverPath = EmbeddedResources.UnpackFromAssembly("chromedriver.exe", uniqueName,
                                                                      Assembly.GetAssembly(typeof(SeleniumWebDriver)));
                }

                var chromeService = ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(driverPath),
                                                                             uniqueName);

                chromeService.SuppressInitialDiagnosticInformation = true;
                var chromeOptions = new ChromeOptions();
                chromeOptions.AddArgument("--log-level=3");

                return(new Func <IWebDriver>(() => new OpenQA.Selenium.Chrome.ChromeDriver(chromeService, chromeOptions, commandTimeout)));

            case Browser.PhantomJs:
                var uniqueNamePhantom = string.Format("phantomjs_{0}.exe", Guid.NewGuid());
                driverPath = EmbeddedResources.UnpackFromAssembly("phantomjs.exe", uniqueNamePhantom, Assembly.GetAssembly(typeof(SeleniumWebDriver)));
                var phantomService = PhantomJSDriverService.CreateDefaultService(Path.GetDirectoryName(driverPath), uniqueNamePhantom);

                IWbTstr config    = WbTstr.Configure();
                string  proxyHost = config.GetPhantomProxyHost();
                if (!string.IsNullOrWhiteSpace(proxyHost))
                {
                    phantomService.Proxy     = proxyHost;
                    phantomService.ProxyType = "http";

                    string proxyAuthentication = config.GetPhantomProxyAuthentication();
                    if (!string.IsNullOrWhiteSpace(proxyAuthentication))
                    {
                        phantomService.ProxyAuthentication = proxyAuthentication;
                    }
                }

                return(new Func <IWebDriver>(() => new PhantomJSDriver(phantomService, new PhantomJSOptions(), commandTimeout)));
            }

            throw new NotImplementedException("Selected browser " + browser.ToString() + " is not supported yet.");
        }