Пример #1
0
        private string TakeScreenshot(IBrowserWrapper browserWrapper)
        {
            var testContext = TestSuiteRunner.TestContextProvider.GetGlobalScopeTestContext();

            try
            {
                var deploymentDirectory = testContext.DeploymentDirectory;
                if (!string.IsNullOrWhiteSpace(TestSuiteRunner.Configuration.TestRunOptions.ScreenshotPath))
                {
                    deploymentDirectory = CreateDirectory(Path.Combine(TestSuiteRunner.Configuration.TestRunOptions.ScreenshotPath, DateTime.UtcNow.ToString("yyyyMMdd")));
                }

                var filename = Path.Combine(deploymentDirectory,
                                            $"{testContext.FullyQualifiedTestClassName}_{testContext.TestName}_{testAttemptNumber}.png");
                TestSuiteRunner.LogVerbose(
                    $"(#{Thread.CurrentThread.ManagedThreadId}) {TestName}: Taking screenshot {filename}");
                browserWrapper.TakeScreenshot(filename);
                TestSuiteRunner.TestContextAccessor.GetTestContext().AddResultFile(filename);
                return(filename);
            }
            catch (Exception ex)
            {
                TestSuiteRunner.LogError(new Exception(
                                             $"(#{Thread.CurrentThread.ManagedThreadId}) {TestName}: Failed to take screenshot.", ex));
                return(null);
            }
        }
Пример #2
0
        private void RunTest(IWebBrowser browser)
        {
            // create a new browser wrapper
            var wrapper = TestSuiteRunner.ServiceFactory.Resolve <IBrowserWrapper>(browser, browser.Driver, this, new ScopeOptions());

            // prepare test context
            var testContext = TestSuiteRunner.TestContextProvider.CreateTestContext(this);

            using (TestConfiguration.Factory.TestSuiteRunner.TestContextAccessor.Scope(testContext))
            {
                try
                {
                    TestSuiteRunner.LogVerbose(
                        $"(#{Thread.CurrentThread.ManagedThreadId}) {TestName}: Running test in browser instance {browser.UniqueName}");

                    // run actual test
                    testAction(wrapper);

                    TestSuiteRunner.LogVerbose($"(#{Thread.CurrentThread.ManagedThreadId}) {TestName}: Test passed");
                }
                catch (TestExceptionBase ex)
                {
                    AddExceptionMetadata(ex, wrapper);
                    throw;
                }
                catch
                {
                    // recreate the browser
                    TestSuiteRunner.LogVerbose($"(#{Thread.CurrentThread.ManagedThreadId}) {TestName}: Test failed");
                    throw;
                }
            }
        }
Пример #3
0
        private async Task RunCrashed(Exception ex)
        {
            await TestSuiteRunner.WebBrowserPool.DisposeBrowser(CurrentWebBrowser);

            TestSuiteRunner.LogVerbose($"(#{Thread.CurrentThread.ManagedThreadId}) {TestName}: Test failed {ex}");
            CurrentWebBrowser = null;
        }
Пример #4
0
        private async Task RetryAsync(Func <Task> action, int testAttemptsCount)
        {
            var errors = new List <Exception>();

            for (testAttemptNumber = 1; testAttemptNumber <= testAttemptsCount; testAttemptNumber++)
            {
                try
                {
                    await action();

                    return;
                }
                catch (Exception ex)
                {
                    TestSuiteRunner.LogVerbose(
                        $"(#{Thread.CurrentThread.ManagedThreadId}) {TestName}: Test attempt #{testAttemptNumber} failed.");
                    errors.Add(ex);
                }
            }
            throw new SeleniumTestFailedException(errors);
        }
Пример #5
0
        public async Task <IWebBrowser> GetOrCreateBrowser(IWebBrowserFactory factory)
        {
            IWebBrowser instance = null;

            while (true)
            {
                // try to find the browser in the pool
                runner.LogVerbose($"(#{Thread.CurrentThread.ManagedThreadId}) Looking for the browser in the browser {factory.Name} pool cache.");
                lock (locker)
                {
                    instance = pool.FirstOrDefault(b => b.Factory == factory);

                    if (instance != null)
                    {
                        pool.Remove(instance);
                    }
                }

                // if the browser is not in the cache, acquire a new one
                if (instance == null)
                {
                    runner.LogVerbose($"(#{Thread.CurrentThread.ManagedThreadId}) No browser {factory.Name} found in the cache, requesting a new one.");
                    instance = await factory.AcquireBrowser();
                }

                if (instance != null)
                {
                    runner.LogVerbose($"(#{Thread.CurrentThread.ManagedThreadId}) Browser instance {instance.UniqueName} acquired successfully.");
                    break;
                }

                // TODO: implement queue using TaskCompletionSource
                runner.LogVerbose($"(#{Thread.CurrentThread.ManagedThreadId}) No instances of {factory.Name} browser available, retrying in 5 seconds...");
                await Task.Delay(5000);
            }

            return(instance);
        }
Пример #6
0
        private async Task RunAsyncCore()
        {
            try
            {
                TestSuiteRunner.LogVerbose(
                    $"(#{Thread.CurrentThread.ManagedThreadId}) {TestName}: Acquiring browser from browser pool");
                CurrentWebBrowser = await TestSuiteRunner.WebBrowserPool.GetOrCreateBrowser(TestConfiguration.Factory);

                RunTest(CurrentWebBrowser);

                await TestSuiteRunner.WebBrowserPool.ReturnBrowserToPool(CurrentWebBrowser);

                TestSuiteRunner.LogVerbose(
                    $"(#{Thread.CurrentThread.ManagedThreadId}) {TestName}: Browser {CurrentWebBrowser.UniqueName} returned to browser pool");
                CurrentWebBrowser = null;
            }
            catch (Exception ex)
            {
                await RunCrashed(ex);

                throw;
            }
        }