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); }
/// <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); }
public AuthenticationHelper(IWebDriverConfig config, ILogger log) { this.config = config; this.log = log; authCookies = new CookieContainer(); initialized = false; }
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); }
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); }
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); }
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; }
/// <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); }
public void CreateFromConfig_NullConfig_ThrowsArgumentNullException() { // Arrange IWebDriverConfig config = null; // Act TestDelegate action = () => WebDriverFactory.CreateFromConfig(config); // Assert Assert.Throws <ArgumentNullException>(action); }
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); }
/// <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); }
/// <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); }
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); }
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); }
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); }
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); }
private static void Initialize(IWebDriverConfig config, IWebDriver driver, IAuthenticationHelper authHelper) => driver.Navigate().GoToUrl(config.BaseUrl);
private Page GetPage(Type pageType, IWebDriverConfig config) => (Page)webDriver.UsingSite <Site>(config, log).NavigateToPage(pageType);