Skip to content

Commit

Permalink
Task completion source for events
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko committed Sep 10, 2024
1 parent 2b3816b commit d23ff62
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public async Task BrowsingContextCreatedEvent()
{
await using var bidi = await driver.AsBidirectionalAsync();

BrowsingContextInfo info = null;
TaskCompletionSource<BrowsingContextInfo> tcs = new();

await bidi.OnBrowsingContextCreatedAsync(e => info = e);
await bidi.OnBrowsingContextCreatedAsync(tcs.SetResult);

driver.SwitchTo().NewWindow(OpenQA.Selenium.WindowType.Window);

var info = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));

Assert.IsNotNull(info);
Console.WriteLine(info);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.BiDi;
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;
Expand All @@ -12,12 +13,14 @@ public async Task NavigationStartedEvent()
{
var browsingContext = await driver.AsBidirectionalContextAsync();

NavigationInfo info = null;
TaskCompletionSource<NavigationInfo> tcs = new();

await browsingContext.OnNavigationStartedAsync(e => info = e);
await browsingContext.OnNavigationStartedAsync(tcs.SetResult);

await browsingContext.NavigateAsync("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html", new() { Wait = ReadinessState.Complete });

var info = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5));

Assert.IsNotNull(info);
}
}

0 comments on commit d23ff62

Please sign in to comment.