Beispiel #1
0
        private static IWbTstr CreateInstance()
        {
            WbTstr wbTstr = new WbTstr(Guid.NewGuid());

            bool?enableDebug = ConfigReader.GetSettingAsBoolean("EnableDebug");

            if (enableDebug.HasValue && enableDebug.Value)
            {
                wbTstr.EnableDebug();
            }

            bool?enableDryRun = ConfigReader.GetSettingAsBoolean("EnableDryRun");

            if (enableDryRun.HasValue && enableDryRun.Value)
            {
                wbTstr.EnableDryRun();
            }

            string useWebDriver = ConfigReader.GetSetting("UseWebDriver");

            if (!String.IsNullOrWhiteSpace(useWebDriver))
            {
                SeleniumWebDriver.Browser browser;
                if (Enum.TryParse(useWebDriver, true, out browser))
                {
                    wbTstr.UseWebDriver(browser);
                }
            }

            string buildKey = ConfigReader.GetSetting("BuildResultKey");

            if (!String.IsNullOrWhiteSpace(buildKey))
            {
                wbTstr.SetBrowserStackBuildIdentifier(buildKey);
            }

            bool?useBrowserStack = ConfigReader.GetSettingAsBoolean("UseBrowserStack");

            if (useBrowserStack.HasValue && useBrowserStack.Value)
            {
                wbTstr.UseBrowserStackAsRemoteDriver();
            }

            bool?enableBrowserStackLocal = ConfigReader.GetSettingAsBoolean("EnableBrowserStackLocal");

            if (enableBrowserStackLocal.HasValue && enableBrowserStackLocal.Value)
            {
                wbTstr.EnableBrowserStackLocal();
            }

            string browserStackProject = ConfigReader.GetSetting("BrowserStackProject");

            if (!string.IsNullOrEmpty(browserStackProject))
            {
                wbTstr.EnableBrowserStackProjectGrouping(browserStackProject);
            }

            return(wbTstr);
        }
Beispiel #2
0
        private IWebDriver WebDriverFactoryMethod(Func <IWebDriver> webDriverFactory, IWebDriver reCreatedWebDriver = null)
        {
            try
            {
                var policy = Policy.Handle <InvalidOperationException>().WaitAndRetry(4, i => TimeSpan.FromSeconds(30));
                return(policy.Execute(
                           () =>
                {
                    var webDriver = reCreatedWebDriver ?? webDriverFactory();
                    if (!FluentTest.IsMultiBrowserTest && FluentTest.ProviderInstance == null)
                    {
                        FluentTest.ProviderInstance = webDriver;
                    }

                    webDriver.Manage().Cookies.DeleteAllCookies();
                    webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

                    // If an alert is open, the world ends if we touch the size property. Ignore this and let it get set by the next command chain
                    try
                    {
                        if (this.Settings.WindowMaximized)
                        {
                            // store window size back before maximizing so we can 'undo' this action if necessary
                            var windowSize = webDriver.Manage().Window.Size;
                            if (!this.Settings.WindowWidth.HasValue)
                            {
                                this.Settings.WindowWidth = windowSize.Width;
                            }

                            if (!this.Settings.WindowHeight.HasValue)
                            {
                                this.Settings.WindowHeight = windowSize.Height;
                            }

                            webDriver.Manage().Window.Maximize();
                        }
                        else if (this.Settings.WindowHeight.HasValue && this.Settings.WindowWidth.HasValue)
                        {
                            webDriver.Manage().Window.Size = new Size(this.Settings.WindowWidth.Value, this.Settings.WindowHeight.Value);
                        }
                        else
                        {
                            var windowSize = webDriver.Manage().Window.Size;
                            this.Settings.WindowHeight = windowSize.Height;
                            this.Settings.WindowWidth = windowSize.Width;
                        }
                    }
                    catch (UnhandledAlertException e)
                    {
                        // TODO: handle error
                    }

                    _mainWindowHandle = webDriver.CurrentWindowHandle;
                    return webDriver;
                }));
            }
            catch (InvalidOperationException e)
            {
                if (reCreatedWebDriver != null)
                {
                    Console.WriteLine("Created a new remote WebDriver, but it still doens't seems to work.");
                    throw;
                }

                // Oke, we're going to create a new remote WebDriver (including a new session)
                // But first we try to terminate the current one.
                Dispose();

                IWebDriver recreated = null;
                try
                {
                    // Get the remote WebDriver configuration
                    WbTstr wbTstr = WbTstr.Configure() as WbTstr;
                    if (wbTstr != null)
                    {
                        var capabilities    = new DesiredCapabilities(wbTstr.Capabilities);
                        var remoteDriverUri = wbTstr.RemoteDriverUri;

                        // Create a new remote WebDriver
                        var policy = Policy.Handle <Exception>().WaitAndRetry(4, i => TimeSpan.FromSeconds(30));
                        recreated = policy.Execute(() => new EnhancedRemoteWebDriver(remoteDriverUri, capabilities, TimeSpan.FromSeconds(60)));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to create new remote WebDriver instance.");
                    throw;
                }

                return(WebDriverFactoryMethod(webDriverFactory, recreated));
            }
        }
Beispiel #3
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());
                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.");
        }