public void Assert(Action action) { AtataContext context = AtataContext.Current; IEnumerable <AssertionResult> assertionResultsBefore = AtataContext.Current.PendingFailureAssertionResults.ToArray(); try { action(); } catch (Exception exception) { var failedResults = ExtractAndRemoveExclusiveFailedAssertionResults(assertionResultsBefore). Concat(new[] { AssertionResult.ForException(exception) }); throw VerificationUtils.CreateAggregateAssertionException(failedResults); } if (context.AggregateAssertionLevel == 0) { var failedResults = ExtractAndRemoveExclusiveFailedAssertionResults(assertionResultsBefore); if (failedResults.Any()) { throw VerificationUtils.CreateAggregateAssertionException(failedResults); } } }
private static void CheckAtataContext() { if (AtataContext.Current == null) { AtataContext.Configure().Build(); } }
private void SetUp(AtataContext context) { OnBuilding(); if (context.BaseUrl != null) { context.Log.Trace($"Set: BaseUrl={context.BaseUrl}"); } LogRetrySettings(context); if (BuildingContext.Culture != null) { ApplyCulture(context, BuildingContext.Culture); } context.DriverFactory = BuildingContext.DriverFactoryToUse; context.DriverAlias = BuildingContext.DriverFactoryToUse.Alias; context.InitDriver(); context.Log.Trace($"Set: Driver={context.Driver.GetType().Name}{BuildingContext.DriverFactoryToUse?.Alias?.ToFormattedString(" (alias={0})")}"); OnBuilt(); }
private static T To <T>(T pageObject, GoOptions options) where T : PageObject <T> { if (AtataContext.Current == null) { AtataContext.Build().SetUp(); } if (AtataContext.Current.PageObject == null) { pageObject = pageObject ?? ActivatorEx.CreateInstance <T>(); AtataContext.Current.PageObject = pageObject; if (!string.IsNullOrWhiteSpace(options.Url)) { ToUrl(options.Url); } pageObject.NavigateOnInit = options.Navigate; pageObject.Init(); return(pageObject); } else { IPageObject currentPageObject = (IPageObject)AtataContext.Current.PageObject; T newPageObject = currentPageObject.GoTo(pageObject, options); AtataContext.Current.PageObject = newPageObject; return(newPageObject); } }
private static void LogRetrySettings(AtataContext context) { string messageFormat = "Set: {0}Timeout={1}; {0}RetryInterval={2}"; context.Log.Trace(messageFormat, "ElementFind", context.ElementFindTimeout.ToShortIntervalString(), context.ElementFindRetryInterval.ToShortIntervalString()); context.Log.Trace(messageFormat, "Waiting", context.WaitingTimeout.ToShortIntervalString(), context.WaitingRetryInterval.ToShortIntervalString()); context.Log.Trace(messageFormat, "Verification", context.VerificationTimeout.ToShortIntervalString(), context.VerificationRetryInterval.ToShortIntervalString()); }
internal EventBus(AtataContext context, IEnumerable <EventSubscriptionItem> eventSubscriptions) { _context = context.CheckNotNull(nameof(context)); if (eventSubscriptions != null) { foreach (var subscription in eventSubscriptions) { Subscribe(subscription.EventType, subscription.EventHandler); } } }
/// <summary> /// Builds the <see cref="AtataContext" /> instance and sets it to <see cref="AtataContext.Current" /> property. /// </summary> /// <returns>The created <see cref="AtataContext"/> instance.</returns> public AtataContext Build() { AtataContext.InitGlobalVariables(); LogManager logManager = new LogManager(); foreach (var logConsumerItem in BuildingContext.LogConsumers) { logManager.Use(logConsumerItem.Consumer, logConsumerItem.MinLevel, logConsumerItem.LogSectionFinish); } foreach (var screenshotConsumer in BuildingContext.ScreenshotConsumers) { logManager.Use(screenshotConsumer); } AtataContext context = new AtataContext { TestName = BuildingContext.TestNameFactory?.Invoke(), BaseUrl = BuildingContext.BaseUrl, Log = logManager, CleanUpActions = BuildingContext.CleanUpActions, RetryTimeout = BuildingContext.RetryTimeout, RetryInterval = BuildingContext.RetryInterval, AssertionExceptionType = BuildingContext.AssertionExceptionType }; AtataContext.Current = context; context.LogTestStart(); context.Log.Start("Set up AtataContext", LogLevel.Trace); if (context.BaseUrl != null) { context.Log.Trace($"Set: BaseUrl={context.BaseUrl}"); } context.Log.Trace($"Set: RetryTimeout={context.RetryTimeout.ToIntervalString()}; RetryInterval={context.RetryInterval.ToIntervalString()}"); context.Driver = BuildingContext.DriverFactoryToUse?.Create() ?? new FirefoxDriver(); context.DriverAlias = BuildingContext.DriverFactoryToUse?.Alias ?? DriverAliases.Firefox; context.Log.Trace($"Set: Driver={context.Driver.GetType().Name}{BuildingContext.DriverFactoryToUse?.Alias?.ToFormattedString(" (alias={0})")}"); context.Driver.Manage().Timeouts().SetRetryTimeout(BuildingContext.RetryTimeout, BuildingContext.RetryInterval); context.Log.EndSection(); context.CleanExecutionStartDateTime = DateTime.Now; return(context); }
private static void ApplyCulture(AtataContext context, CultureInfo culture) { Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = culture; #if !NET40 if (AtataContext.ModeOfCurrent == AtataContextModeOfCurrent.Static) { CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = culture; } #endif context.Log.Trace($"Set: Culture={culture.Name}"); }
public void ReportFailure(string message, Exception exception) { string completeMessage = $"Unexpected {message}"; completeMessage = VerificationUtils.AppendExceptionToFailureMessage(completeMessage, exception); string stackTrace = VerificationUtils.BuildStackTraceForAggregateAssertion(); AtataContext context = AtataContext.Current; context.AssertionResults.Add(AssertionResult.ForWarning(completeMessage, stackTrace)); context.Log.Warn(completeMessage); context.WarningReportStrategy.Report(completeMessage, stackTrace); }
internal static string BuildPath() { AtataContext context = AtataContext.Current; DateTime buildStart = context?.BuildStartInTimeZone ?? AtataContext.BuildStart.Value; string path = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "Logs", buildStart.ToString(DefaultDateTimeFormat, CultureInfo.InvariantCulture)); return string.IsNullOrEmpty(context?.TestName) ? path : Path.Combine(path, context.TestNameSanitized); }
protected PageObject() { _context = AtataContext.Current ?? throw new InvalidOperationException( $"Cannot instantiate {GetType().Name} because {nameof(AtataContext)}.{nameof(AtataContext.Current)} is null."); NavigateOnInit = true; ScopeLocator = new PlainScopeLocator(CreateScopeBy); Owner = (TOwner)this; Report = new Report <TOwner>((TOwner)this, Log); PageUri = new UriProvider <TOwner>(this, GetUri, "URI"); UIComponentResolver.InitPageObject <TOwner>(this); }
public void Handle(AtataContextCleanUpEvent eventData, AtataContext context) { string directoryPath = _directoryPathBuilder.Invoke(context); directoryPath = context.FillTemplateString(directoryPath); DirectoryInfo directory = new DirectoryInfo(directoryPath); if (directory.Exists) { var files = directory.EnumerateFiles("*", SearchOption.AllDirectories); foreach (var file in files) { NUnitAdapter.AddTestAttachment(file.FullName); } } }
private string BuildFilePath() { AtataContext context = AtataContext.Current; if (FilePathBuilder != null) { return(FilePathBuilder.Invoke(context).SanitizeForPath()); } string folderPath = FolderPathBuilder?.Invoke(context) ?? BuildDefaultFolderPath(); folderPath = folderPath.SanitizeForPath(); string fileName = FileNameBuilder?.Invoke(context) ?? BuildDefaultFileName(context); fileName = fileName.SanitizeForFileName(); return(Path.Combine(folderPath, fileName)); }
private void OnBuilt(AtataContext context) { if (BuildingContext.OnBuiltActions != null) { foreach (Action action in BuildingContext.OnBuiltActions) { try { action(); } catch (Exception e) { context.Log.Error($"On {nameof(AtataContext)} built action failure.", e); } } } context.Log.EndSection(); context.CleanExecutionStartDateTime = DateTime.Now; }
private void OnBuilding(AtataContext context) { context.LogTestStart(); context.Log.Start("Set up AtataContext", LogLevel.Trace); if (BuildingContext.OnBuildingActions != null) { foreach (Action action in BuildingContext.OnBuildingActions) { try { action(); } catch (Exception e) { context.Log.Error($"On {nameof(AtataContext)} building action failure.", e); } } } }
public void ReportFailure(string message, Exception exception) { string completeMessage = $"Wrong {message}"; string completeMessageWithException = VerificationUtils.AppendExceptionToFailureMessage(completeMessage, exception); string stackTrace = VerificationUtils.BuildStackTraceForAggregateAssertion(); AtataContext context = AtataContext.Current; context.AssertionResults.Add(AssertionResult.ForFailure(completeMessageWithException, stackTrace)); if (context.AggregateAssertionLevel == 0) { throw VerificationUtils.CreateAssertionException(completeMessage, exception); } else { context.Log.Error(completeMessage); context.AggregateAssertionStrategy.ReportFailure(completeMessageWithException, stackTrace); } }
/// <summary> /// Navigates to the specified URL. /// </summary> /// <param name="url">The URL.</param> public static void ToUrl(string url) { if (AtataContext.Current == null) { AtataContext.Build().SetUp(); } Uri absoluteUri; if (!Uri.TryCreate(url, UriKind.Absolute, out absoluteUri)) { if (!AtataContext.Current.IsNavigated && AtataContext.Current.BaseUrl == null) { if (string.IsNullOrWhiteSpace(url)) { throw new InvalidOperationException("Cannot navigate to empty or null URL. AtataContext.Current.BaseUrl can be set with base URL."); } else { throw new InvalidOperationException($"Cannot navigate to relative URL \"{url}\". AtataContext.Current.BaseUrl can be set with base URL."); } } if (AtataContext.Current.BaseUrl == null) { Uri currentUri = new Uri(AtataContext.Current.Driver.Url, UriKind.Absolute); string domainPart = currentUri.GetComponents(UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.Unescaped); Uri domainUri = new Uri(domainPart); absoluteUri = new Uri(domainUri, url); } else { absoluteUri = ConcatWithBaseUrl(url); } } Navigate(absoluteUri); }
/// <summary> /// Sets up the context. /// </summary> public void SetUp() { AtataContext.InitGlobalVariables(); LogManager logManager = new LogManager(); foreach (var logConsumerItem in BuildingContext.LogConsumers) { logManager.Use(logConsumerItem.Consumer, logConsumerItem.MinLevel, logConsumerItem.LogSectionFinish); } foreach (var screenshotConsumer in BuildingContext.ScreenshotConsumers) { logManager.Use(screenshotConsumer); } AtataContext context = new AtataContext { TestName = BuildingContext.TestName, BaseUrl = BuildingContext.BaseUrl, Log = logManager, CleanUpActions = BuildingContext.CleanUpActions, RetryTimeout = BuildingContext.RetryTimeout, RetryInterval = BuildingContext.RetryInterval }; AtataContext.Current = context; context.LogTestStart(); context.Log.Start("Init WebDriver"); context.Driver = BuildingContext.DriverCreator?.Invoke() ?? new FirefoxDriver(); context.Driver.Manage().Timeouts().SetRetryTimeout(BuildingContext.RetryTimeout, BuildingContext.RetryInterval); context.Log.EndSection(); context.CleanExecutionStartDateTime = DateTime.Now; }
public void ReportFailure(string message, Exception exception) { string completeMessage = $"Unexpected {message}"; string completeMessageWithException = VerificationUtils.AppendExceptionToFailureMessage(completeMessage, exception); string stackTrace = VerificationUtils.BuildStackTraceForAggregateAssertion(); AtataContext context = AtataContext.Current; if (context != null) { context.AssertionResults.Add(AssertionResult.ForWarning(completeMessageWithException, stackTrace)); context.Log.Warn(completeMessageWithException); context.WarningReportStrategy.Report(completeMessageWithException, stackTrace); } else { throw new InvalidOperationException( $"Cannot report warning to {nameof(AtataContext)}.{nameof(AtataContext.Current)} as current context is null.", VerificationUtils.CreateAssertionException(completeMessage, exception)); } }
public AtataContextLogEventInfoFactory(AtataContext context) { _context = context.CheckNotNull(nameof(context)); }
protected virtual string BuildDefaultFileName(AtataContext context) => DefaultFileName;
/// <summary> /// Builds the <see cref="AtataContext" /> instance and sets it to <see cref="AtataContext.Current" /> property. /// </summary> /// <returns>The created <see cref="AtataContext"/> instance.</returns> public AtataContext Build() { AtataContext.InitGlobalVariables(); ValidateBuildingContextBeforeBuild(); LogManager logManager = new LogManager(); foreach (var logConsumerItem in BuildingContext.LogConsumers) { logManager.Use(logConsumerItem); } foreach (var screenshotConsumer in BuildingContext.ScreenshotConsumers) { logManager.Use(screenshotConsumer); } IObjectConverter objectConverter = new ObjectConverter { AssemblyNamePatternToFindTypes = BuildingContext.DefaultAssemblyNamePatternToFindTypes }; IObjectMapper objectMapper = new ObjectMapper(objectConverter); IObjectCreator objectCreator = new ObjectCreator(objectConverter, objectMapper); AtataContext context = new AtataContext { TestName = BuildingContext.TestNameFactory?.Invoke(), BaseUrl = BuildingContext.BaseUrl, Log = logManager, OnDriverCreatedActions = BuildingContext.OnDriverCreatedActions?.ToList() ?? new List <Action <RemoteWebDriver> >(), CleanUpActions = BuildingContext.CleanUpActions?.ToList() ?? new List <Action>(), Attributes = BuildingContext.Attributes.Clone(), BaseRetryTimeout = BuildingContext.BaseRetryTimeout, BaseRetryInterval = BuildingContext.BaseRetryInterval, ElementFindTimeout = BuildingContext.ElementFindTimeout, ElementFindRetryInterval = BuildingContext.ElementFindRetryInterval, WaitingTimeout = BuildingContext.WaitingTimeout, WaitingRetryInterval = BuildingContext.WaitingRetryInterval, VerificationTimeout = BuildingContext.VerificationTimeout, VerificationRetryInterval = BuildingContext.VerificationRetryInterval, Culture = BuildingContext.Culture ?? CultureInfo.CurrentCulture, AssertionExceptionType = BuildingContext.AssertionExceptionType, AggregateAssertionExceptionType = BuildingContext.AggregateAssertionExceptionType, AggregateAssertionStrategy = BuildingContext.AggregateAssertionStrategy ?? new AtataAggregateAssertionStrategy(), WarningReportStrategy = BuildingContext.WarningReportStrategy ?? new AtataWarningReportStrategy(), ObjectConverter = objectConverter, ObjectMapper = objectMapper, ObjectCreator = objectCreator }; AtataContext.Current = context; context.LogTestStart(); context.Log.ExecuteSection( new LogSection("Set up AtataContext", LogLevel.Trace), () => SetUp(context)); context.PureExecutionStopwatch.Start(); return(context); }
/// <summary> /// Builds the <see cref="AtataContext" /> instance and sets it to <see cref="AtataContext.Current" /> property. /// </summary> /// <returns>The created <see cref="AtataContext"/> instance.</returns> public AtataContext Build() { AtataContext.InitGlobalVariables(); ValidateBuildingContextBeforeBuild(); LogManager logManager = new LogManager(); foreach (var logConsumerItem in BuildingContext.LogConsumers) { logManager.Use(logConsumerItem.Consumer, logConsumerItem.MinLevel, logConsumerItem.LogSectionFinish); } foreach (var screenshotConsumer in BuildingContext.ScreenshotConsumers) { logManager.Use(screenshotConsumer); } AtataContext context = new AtataContext { TestName = BuildingContext.TestNameFactory?.Invoke(), BaseUrl = BuildingContext.BaseUrl, Log = logManager, OnDriverCreatedActions = BuildingContext.OnDriverCreatedActions?.ToList() ?? new List <Action <RemoteWebDriver> >(), CleanUpActions = BuildingContext.CleanUpActions?.ToList() ?? new List <Action>(), BaseRetryTimeout = BuildingContext.BaseRetryTimeout, BaseRetryInterval = BuildingContext.BaseRetryInterval, ElementFindTimeout = BuildingContext.ElementFindTimeout, ElementFindRetryInterval = BuildingContext.ElementFindRetryInterval, WaitingTimeout = BuildingContext.WaitingTimeout, WaitingRetryInterval = BuildingContext.WaitingRetryInterval, VerificationTimeout = BuildingContext.VerificationTimeout, VerificationRetryInterval = BuildingContext.VerificationRetryInterval, Culture = BuildingContext.Culture ?? CultureInfo.CurrentCulture, AssertionExceptionType = BuildingContext.AssertionExceptionType, AggregateAssertionExceptionType = BuildingContext.AggregateAssertionExceptionType, AggregateAssertionStrategy = BuildingContext.AggregateAssertionStrategy ?? new AtataAggregateAssertionStrategy(), WarningReportStrategy = BuildingContext.WarningReportStrategy ?? new AtataWarningReportStrategy() }; AtataContext.Current = context; OnBuilding(context); if (context.BaseUrl != null) { context.Log.Trace($"Set: BaseUrl={context.BaseUrl}"); } LogRetrySettings(context); if (BuildingContext.Culture != null) { ApplyCulture(context, BuildingContext.Culture); } context.DriverFactory = BuildingContext.DriverFactoryToUse; context.DriverAlias = BuildingContext.DriverFactoryToUse.Alias; context.InitDriver(); context.Log.Trace($"Set: Driver={context.Driver.GetType().Name}{BuildingContext.DriverFactoryToUse?.Alias?.ToFormattedString(" (alias={0})")}"); OnBuilt(context); return(context); }
/// <summary> /// Initializes a new instance of the <see cref="EventBus"/> class. /// </summary> /// <param name="context">The context.</param> public EventBus(AtataContext context) : this(context, null) { }
/// <summary> /// Initializes a new instance of the <see cref="AtataNavigator"/> class. /// </summary> /// <param name="context">The context.</param> public AtataNavigator(AtataContext context) { this.context = context.CheckNotNull(nameof(context)); }
/// <summary> /// Builds the <see cref="AtataContext" /> instance and sets it to <see cref="AtataContext.Current" /> property. /// </summary> /// <returns>The created <see cref="AtataContext"/> instance.</returns> public AtataContext Build() { AtataContext.InitGlobalVariables(); ValidateBuildingContextBeforeBuild(); LogManager logManager = new LogManager(); foreach (var logConsumerItem in BuildingContext.LogConsumers) { logManager.Use(logConsumerItem.Consumer, logConsumerItem.MinLevel, logConsumerItem.LogSectionFinish); } foreach (var screenshotConsumer in BuildingContext.ScreenshotConsumers) { logManager.Use(screenshotConsumer); } AtataContext context = new AtataContext { TestName = BuildingContext.TestNameFactory?.Invoke(), BaseUrl = BuildingContext.BaseUrl, Log = logManager, CleanUpActions = BuildingContext.CleanUpActions, RetryTimeout = BuildingContext.RetryTimeout, RetryInterval = BuildingContext.RetryInterval, Culture = BuildingContext.Culture ?? CultureInfo.CurrentCulture, AssertionExceptionType = BuildingContext.AssertionExceptionType }; AtataContext.Current = context; context.LogTestStart(); context.Log.Start("Set up AtataContext", LogLevel.Trace); if (context.BaseUrl != null) { context.Log.Trace($"Set: BaseUrl={context.BaseUrl}"); } context.Log.Trace($"Set: RetryTimeout={context.RetryTimeout.ToIntervalString()}; RetryInterval={context.RetryInterval.ToIntervalString()}"); if (BuildingContext.Culture != null) { if (AtataContext.IsThreadStatic) { Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = BuildingContext.Culture; } else { #if NET40 Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = BuildingContext.Culture; #else CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = BuildingContext.Culture; #endif } context.Log.Trace($"Set: Culture={BuildingContext.Culture.Name}"); } context.DriverFactory = BuildingContext.DriverFactoryToUse; context.Driver = BuildingContext.DriverFactoryToUse.Create(); if (context.Driver == null) { throw new InvalidOperationException($"Failed to build {nameof(AtataContext)} as driver factory returned 'null' as a driver."); } context.DriverAlias = BuildingContext.DriverFactoryToUse.Alias; context.Log.Trace($"Set: Driver={context.Driver.GetType().Name}{BuildingContext.DriverFactoryToUse?.Alias?.ToFormattedString(" (alias={0})")}"); context.Driver.Manage().Timeouts().SetRetryTimeout(BuildingContext.RetryTimeout, BuildingContext.RetryInterval); context.Log.EndSection(); context.CleanExecutionStartDateTime = DateTime.Now; return(context); }
/// <inheritdoc/> public void Handle(TEvent eventData, AtataContext context) => _action.Invoke(eventData, context);