Example #1
0
        public void SeleniumTestFailedExceptionTest()

        {
            var exps = new List <Exception>()
            {
                new Exception("Exception1"), new Exception("Exception2"), new Exception("Exception3")
            };
            var exp = new SeleniumTestFailedException(exps, "SpecificBrowserName", "SpecificScreen", "SpecificSession");
            var str = exp.ToString();

            TestContext.WriteLine(str);

            Assert.IsTrue(str.Contains("Exception1"));
            Assert.IsTrue(str.Contains("Exception2"));
            Assert.IsTrue(str.Contains("Exception3"));
            Assert.IsTrue(str.Contains("SpecificBrowserName"));
            Assert.IsTrue(str.Contains("SpecificScreen"));
            Assert.IsTrue(str.Contains("SpecificSession"));
        }
        public void SeleniumTestFailedExceptionTest()

        {
            var exps = new List <Exception>()
            {
                new UnexpectedElementException("Exception1")
                {
                    CurrentUrl = UrlConst, WebBrowser = "Chrome"
                }, new UnexpectedElementException("Exception2"), new UnexpectedElementException("Exception3")
            };
            var exp = new SeleniumTestFailedException(exps);
            var str = exp.ToString();

            TestContext.WriteLine(str);

            Assert.IsTrue(str.Contains("Exception1"));
            Assert.IsTrue(str.Contains("Exception2"));
            Assert.IsTrue(str.Contains("Exception3"));
            Assert.IsTrue(str.Contains("Url: https://example.com"));
            Assert.IsTrue(str.Contains("Browser: Chrome"));
            Assert.IsTrue(str.Contains(UrlConst));
        }
        /// <summary>
        /// Runs the specified testBody in all configured browsers.
        /// </summary>
        protected virtual void RunInAllBrowsers(Action <BrowserWrapper> testBody)
        {
            Log("Test no.: " + testsIndexer++, 10);
            CheckAvailableWebDriverFactories();
            foreach (var browserFactory in BrowserFactories)
            {
                string    browserName;
                Exception exception = null;

                CurrentTestExceptions.Clear();
                if (!(SeleniumTestsConfiguration.PlainMode || SeleniumTestsConfiguration.DeveloperMode))
                {
                    TryExecuteTest(testBody, browserFactory, out browserName, out exception);
                }
                else
                {
                    //developer mode - it throws exception directly without catch statement
                    ExecuteTest(testBody, browserFactory, out browserName);
                }
                if (exception != null)
                {
                    Exception throwException;
                    if (CurrentSubSection == null)
                    {
                        throwException = new SeleniumTestFailedException(CurrentTestExceptions, browserName, ScreenshotsFolderPath);
                    }
                    else
                    {
                        throwException = new SeleniumTestFailedException(CurrentTestExceptions, browserName, ScreenshotsFolderPath, CurrentSubSection);
                    }

                    Log("\r\n\r\n\r\n\r\nException logging: \r\n\r\n");
                    Log(throwException);
                    throw throwException;
                }
            }
        }
        protected virtual void TryExecuteTest(Action <BrowserWrapper> testBody, IWebDriverFactory browserFactory, out string browserName, out Exception exception)
        {
            var attemptNumber  = 0;
            var attampsMaximum = SeleniumTestsConfiguration.TestAttempts + (SeleniumTestsConfiguration.FastMode ? 1 : 0);

            do
            {
                attemptNumber++;
                WriteLine($"Attamp #{attemptNumber} starts....");
                exception = null;
                var browser = LatestLiveWebDriver = browserFactory.CreateNewInstance();
                LogDriverId(browser, "Driver creation - TryExecuteTest");
                var scope = new ScopeOptions()
                {
                    CurrentWindowHandle = browser.CurrentWindowHandle
                };
                wrapper     = new BrowserWrapper(browser, this, scope);
                browserName = browser.GetType().Name;

                WriteLine($"Testing browser '{browserName}' attempt no. {attemptNumber}");
                bool exceptionWasThrow = false;
                try
                {
                    BeforeSpecificBrowserTestStarts(wrapper);
                    Log("Execution of user test starting.", 10);
                    testBody(wrapper);
                    Log("Execution of user test ended.", 10);
                    AfterSpecificBrowserTestEnds(wrapper);
                }
                catch (Exception ex)
                {
                    exceptionWasThrow = true;
                    bool isExpected = false;
                    if (ExpectedExceptionType != null)
                    {
                        isExpected = ex.GetType() == ExpectedExceptionType || (AllowDerivedExceptionTypes && ExpectedExceptionType.IsInstanceOfType(ex));
                    }
                    Log("Test is expected to drop: " + isExpected, 10);
                    if (!isExpected)
                    {
                        TakeScreenshot(attemptNumber, wrapper);
                        // fail the test
                        CurrentTestExceptions.Add(exception = ex);
                        Log("Test attemp was not successfull! - TEST ATTAMP FAILED", 10);
                    }
                    if (attemptNumber < attampsMaximum)
                    {
                        RecreateBrowsers(browserFactory);
                    }
                    else
                    {
                        DisposeBrowsers(browserFactory);
                    }
                }
                finally
                {
                    if (!exceptionWasThrow)
                    {
                        CleanBrowsers(browserFactory);
                    }
                }
                if (ExpectedExceptionType != null && !exceptionWasThrow)
                {
                    CurrentTestExceptions.Add(exception = new SeleniumTestFailedException("Test was supposted to fail and it did not."));
                }
            }while (exception != null && attemptNumber < attampsMaximum);
        }