public IWebDriver CreateNewInstance() { if (!string.IsNullOrWhiteSpace(pathToFirefoxBinary)) { return(PrepareDriver(AlternativeInstance())); } try { return(PrepareDriver(new FirefoxDriverWrapper(GetProfile()))); } catch { SeleniumTestBase.Log("Default location of firefox was not found."); var env = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); if (env.Contains("(x86)")) { env = env.Replace("(x86)", "").Trim(); } var firefox = "Mozilla Firefox\\Firefox.exe"; if (File.Exists(Path.Combine(env, firefox))) { return(PrepareDriver(AlternativeInstance(env, firefox))); } env = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); if (File.Exists(Path.Combine(env, firefox))) { return(PrepareDriver(AlternativeInstance(env, firefox))); } throw; } }
/// <summary> /// Closes the current browser /// </summary> public void Dispose() { Browser.Quit(); Browser.Dispose(); if (Browser is IWebDriverWrapper) { ((IWebDriverWrapper)Browser).Disposed = true; SeleniumTestBase.LogDriverId(Browser, "Dispose - ChromeDriver"); } SeleniumTestBase.Log("IWebDriver was disposed."); }
public virtual void Clear() { SeleniumTestBase.Log("Cleaning session"); ExpectedConditions.AlertIsPresent()(Driver)?.Dismiss(); Driver.Manage().Cookies.DeleteAllCookies(); if (!(Driver.Url.Contains("chrome:") || Driver.Url.Contains("data:") || Driver.Url.Contains("about:"))) { ((IJavaScriptExecutor)driver).ExecuteScript( "if(typeof(Storage) !== undefined) { localStorage.clear(); }"); ((IJavaScriptExecutor)driver).ExecuteScript( "if(typeof(Storage) !== undefined) { sessionStorage.clear(); }"); } Driver.Navigate().GoToUrl("about:blank"); }
/// <summary> /// Navigates to specific url. /// </summary> /// <param name="url">url to navigate </param> /// <remarks> /// If url is ABSOLUTE, browser is navigated directly to url. /// If url is RELATIVE, browser is navigated to url combined from base url and relative url. /// Base url is specified in test configuration. (This is NOT url host of current page!) /// </remarks> public void NavigateToUrl(string url) { if (string.IsNullOrWhiteSpace(url)) { if (string.IsNullOrWhiteSpace(BaseUrl)) { throw new InvalidRedirectException(); } SeleniumTestBase.Log($"Start navigation to: {BaseUrl}", 10); Browser.Navigate().GoToUrl(BaseUrl); return; } //redirect if is absolute if (Uri.IsWellFormedUriString(url, UriKind.Absolute)) { SeleniumTestBase.Log($"Start navigation to: {url}", 10); Browser.Navigate().GoToUrl(url); return; } //redirect absolute with same schema if (url.StartsWith("//")) { var schema = new Uri(CurrentUrl).Scheme; var navigateUrltmp = $"{schema}:{url}"; SeleniumTestBase.Log($"Start navigation to: {navigateUrltmp}", 10); Browser.Navigate().GoToUrl(navigateUrltmp); return; } var builder = new UriBuilder(BaseUrl); // replace url fragments if (url.StartsWith("/")) { builder.Path = ""; var urlToNavigate = builder.ToString().TrimEnd('/') + "/" + url.TrimStart('/'); SeleniumTestBase.Log($"Start navigation to: {urlToNavigate}", 10); Browser.Navigate().GoToUrl(urlToNavigate); return; } var navigateUrl = builder.ToString().TrimEnd('/') + "/" + url.TrimStart('/'); SeleniumTestBase.Log($"Start navigation to: {navigateUrl}", 10); Browser.Navigate().GoToUrl(navigateUrl); }
/// <summary> /// Checks if browser can access given Url (browser returns status code 2??). /// </summary> /// <param name="url"></param> /// <param name="urlKind"></param> /// <returns></returns> public BrowserWrapper CheckIfUrlIsAccessible(string url, UrlKind urlKind) { var currentUri = new Uri(CurrentUrl); if (urlKind == UrlKind.Relative) { url = GetAbsoluteUrl(url); } if (urlKind == UrlKind.Absolute && url.StartsWith("//")) { if (!string.IsNullOrWhiteSpace(currentUri.Scheme)) { url = currentUri.Scheme + ":" + url; } } HttpWebResponse response = null; SeleniumTestBase.Log($"CheckIfUrlIsAccessible: Checking of url: '{url}'", 10); var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "HEAD"; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException e) { throw new WebException($"Unable to access {url}! {e.Status}", e); } finally { response?.Close(); } return(this); }
private static IWebDriver AlternativeInstance(string env, string firefox) { pathToFirefoxBinary = Path.Combine(env, firefox); SeleniumTestBase.Log($"Setting up new firefox binary file path to {pathToFirefoxBinary}"); return(PrepareDriver(AlternativeInstance())); }