Example #1
0
        public static IWebElement UntilVisible(IWebElement element, TimeSpan timeOut)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            while (true)
            {
                WebDriverException lastException = null;
                try
                {
                    if (element.Displayed)
                    {
                        return(element);
                    }
                    System.Threading.Thread.Sleep(100);
                }
                catch (ElementNotVisibleException e)     { lastException = e; }
                catch (NoSuchElementException e)         { lastException = e; }
                catch (StaleElementReferenceException e) { lastException = e; }

                if (sw.Elapsed > timeOut)
                {
                    string exceptionMessage = lastException == null ? "" : lastException.Message;
                    string errorMessage     = string.Format("Wait.UntilVisible: Element was not displayed after {0} Milliseconds" +
                                                            "\r\n Error Message:\r\n{1}", timeOut.TotalMilliseconds, exceptionMessage);
                    throw new TimeoutException(errorMessage);
                }
            }
        }
        public void DoOnFindOneOrMoreElementsWithException(string findElementTypeMethodName, By by, WebDriverException e)
        {
            string             newMessage       = e.Message + " {\"method\":\"" + findElementTypeMethodName + "\",\"params\":\"" + by + "\"}";
            WebDriverException wrappedException = new WebDriverException(newMessage, e);

            dataExchangeEntryHelper.SendEntryThrow(WebDrivers.GetURL(driver), WebDrivers.GetTitle(driver), wrappedException, findElementTypeMethodName, WebDrivers.GetAdvancedValues(driver, config));
        }
        public void DoOnSetUrlWithException(WebDriverException e, string url)
        {
            string             methodName       = "setUrl";
            string             newMessage       = e.Message + " {\"method\":\"" + methodName + "\",\"params\":\"" + url + "\"}";
            WebDriverException wrappedException = new WebDriverException(newMessage, e);

            dataExchangeEntryHelper.SendEntryThrow(WebDrivers.GetURL(driver), WebDrivers.GetTitle(driver), wrappedException, methodName, WebDrivers.GetAdvancedValues(driver, config));
        }
Example #4
0
        public void Find_ByScript_WithInvalidScript()
        {
            IWebElement element = null;

            WebDriverException exception = Assert.Throws <WebDriverException>(() =>
                                                                              element = _page.OptionByScriptWithInvalidScript.Scope);

            Assert.That(exception.Message, Does.StartWith("javascript error:"));

            AssertThrowsWithInnerException <AssertionException, WebDriverException>(() =>
                                                                                    _page.OptionByScriptWithInvalidScript.Should.AtOnce.Exist());
        }
 private void LogPageSource(WebDriverException exception)
 {
     try
     {
         Logger.Debug($"Page source:{Environment.NewLine}{Application.Driver.PageSource}", exception);
     }
     catch (WebDriverException e)
     {
         Logger.Error(exception.Message);
         Logger.Fatal("An exception occurred while tried to save the page source", e);
     }
 }
Example #6
0
        [TestCase(20, 120, "10102020", "1000", "10102020", "1345", true)]   //all true data
        public void Test1(int chargeP, int distanceP, string startDateP, string startTimeP, string endDateP, string endTimeP, bool state)
        {
            opsBrowser.Goto(ulrToTest);
            System.Threading.Thread.Sleep(5000);

            webDriver = opsBrowser.getDriver;
            var exeption = new WebDriverException();

            IWebElement charge = webDriver.FindElement(By.XPath("//input[@name='chargeStatus']"));

            charge.SendKeys(chargeP.ToString());

            IWebElement distance = webDriver.FindElement(By.XPath("//input[@name='distance']"));

            distance.SendKeys(distanceP.ToString());

            IWebElement startTime = webDriver.FindElement(By.XPath("//input[@name='startTime']"));

            startTime.SendKeys(startDateP);
            startTime.SendKeys(Keys.ArrowRight);
            startTime.SendKeys(startTimeP);
            startTime.SendKeys(Keys.Tab);

            IWebElement endTime = webDriver.FindElement(By.XPath("//input[@name='endTime']"));

            endTime.SendKeys(endDateP);
            endTime.SendKeys(Keys.ArrowRight);
            endTime.SendKeys(endTimeP);
            endTime.SendKeys(Keys.Tab);

            Random        r             = new Random();
            IWebElement   plugType      = webDriver.FindElement(By.XPath("//select[@id='plugType']"));
            SelectElement selectElement = new SelectElement(plugType);

            selectElement.SelectByIndex(r.Next(1, 6));

            IWebElement submit = webDriver.FindElement(By.XPath("//button[@type='submit']"));

            System.Threading.Thread.Sleep(2000);
            submit.Click();
            if (state)
            {
                System.Threading.Thread.Sleep(1000);
                Assert.IsTrue(webDriver.Url.ToString().Equals("https://localhost:44336/Booking/Post"));
            }
            if (!state)
            {
                System.Threading.Thread.Sleep(1000);
                Assert.IsTrue(webDriver.Url.ToString().Equals("https://localhost:44336/Booking/Create"));
            }
        }
Example #7
0
        public TResult Until <TResult>(Func <TObjectToWaitOn, TResult> condition)
        {
            if (condition == null)
            {
                                // ReSharper disable once LocalizableElement
                                throw new ArgumentNullException("condition", "condition cannot be null");
            }

            var resultType = typeof(TResult);

            if ((resultType.IsValueType && resultType != typeof(bool)) || !resultType.IsSubclassOf(typeof(object)))
            {
                                // ReSharper disable once LocalizableElement
                                throw new ArgumentException("Can only wait on an object or boolean response, tried to use type: " + resultType, "condition");
            }

            WebDriverException lastException = null;
            var endTime = _clock.LaterBy(_defaultTimeout);

            while (_clock.IsNowBefore(endTime))
            {
                try
                {
                    var result = condition(_objectToWaitOn);
                    if (resultType == typeof(bool))
                    {
                        var boolResult = result as bool?;
                        if (boolResult.HasValue && boolResult.Value)
                        {
                            return(result);
                        }
                    }
                    else
                    {
                        if (result != null)
                        {
                            return(result);
                        }
                    }
                }
                catch (WebDriverException e)
                {
                    lastException = e;
                }

                Thread.Sleep(_defaultSleepTimeout);
            }

            throw new TimeoutException(string.Format(CultureInfo.InvariantCulture, "Timed out after {0} seconds {1}", _defaultTimeout.TotalSeconds, _timeOutMessage), lastException);
        }
 /// <summary>
 /// Throw the stored exception if there is one.
 /// </summary>
 void ThrowStoredException()
 {
     try
     {
         if (exception != null)
         {
             throw exception;
         }
     }
     finally
     {
         exception = null;
     }
 }
        protected void SignInUser(string originalDestinationUri)
        {
            bool loggedIn = false;
            int  noTimesAttemptToSignIn =
                this.Configuration.GetNoTimesAttemptToSignIn();
            int currentAttempts = 0;

            WebDriverException thrownException = null;

            while ((currentAttempts < noTimesAttemptToSignIn) && (!loggedIn))
            {
                try
                {
                    thrownException = null;

                    this.AttemptToSignInUser(originalDestinationUri);

                    loggedIn = true;
                }
                catch (NoSuchElementException noSuchElementException)
                {
                    thrownException = noSuchElementException;
                }
                catch (StaleElementReferenceException staleElementReferenceException)
                {
                    thrownException = staleElementReferenceException;
                }

                if (thrownException != null)
                {
                    currentAttempts++;
                }
            }

            if (!loggedIn)
            {
                throw new NotFoundException(
                          $"Attempted to sign into the application using " +
                          $"credentials in secrets file {noTimesAttemptToSignIn} " +
                          $"time(s), however the required elements were missing.",
                          thrownException);
            }
        }
        protected void Click(IWebElement element)
        {
            AguardarLoading();
            WebDriverException possibleWebDriverException = null;
            Stopwatch          timeOut = new Stopwatch();

            timeOut.Start();
            while ((((int)timeOut.Elapsed.TotalSeconds) / 1000) <= GlobalParameters.CONFIG_DEFAULT_TIMEOUT_IN_SECONDS)
            {
                try
                {
                    TouchAction action = new TouchAction(DriverFactory.INSTANCE);
                    action.Tap(element);
                    // action.SingleTap(element);
                    action.Perform();
                    action.Release();
                    ExtentReportHelpers.AddTestInfo(3, "");
                    timeOut.Stop();
                    return;
                }
                catch (StaleElementReferenceException e)
                {
                    continue;
                }
                catch (WebDriverException e)
                {
                    possibleWebDriverException = e;
                    if (e.Message.Contains("Other element would receive the click"))
                    {
                        continue;
                    }
                    throw e;
                }
            }
            try
            {
                throw new Exception(possibleWebDriverException.Message);
            }
            catch (Exception e)
            {
                e.StackTrace.ToString();
            }
        }
 protected override void HandleLoadError(WebDriverException ex)
 {
     throw new Exception("HandleLoadError called", ex);
 }
 public void DoOnSetUrlWithException(WebDriverException e, string url)
 {
     // nothing to do
 }
 public void DoOnFindOneOrMoreElementsWithException(string findElementTypeMethodName, By by, WebDriverException e)
 {
     // nothing to do
 }
 public void SendEntryThrow(string currentURL, string pageTitle, WebDriverException e, string methodName, IDictionary <string, long> advancedValues)
 {
     exception = e;
     SendEntry(currentURL, pageTitle, methodName, advancedValues);
 }
Example #15
0
 /// <summary>
 /// HandleLoadError gives a subclass the opportunity to handle a <see cref="WebDriverException"/> that occurred
 /// during the execution of <see cref="ExecuteLoad"/>.
 /// </summary>
 /// <param name="ex">The exception which occurs on load.</param>
 protected virtual void HandleLoadError(WebDriverException ex)
 {
 }
        public void SetUp()
        {
            if (WebDriver != null)
            {
                return;
            }

            if (_browser.Equals("firefox"))
            {
                SetWebDriverToFirefox();
            }
            else if (_browser.Equals("ie"))
            {
                if (GetCurrentPlatform().Equals("Windows"))
                {
                    if (_is64BitOS)
                    {
                        _ieDriverServer = _ieDriverServer + "_x64";
                    }
                    else
                    {
                        _ieDriverServer = _ieDriverServer + "_Win32";
                    }
                }
                else
                {
                    Trace("Assigned browser {" + _browser + "} is not supported on " + _platform);
                    var err = new WebDriverException();
                    err.Should().BeAssignableTo <WebDriverException>();
                    TearDown();
                }
                SetWebDriverToIE();
            }
            else if (_browser.Equals("chrome"))
            {
                if (GetCurrentPlatform().Equals("Mac"))
                {
                    _chromedriver = _chromedriver + "_mac";
                }
                else if (GetCurrentPlatform().Equals("Linux"))
                {
                    if (!_is64BitOS)
                    {
                        _chromedriver = _chromedriver + "_linux32";
                    }
                    else
                    {
                        _chromedriver = _chromedriver + "_linux64";
                    }
                }
                else
                {
                    _chromedriver = _chromedriver + "_win";
                }

                SetWebDriverToChrome();
            }
            else if (_browser.Equals("safari"))
            {
                SetWebDriverToSafari();
            }
            else
            {
                if (GetCurrentPlatform().Equals("Mac"))
                {
                    _phantomjs = _phantomjs + "_mac";
                }
                else if (GetCurrentPlatform().Equals("Windows"))
                {
                    _phantomjs = _phantomjs + "_win";
                }
                else if (GetCurrentPlatform().Equals("Linux"))
                {
                    if (!_is64BitOS)
                    {
                        _phantomjs = _phantomjs + "_linux32";
                    }
                    else
                    {
                        _phantomjs = _phantomjs + "_linux64";
                    }
                }
                SetWebDriverToPhantomJs();
            }

            if (WebDriver != null)
            {
                WebDriver.Manage().Window.Maximize();
                WebDriver.Manage().Timeouts().ImplicitlyWait(TimeToWait);
            }

            Trace("Web driver {" + _browser + "} successfully started...");
            ScenarioContext.Current.SetWebDriver(WebDriver);
        }
Example #17
0
 protected override void HandleLoadError(WebDriverException ex) =>
 throw new LoadableComponentException($"Page {typeof(T).Name} was not loaded", ex);