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;
            }
        }
Beispiel #2
0
 public void Recreate()
 {
     SeleniumTestBase.LogDriverId(driver, "Recreation - SelfCleanUpWebDriver (OLD)");
     Dispose();
     driver = CreateInstance();
     SeleniumTestBase.LogDriverId(driver, "Recreation - SelfCleanUpWebDriver (NEW)");
 }
        /// <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.");
        }
Beispiel #4
0
        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);
        }
Beispiel #6
0
 public void Dispose()
 {
     if (driver != null)
     {
         try
         {
             SeleniumTestBase.LogDriverId(driver, "Dispose    - SelfCleanUpWebDriver");
             ExpectedConditions.AlertIsPresent()(Driver)?.Dismiss();
             driver.Close();
             driver.Quit();
             driver.Dispose();
             driver = null;
         }
         catch (Exception)
         {
         }
     }
 }
        /// <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);
        }
 public FirefoxDriverWrapper(FirefoxDriverService service, FirefoxOptions options, TimeSpan commandTimeout) : base(service, options, commandTimeout)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - FirefoxDriver");
 }
 public ChromeDriverWrapper(string chromeDriverDirectory, ChromeOptions options, TimeSpan commandTimeout) : base(chromeDriverDirectory, options, commandTimeout)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - ChromeDriver");
 }
 public ChromeDriverWrapper(ChromeDriverService service, ChromeOptions options) : base(service, options)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - ChromeDriver");
 }
 public FirefoxDriverWrapper(FirefoxBinary binary, FirefoxProfile profile, TimeSpan commandTimeout) : base(binary, profile, commandTimeout)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - FirefoxDriver");
 }
 public ChromeDriverWrapper(string chromeDriverDirectory, ChromeOptions options) : base(chromeDriverDirectory, options)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - ChromeDriver");
 }
 public InternetExplorerDriverWrapper(InternetExplorerDriverService service, InternetExplorerOptions options, TimeSpan commandTimeout) : base(service, options, commandTimeout)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - InternetExplorerDriver");
 }
 public InternetExplorerDriverWrapper(string internetExplorerDriverServerDirectory, InternetExplorerOptions options, TimeSpan commandTimeout) : base(internetExplorerDriverServerDirectory, options, commandTimeout)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - InternetExplorerDriver");
 }
 public ChromeDriverWrapper(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout) : base(service, options, commandTimeout)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - ChromeDriver");
 }
 public FirefoxDriverWrapper(FirefoxOptions options) : base(options)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - FirefoxDriver");
 }
 public FirefoxDriverWrapper(FirefoxDriverService service) : base(service)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - FirefoxDriver");
 }
 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()));
 }
 public FirefoxDriverWrapper()
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - FirefoxDriver");
 }
 public InternetExplorerDriverWrapper(string internetExplorerDriverServerDirectory) : base(internetExplorerDriverServerDirectory)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - InternetExplorerDriver");
 }
 public FirefoxDriverWrapper(ICapabilities capabilities) : base(capabilities)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - FirefoxDriver");
 }
 public InternetExplorerDriverWrapper(InternetExplorerDriverService service, InternetExplorerOptions options) : base(service, options)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - InternetExplorerDriver");
 }
 public FirefoxDriverWrapper(FirefoxBinary binary, FirefoxProfile profile) : base(binary, profile)
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - FirefoxDriver");
 }
 public InternetExplorerDriverWrapper()
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - InternetExplorerDriver");
 }
 public ChromeDriverWrapper()
 {
     SeleniumTestBase.LogDriverId(this, "CTOR - ChromeDriver");
 }