public async Task SetUp()
        {
            this.log = this.CreateLogger();
            var args = new NameValueCollection();

            url = new Uri($"http://localhost:{TcpPort.NextFreePort()}");
            args.Add("selenium.baseUrl", url.ToString());

            browserConfig = WebDriver
                            .Configure(cfg => cfg.WithDefaultOptions().Providers(x => args[x]), log)
                            .Build();

            webHost = WebHost.Start(
                url.ToString(),
                router => router
                .MapGet(
                    "/page1",
                    (req, res, data) =>
            {
                res.ContentType = "text/html";
                return(res.WriteAsync("<body><a href='page2'>Page 2</a></body>"));
            })
                .MapGet(
                    "/page2",
                    (req, res, data) =>
            {
                res.ContentType = "text/html";
                return(res.WriteAsync("<body><label><input type='radio' />Foo</label></body>"));
            }));
            webDriver = WebDriverFactory.Create(browserConfig);
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TSite"></typeparam>
        /// <param name="webDriver"></param>
        /// <param name="baseAddress"></param>
        /// <returns></returns>
        public static TSite UsingSite <TSite>(this IWebDriver webDriver, IWebDriverConfig config, ILogger log) where TSite : IWebSite, new()
        {
            var site = new TSite();

            site.Initialize(webDriver, config, log);
            return(site);
        }
예제 #3
0
 public AuthenticationHelper(IWebDriverConfig config, ILogger log)
 {
     this.config = config;
     this.log    = log;
     authCookies = new CookieContainer();
     initialized = false;
 }
예제 #4
0
        public static IWebDriverConfig GetFromPreset(WebDriverType type, string preset)
        {
            if (type == default(WebDriverType))
            {
                throw new ArgumentException(nameof(type));
            }
            if (preset == null)
            {
                throw new ArgumentNullException(nameof(preset));
            }

            if (string.IsNullOrWhiteSpace(preset))
            {
                throw new ArgumentException(nameof(preset));
            }

            IWebDriverConfig webDriverConfig = null;

            switch (type)
            {
            case WebDriverType.Chrome:
                webDriverConfig = GetPresetChromeWebDriverConfig(preset);
                break;
            }

            return(webDriverConfig);
        }
예제 #5
0
        private void AuthenticatedInitialize(IWebDriverConfig config, IWebDriver driver, IAuthenticationHelper authHelper)
        {
            void ApplyCookiesToWebDriverAndNavigate()
            {
                var cc = authHelper.AuthCookies;

                driver.Manage().Cookies.DeleteAllCookies();
                cc.GetCookies(config.BaseUrl)
                .Cast <System.Net.Cookie>()
                .Select(
                    c =>
                {
                    if (c.Domain.Contains("localhost"))
                    {
                        c.Domain = null;
                    }
                    return(c);
                })
                .ToList()
                .ForEach(
                    cookie => driver.Manage()
                    .Cookies.AddCookie(new Cookie(cookie.Name, cookie.Value, cookie.Domain, cookie.Path, null)));

                driver.Navigate().GoToUrl(config.BaseUrl);
            }

            driver.GoToRelativeUrl(config.BaseUrl + config.RelativeMicroceliumLogoPath);
            ApplyCookiesToWebDriverAndNavigate();
            driver.WaitForElementToBeVisible(By.LinkText("Logout"));
            driver.DefinitivelyWaitForAnyAjax(log, config.PageLoadTimeout);
        }
예제 #6
0
        private static IWebDriver CreateChromeWebDriver(IWebDriverConfig config)
        {
            var chromeConfig = config as ChromeWebDriverConfig;
            var options      = chromeConfig.AsChromeOptions();
            var service      = ChromeDriverService.CreateDefaultService();
            var webDriver    = new ChromeDriver(service, options);

            return(webDriver);
        }
예제 #7
0
 void IWebSite.Initialize(IWebDriver d, IWebDriverConfig c, ILogger l)
 {
     if (driver != null)
     {
         throw new InvalidOperationException("Site has already been initialized");
     }
     driver      = d;
     config      = c;
     log         = l;
     baseAddress = c.BaseUrl;
 }
예제 #8
0
        /// <summary>
        /// Initializes a Selenium WebDriver in a Lazy and Thread-safe way
        /// </summary>
        /// <param name="browserConfig"></param>
        /// <param name="initializeBrowser"></param>
        /// <returns></returns>
        public static (IWebDriver Driver, ExceptionDispatchInfo InitializationException) CreateAndInitialize(
            IWebDriverConfig browserConfig,
            Action <IWebDriverConfig, IWebDriver> initializeBrowser)
        {
            var driver = Create(browserConfig);
            ExceptionDispatchInfo ie = null;

            try { initializeBrowser(browserConfig, driver); }
            catch (Exception e) { ie = ExceptionDispatchInfo.Capture(e);; }
            return(driver, ie);
        }
예제 #9
0
        public void CreateFromConfig_NullConfig_ThrowsArgumentNullException()
        {
            // Arrange
            IWebDriverConfig config = null;

            // Act
            TestDelegate action = () => WebDriverFactory.CreateFromConfig(config);

            // Assert
            Assert.Throws <ArgumentNullException>(action);
        }
예제 #10
0
        public void SetUp()
        {
            var log  = this.CreateLogger();
            var args = new NameValueCollection();

            fakeWebDriver = Substitute.For <IWebDriver>();
            args.Add("selenium.baseUrl", $"http://localhost:{TcpPort.NextFreePort()}");
            args.Add("webdriver.browser.type", "fake-webdriver");
            fakeConfig = WebDriver.Configure(cfg => cfg.WithDefaultOptions().Providers(x => args[x]), log).Build();
            WebDriverFactory.AddDriverBuilder("fake-webdriver", c => fakeWebDriver);
        }
        public void Initialize_NullWebDriverConfig_ThrowsArgumentNullException()
        {
            // Arrange
            IWebDriverConfig webDriverConfig = null;
            var tracker = Substitute.For <ISessionTracker>();

            // Act
            TestDelegate action = () => _defaultPerformer.Initialize(webDriverConfig, tracker);

            // Assert
            Assert.Throws <ArgumentNullException>(action);
        }
예제 #12
0
        /// <summary>
        /// Initializes the page
        /// </summary>
        /// <param name="driver"></param>
        /// <param name="config"></param>
        public virtual void Initialize(IWebDriver driver, IWebDriverConfig config, ILogger log)
        {
            if (Driver != null)
            {
                throw new InvalidOperationException("Page has already been initialized");
            }

            this.log = log;
            Config   = config;
            Driver   = driver;
            Driver.DefinitivelyWaitForAnyAjax(log, PageTimeout);
        }
예제 #13
0
        /// <summary>
        /// Creates a web driver
        /// </summary>
        /// <param name="browserConfig"></param>
        /// <returns></returns>
        public static IWebDriver Create(IWebDriverConfig browserConfig)
        {
            if (browserConfig.BrowserType == null || !DriverBuilders.ContainsKey(browserConfig.BrowserType))
            {
                throw new NotImplementedException($"No browser configured for type '{browserConfig.BrowserType}'");
            }

            var builder   = DriverBuilders[browserConfig.BrowserType];
            var webDriver = builder(browserConfig);

            webDriver.Manage().Timeouts().ImplicitWait = browserConfig.ImplicitTimeout;
            webDriver.Manage().Timeouts().PageLoad     = browserConfig.PageLoadTimeout;

            return(webDriver);
        }
예제 #14
0
        protected WbTstrFixtureBase()
        {
            if (Attribute.GetCustomAttribute(GetType(), typeof(WebDriverConfigAttribute)) is WebDriverConfigAttribute attribute)
            {
                _webDriverScope = attribute.Scope;

                bool usePreset = !string.IsNullOrEmpty(attribute.Preset);
                _webDriverConfig = usePreset ? WebDriverConfigs.GetFromPreset(attribute.Type, attribute.Preset)
                                             : WebDriverConfigs.GetDefault(attribute.Type);
            }
            else
            {
                throw new MissingWebDriverConfigException("WebDriverConfig attribute is not present.");
            }
        }
        public ISessionPerformer Initialize(IWebDriverConfig webDriverConfig, ISessionTracker tracker)
        {
            if (webDriverConfig == null)
            {
                throw new ArgumentNullException(nameof(webDriverConfig));
            }

            if (_initialized)
            {
                throw new InvalidOperationException($"{nameof(SequentialSessionPerformer)} can be initialized only once.");
            }

            _webDriver = new Lazy <IWebDriver>(() => WebDriverFactory.CreateFromConfig(webDriverConfig));
            _tracker   = tracker ?? throw new ArgumentNullException(nameof(tracker));

            _initialized = true;
            return(this);
        }
예제 #16
0
        public static IWebDriverConfig GetDefault(WebDriverType type)
        {
            if (type == default(WebDriverType))
            {
                throw new ArgumentException(nameof(type));
            }

            IWebDriverConfig webDriverConfig = null;

            switch (type)
            {
            case WebDriverType.Chrome:
                webDriverConfig = new ChromeWebDriverConfig();
                break;
            }

            return(webDriverConfig);
        }
예제 #17
0
        public static IWebDriver CreateFromConfig(IWebDriverConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            IWebDriver webDriver;

            switch (config.Type)
            {
            case Constants.WebDriverType.Chrome:
                webDriver = CreateChromeWebDriver(config);
                break;

            default:
                throw new ArgumentException($"WebDriver type not supported: {Enum.GetName(typeof(Constants.WebDriverType), (object)config.Type)}");
            }

            return(webDriver);
        }
예제 #18
0
        private ChromeOptions CreateChromeOptions(IWebDriverConfig browserConfig)
        {
            var opts = new ChromeOptions();

            opts.AddArguments("--disable-extensions");
            opts.AddArguments("--no-sandbox");
            opts.AddArguments($"--window-size={browserConfig.BrowserSize.Width},{browserConfig.BrowserSize.Height}");

            if (browserConfig.RunHeadless && downloadDirectory == null)
            {
                opts.AddArguments("--headless");
                opts.AddArguments("--disable-gpu");
                opts.AddArguments("--hide-scrollbars");
            }

            if (downloadDirectory != null)
            {
                opts.AddUserProfilePreference("download.default_directory", downloadDirectory.FullName);
            }

            options?.Invoke(opts);
            return(opts);
        }
예제 #19
0
 private static void Initialize(IWebDriverConfig config, IWebDriver driver, IAuthenticationHelper authHelper)
 => driver.Navigate().GoToUrl(config.BaseUrl);
예제 #20
0
 private Page GetPage(Type pageType, IWebDriverConfig config)
 => (Page)webDriver.UsingSite <Site>(config, log).NavigateToPage(pageType);