Best Atata code snippet using Atata.EventBus.EventBus
EventBusTests.cs
Source:EventBusTests.cs
2using Moq;3using NUnit.Framework;4namespace Atata.Tests5{6 public class EventBusTests7 {8 protected Subject<EventBus> Sut { get; private set; }9 protected AtataContext Context { get; private set; }10 [SetUp]11 public void SetUp()12 {13 Context = AtataContext.Configure()14 .UseDriverInitializationStage(AtataContextDriverInitializationStage.None)15 .Build();16 Sut = new EventBus(Context)17 .ToSutSubject();18 }19 [TestFixture]20 public class Publish : EventBusTests21 {22 [Test]23 public void Null() =>24 Sut.Invoking(x => x.Publish<TestEvent>(null))25 .Should.Throw<ArgumentNullException>();26 [Test]27 public void WhenThereIsNoSubscription() =>28 Sut.Invoking(x => x.Publish(new TestEvent()))29 .Should.Not.Throw();30 [Test]31 public void WhenThereIsSubscription()32 {33 var actionMock = new Mock<Action<TestEvent>>();34 var eventData = new TestEvent();35 Sut.Object.Subscribe(actionMock.Object);36 Sut.Act(x => x.Publish(eventData));37 actionMock.Verify(x => x(eventData), Times.Once);38 }39 [Test]40 public void WhenThereIsSubscription_CanHandle_False()41 {42 var conditionalEventHandlerMock = new Mock<IConditionalEventHandler<TestEvent>>(MockBehavior.Strict);43 var eventData = new TestEvent();44 Sut.Object.Subscribe(conditionalEventHandlerMock.Object);45 conditionalEventHandlerMock.Setup(x => x.CanHandle(eventData, Context)).Returns(false);46 Sut.Act(x => x.Publish(eventData));47 }48 [Test]49 public void WhenThereIsSubscription_CanHandle_True()50 {51 var conditionalEventHandlerMock = new Mock<IConditionalEventHandler<TestEvent>>(MockBehavior.Strict);52 var eventData = new TestEvent();53 Sut.Object.Subscribe(conditionalEventHandlerMock.Object);54 conditionalEventHandlerMock.Setup(x => x.CanHandle(eventData, Context)).Returns(true);55 conditionalEventHandlerMock.Setup(x => x.Handle(eventData, Context));56 Sut.Act(x => x.Publish(eventData));57 }58 [Test]59 public void WhenThereAreMultipleSubscriptions()60 {61 var actionMock1 = new Mock<Action<TestEvent>>(MockBehavior.Strict);62 var actionMock2 = new Mock<Action<TestEvent, AtataContext>>(MockBehavior.Strict);63 var eventHandlerMock1 = new Mock<IConditionalEventHandler<TestEvent>>(MockBehavior.Strict);64 var eventHandlerMock2 = new Mock<IEventHandler<TestEvent>>(MockBehavior.Strict);65 var eventData = new TestEvent();66 Sut.Object.Subscribe(actionMock1.Object);67 Sut.Object.Subscribe(actionMock2.Object);68 Sut.Object.Subscribe(eventHandlerMock1.Object);69 Sut.Object.Subscribe(eventHandlerMock2.Object);70 MockSequence sequence = new MockSequence();71 actionMock1.InSequence(sequence).Setup(x => x(eventData));72 actionMock2.InSequence(sequence).Setup(x => x(eventData, Context));73 eventHandlerMock1.InSequence(sequence).Setup(x => x.CanHandle(eventData, Context)).Returns(true);74 eventHandlerMock1.InSequence(sequence).Setup(x => x.Handle(eventData, Context));75 eventHandlerMock2.InSequence(sequence).Setup(x => x.Handle(eventData, Context));76 Sut.Act(x => x.Publish(eventData));77 }78 [Test]79 public void AfterUnsubscribe()80 {81 var actionMock1 = new Mock<Action<TestEvent>>();82 var actionMock2 = new Mock<Action<TestEvent, AtataContext>>();83 var eventData = new TestEvent();84 var subscription1 = Sut.Object.Subscribe(actionMock1.Object);85 Sut.Object.Subscribe(actionMock2.Object);86 Sut.Object.Unsubscribe(subscription1);87 Sut.Act(x => x.Publish(eventData));88 actionMock1.Verify(x => x(eventData), Times.Never);89 actionMock2.Verify(x => x(eventData, Context), Times.Once);90 }91 [Test]92 public void AfterUnsubscribeHandler()93 {94 var actionMock = new Mock<Action<TestEvent>>();95 var eventHandlerMock = new Mock<IEventHandler<TestEvent>>();96 var eventData = new TestEvent();97 Sut.Object.Subscribe(actionMock.Object);98 Sut.Object.Subscribe(eventHandlerMock.Object);99 Sut.Object.UnsubscribeHandler(eventHandlerMock.Object);100 Sut.Act(x => x.Publish(eventData));101 actionMock.Verify(x => x(eventData), Times.Once);102 eventHandlerMock.Verify(x => x.Handle(eventData, Context), Times.Never);103 }104 [Test]105 public void AfterUnsubscribeAll()106 {107 var actionMock1 = new Mock<Action<TestEvent>>();108 var actionMock2 = new Mock<Action<TestEvent, AtataContext>>();109 var eventData = new TestEvent();110 Sut.Object.Subscribe(actionMock1.Object);111 Sut.Object.Subscribe(actionMock2.Object);112 Sut.Object.UnsubscribeAll<TestEvent>();113 Sut.Act(x => x.Publish(eventData));114 actionMock1.Verify(x => x(eventData), Times.Never);115 actionMock2.Verify(x => x(eventData, Context), Times.Never);116 }117 }118 [TestFixture]119 public class Subscribe : EventBusTests120 {121 [Test]122 public void Action_Null() =>123 Sut.Invoking(x => x.Subscribe<TestEvent>(null as Action))124 .Should.Throw<ArgumentNullException>();125 [Test]126 public void Action()127 {128 var actionMock = new Mock<Action<TestEvent>>();129 Sut.ResultOf(x => x.Subscribe(actionMock.Object))130 .Should.Not.BeNull();131 }132 }133 [TestFixture]134 public class Unsubscribe : EventBusTests135 {136 [Test]137 public void Null() =>138 Sut.Invoking(x => x.Unsubscribe(null))139 .Should.Throw<ArgumentNullException>();140 [Test]141 public void Valid()142 {143 var actionMock = new Mock<Action<TestEvent>>();144 var subscription = Sut.Object.Subscribe(actionMock.Object);145 Sut.Invoking(x => x.Unsubscribe(subscription))146 .Should.Not.Throw();147 }148 [Test]149 public void Twice()150 {151 var actionMock = new Mock<Action<TestEvent>>();152 var subscription = Sut.Object.Subscribe(actionMock.Object);153 Sut.Act(x => x.Unsubscribe(subscription));154 Sut.Invoking(x => x.Unsubscribe(subscription))155 .Should.Not.Throw();156 }157 }158 [TestFixture]159 public class UnsubscribeHandler : EventBusTests160 {161 [Test]162 public void Null() =>163 Sut.Invoking(x => x.UnsubscribeHandler(null))164 .Should.Throw<ArgumentNullException>();165 [Test]166 public void Valid()167 {168 var actionMock = new Mock<IEventHandler<TestEvent>>();169 Sut.Object.Subscribe(actionMock.Object);170 Sut.Invoking(x => x.UnsubscribeHandler(actionMock.Object))171 .Should.Not.Throw();172 }173 [Test]...
EventBus.cs
Source:EventBus.cs
...6{7 /// <summary>8 /// Represents the event bus, which provides a functionality of subscribing to and publishing events.9 /// </summary>10 public class EventBus : IEventBus11 {12 private readonly AtataContext _context;13 private readonly ConcurrentDictionary<Type, List<EventHandlerSubscription>> _subscriptionMap = new ConcurrentDictionary<Type, List<EventHandlerSubscription>>();14 /// <summary>15 /// Initializes a new instance of the <see cref="EventBus"/> class.16 /// </summary>17 /// <param name="context">The context.</param>18 public EventBus(AtataContext context)19 : this(context, null)20 {21 }22 internal EventBus(AtataContext context, IEnumerable<EventSubscriptionItem> eventSubscriptions)23 {24 _context = context.CheckNotNull(nameof(context));25 if (eventSubscriptions != null)26 foreach (var subscription in eventSubscriptions)27 Subscribe(subscription.EventType, subscription.EventHandler);28 }29 /// <inheritdoc/>30 public void Publish<TEvent>(TEvent eventData)31 {32 eventData.CheckNotNull(nameof(eventData));33 if (_subscriptionMap.TryGetValue(typeof(TEvent), out var eventHandlerSubscriptions))34 {35 object[] eventHandlersArray;36 lock (eventHandlerSubscriptions)...
FileScreenshotConsumerBase.cs
Source:FileScreenshotConsumerBase.cs
...3233 screenshotInfo.Screenshot.SaveAsFile(filePath, ImageFormat);3435 AtataContext.Current.Log.Info($"Screenshot saved to file \"{filePath}\"");36 AtataContext.Current.EventBus.Publish(new ScreenshotFileSavedEvent(screenshotInfo, filePath));37 }3839 /// <summary>40 /// Builds the path of the file without the extension.41 /// </summary>42 /// <param name="screenshotInfo">The screenshot information.</param>43 /// <returns>The file path without the extension.</returns>44 protected abstract string BuildFilePath(ScreenshotInfo screenshotInfo);45 }46}
...
EventBus
Using AI Code Generation
1using Atata;2using NUnit.Framework;3using OpenQA.Selenium;4using OpenQA.Selenium.Chrome;5using System;6using System.Collections.Generic;7using System.Linq;8using System.Text;9using System.Threading.Tasks;10{11 {12 public void _2()13 {14 AtataContext.Configure()15 .UseChrome()16 .UseCulture("en-US")17 .UseAllNUnitFeatures()18 .AddNUnitTestContextLogging()19 .Build();20 using (AtataContext.Begin())21 {22 Go.To<GooglePage>()23 .SearchFor("Atata")24 .Results.Should.Contain("Atata Framework");25 }26 }27 }28 {29 [FindByClass("gLFyf")]30 public TextInput<_> Search { get; private set; }31 [FindByClass("gNO89b")]32 public ButtonDelegate<_> SearchButton { get; private set; }33 [FindByClass("srg")]34 public ControlList<SearchResultItem, _> Results { get; private set; }35 public _ SearchFor(string term)36 {37 return Search.Set(term).SearchButton.Click();38 }39 }40 {41 public LinkDelegate<_> Title { get; private set; }42 }43}44using Atata;45using NUnit.Framework;46using OpenQA.Selenium;47using OpenQA.Selenium.Chrome;48using System;49using System.Collections.Generic;50using System.Linq;51using System.Text;52using System.Threading.Tasks;53{54 {55 public void _3()56 {57 AtataContext.Configure()58 .UseChrome()59 .UseCulture("en-US")60 .UseAllNUnitFeatures()61 .AddNUnitTestContextLogging()62 .Build();63 using (AtataContext.Begin())64 {65 Go.To<GooglePage>()66 .SearchFor("Atata")67 .Results.Should.Contain("Atata Framework");68 }69 }70 }
EventBus
Using AI Code Generation
1using Atata;2using NUnit.Framework;3{4 {5 public void TestMethod1()6 {7 Atata.EventBus.Subscribe<LogEvent>(e =>8 {9 if (e.Level == LogLevel.Trace)10 TestContext.Out.WriteLine($"[{e.Level}] {e.Message}");11 TestContext.Out.WriteLine($"[{e.Level}] {e.Message} ({e.Exception.Message})");12 });13 Atata.EventBus.Subscribe<LogEvent>(e =>14 {15 TestContext.Out.WriteLine($"[{e.Level}] {e.Message}");16 });17 Go.To<HomePage>();18 }19 }20}
EventBus
Using AI Code Generation
1Atata.EventBus.Subscribe<SomeEvent>(e => 2{3});4Atata.EventBus.Unsubscribe<SomeEvent>(e => 5{6});7Atata.EventBus.Subscribe<SomeEvent>(e =>8{9});10Atata.EventBus.Unsubscribe<SomeEvent>(e =>11{12});13Atata.EventBus.Subscribe<SomeEvent>(e =>14{15});16Atata.EventBus.Unsubscribe<SomeEvent>(e =>17{18});19Atata.EventBus.Subscribe<SomeEvent>(e =>20{21});22Atata.EventBus.Unsubscribe<SomeEvent>(e =>23{24});25Atata.EventBus.Subscribe<SomeEvent>(e =>26{27});28Atata.EventBus.Unsubscribe<SomeEvent>(e =>29{30});31Atata.EventBus.Subscribe<SomeEvent>(e =>32{33});34Atata.EventBus.Unsubscribe<SomeEvent>(e =>35{36});
EventBus
Using AI Code Generation
1{2 public void Test()3 {4 Go.To<HomePage>()5 .SearchFor("Atata")6 .Results.Should.Contain(x => x.Title.Should.Contain("Atata"));7 }8}9using Atata.Logging;10{11 public void Test()12 {13 Go.To<HomePage>()14 .SearchFor("Atata")15 .Results.Should.Contain(x => x.Title.Should.Contain("Atata"));16 }17}18using Atata.Logging;19{20 public void Test()21 {22 Go.To<HomePage>()23 .SearchFor("Atata")24 .Results.Should.Contain(x => x.Title.Should.Contain("Atata"));25 }26}27using Atata.Logging;28{29 public void Test()30 {31 Go.To<HomePage>()32 .SearchFor("Atata")33 .Results.Should.Contain(x => x.Title.Should.Contain("Atata"));34 }35}36using Atata.Logging;37{38 public void Test()39 {40 Go.To<HomePage>()41 .SearchFor("Atata")42 .Results.Should.Contain(x => x.Title.Should.Contain("Atata"));43 }44}45using Atata.Logging;46{47 public void Test()48 {49 Go.To<HomePage>()50 .SearchFor("Atata")
EventBus
Using AI Code Generation
1Atata.EventBus.On<LogEvent>(logEvent =>2{3 Console.WriteLine(logEvent.Message);4});5AtataContext.Log("Some log message");6AtataContext.Log("Some log message");7AtataContext.Log("Some log message");8AtataContext.Log("Some log message");9AtataContext.Log("Some log message");10AtataContext.Log("Some log message");11AtataContext.Log("Some log message");12AtataContext.Log("Some log message");13AtataContext.Log("Some log message");14AtataContext.Log("Some log message");15AtataContext.Log("Some log message");16AtataContext.Log("Some log message");17AtataContext.Log("Some log message");18AtataContext.Log("Some log message");19AtataContext.Log("Some log message");20AtataContext.Log("Some log message");21AtataContext.Log("Some log message");22AtataContext.Log("Some log message");
EventBus
Using AI Code Generation
1public void TestMethod()2{3 Atata.EventBus.Subscribe<LogEvent>(logEvent => _output.WriteLine(logEvent.Message));4 Go.To<HomePage>();5}6public void TestMethod()7{8 AtataContext.SubscribeTo<LogEvent>(logEvent => _output.WriteLine(logEvent.Message));9 Go.To<HomePage>();10}11public void TestMethod()12{13 AtataContext.Current.On<LogEvent>(logEvent => _output.WriteLine(logEvent.Message));14 Go.To<HomePage>();15}16public void TestMethod()17{18 AtataContext.Current.Log.On(logEvent => _output.WriteLine(logEvent.Message));19 Go.To<HomePage>();20}21public void TestMethod()22{23 AtataContext.Current.Log.OnInfo(logEvent => _output.WriteLine(logEvent.Message));24 Go.To<HomePage>();25}26public void TestMethod()27{28 AtataContext.Current.Log.OnInfo().WriteLineTo(_output);29 Go.To<HomePage>();30}31public void TestMethod()32{33 AtataContext.Current.Log.OnInfo().WriteLineTo(_output);34 Go.To<HomePage>();35}36public void TestMethod()37{
EventBus
Using AI Code Generation
1{2 {3 public void Test()4 {5 var eventBus = Atata.EventBus;6 eventBus.Register<MyEvent>(e => { Console.WriteLine("Received event"); });7 eventBus.Publish(new MyEvent());8 eventBus.Unregister<MyEvent>();9 }10 }11}12{13 {14 public void Test()15 {16 var eventBus = Atata.EventBus;17 eventBus.Register<MyEvent>(e => { Console.WriteLine("Received event"); });18 eventBus.Publish(new MyEvent());19 eventBus.Unregister<MyEvent>();20 }21 }22}23{24 {25 public void Test()26 {27 var eventBus = Atata.EventBus;28 eventBus.Register<MyEvent>(e => { Console.WriteLine("Received event"); });29 eventBus.Publish(new MyEvent());30 eventBus.Unregister<MyEvent>();31 }32 }33}34{35 {36 public void Test()37 {38 var eventBus = Atata.EventBus;39 eventBus.Register<MyEvent>(e => { Console.WriteLine("Received event"); });40 eventBus.Publish(new MyEvent());41 eventBus.Unregister<MyEvent>();42 }43 }44}45{46 {47 public void Test()48 {49 var eventBus = Atata.EventBus;
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!