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; } } }
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); } }
private async Task RunCrashed(Exception ex) { await TestSuiteRunner.WebBrowserPool.DisposeBrowser(CurrentWebBrowser); TestSuiteRunner.LogVerbose($"(#{Thread.CurrentThread.ManagedThreadId}) {TestName}: Test failed {ex}"); CurrentWebBrowser = null; }
public TestInstance(TestSuiteRunner runner, ISeleniumTest testClass, TestConfiguration testConfiguration, string testName, Action <IBrowserWrapper> testAction) { this.TestSuiteRunner = runner; this.TestClass = testClass; this.TestConfiguration = testConfiguration; this.TestName = testName; this.testAction = testAction; }
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); }
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; } }
public static void LogError(this TestSuiteRunner runner, Exception ex) { runner.LoggerService.WriteLine(runner.TestContextProvider.GetGlobalScopeTestContext(), ex.ToString(), TraceLevel.Error); }
public static void LogInfo(this TestSuiteRunner runner, string message) { runner.LoggerService.WriteLine(runner.TestContextProvider.GetGlobalScopeTestContext(), message, TraceLevel.Info); }
public WebBrowserPool(TestSuiteRunner runner) { this.runner = runner; }