- 
 
- 
  Notifications
 You must be signed in to change notification settings 
- Fork 1.5k
[dotnet] Add examples for BiDi W3C Browsing Context #1940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
7d92ca5
 34a0c78
 2b3816b
 d23ff62
 a6b1e44
 2a8aa0b
 1ba4589
 2e02cc2
 cf93f36
 a108d99
 adda703
 12f9167
 0cd9dc3
 c3d85b9
 0e4bd13
 04704a1
 a5bf483
 90aa4c1
 8e9763c
 6ef220a
 7b0b273
 b93f3c2
 9a9bbd7
 f121c9e
 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using OpenQA.Selenium.BiDi; | ||
| using System.Threading.Tasks; | ||
|  | ||
| namespace SeleniumDocs.BiDi.BrowsingContext; | ||
|  | ||
| partial class BrowsingContextTest | ||
| { | ||
| [TestMethod] | ||
| public async Task Activate() | ||
| { | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| await context.ActivateAsync(); | ||
| } | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using OpenQA.Selenium.BiDi; | ||
| using OpenQA.Selenium.BiDi.Modules.BrowsingContext; | ||
| using System.Threading.Tasks; | ||
|  | ||
| namespace SeleniumDocs.BiDi.BrowsingContext; | ||
|  | ||
| partial class BrowsingContextTest | ||
| { | ||
| [TestMethod] | ||
| public async Task CaptureScreenshot() | ||
| { | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| var screenshot = await context.CaptureScreenshotAsync(); | ||
|  | ||
| Assert.IsNotNull(screenshot); | ||
| Assert.IsNotNull(screenshot.Data); | ||
| Assert.IsNotNull(screenshot.ToByteArray()); | ||
| } | ||
|  | ||
| [TestMethod] | ||
| public async Task CaptureViewportScreenshot() | ||
| { | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| var screenshot = await context.CaptureScreenshotAsync(new() { Clip = new BoxClipRectangle(5, 5, 10, 10) }); | ||
|  | ||
| Assert.IsNotNull(screenshot); | ||
| Assert.IsNotNull(screenshot.Data); | ||
| } | ||
|  | ||
| [TestMethod] | ||
| public async Task CaptureElementScreenshot() | ||
| { | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| driver.Url = "https://www.selenium.dev/selenium/web/formPage.html"; | ||
|  | ||
| var element = (await context.LocateNodesAsync(Locator.Css("#checky")))[0]; | ||
|  | ||
| //TODO: ShareId is a type, not string | ||
|   | ||
| var screenshot = await context.CaptureScreenshotAsync(new() { Clip = new ElementClipRectangle(new(element.SharedId)) }); | ||
|  | ||
| Assert.IsNotNull(screenshot); | ||
| Assert.IsNotNull(screenshot.Data); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using OpenQA.Selenium.BiDi; | ||
| using OpenQA.Selenium.BiDi.Modules.BrowsingContext; | ||
| using System.Threading.Tasks; | ||
|  | ||
| namespace SeleniumDocs.BiDi.BrowsingContext; | ||
|  | ||
| partial class BrowsingContextTest | ||
| { | ||
| [TestMethod] | ||
| public async Task CloseTab() | ||
| { | ||
| await using var bidi = await driver.AsBidirectionalAsync(); | ||
|  | ||
| var context = await bidi.CreateContextAsync(ContextType.Tab); | ||
|  | ||
| await context.CloseAsync(); | ||
| } | ||
|  | ||
| [TestMethod] | ||
| public async Task CloseWindow() | ||
| { | ||
| await using var bidi = await driver.AsBidirectionalAsync(); | ||
|  | ||
| var context = await bidi.CreateContextAsync(ContextType.Window); | ||
|  | ||
| await context.CloseAsync(); | ||
| } | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using OpenQA.Selenium.BiDi; | ||
| using OpenQA.Selenium.BiDi.Modules.BrowsingContext; | ||
| using System.Threading.Tasks; | ||
|  | ||
| namespace SeleniumDocs.BiDi.BrowsingContext; | ||
|  | ||
| partial class BrowsingContextTest | ||
| { | ||
| [TestMethod] | ||
| public async Task OpenNewTab() | ||
| { | ||
| await using var bidi = await driver.AsBidirectionalAsync(); | ||
|  | ||
| var context = await bidi.CreateContextAsync(ContextType.Tab); | ||
|  | ||
| Assert.IsNotNull(context); | ||
| } | ||
|  | ||
| [TestMethod] | ||
| public async Task OpenNewWindow() | ||
| { | ||
| await using var bidi = await driver.AsBidirectionalAsync(); | ||
|  | ||
| var context = await bidi.CreateContextAsync(ContextType.Window); | ||
|  | ||
| Assert.IsNotNull(context); | ||
| } | ||
|  | ||
| [TestMethod] | ||
| public async Task OpenTabWithReferenceBrowsingContext() | ||
| { | ||
| var context1 = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| var context2 = await context1.BiDi.CreateContextAsync(ContextType.Tab, new() { ReferenceContext = context1 }); | ||
|   | ||
|  | ||
| Assert.IsNotNull(context2); | ||
| } | ||
|  | ||
| [TestMethod] | ||
| public async Task OpenWindowWithReferenceBrowsingContext() | ||
| { | ||
| var context1 = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| var context2 = await context1.BiDi.CreateContextAsync(ContextType.Window, new() { ReferenceContext = context1 }); | ||
|  | ||
| Assert.IsNotNull(context2); | ||
| } | ||
|  | ||
| [TestMethod] | ||
| public async Task UseExistingWindowHandle() | ||
| { | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| Assert.IsNotNull(context); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| 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; | ||
|  | ||
| partial class BrowsingContextTest | ||
| { | ||
| [TestMethod] | ||
| public async Task BrowsingContextCreatedEvent() | ||
| { | ||
| await using var bidi = await driver.AsBidirectionalAsync(); | ||
|  | ||
| TaskCompletionSource<BrowsingContextInfo> tcs = new(); | ||
|  | ||
| await bidi.OnContextCreatedAsync(tcs.SetResult); | ||
|  | ||
| driver.SwitchTo().NewWindow(OpenQA.Selenium.WindowType.Window); | ||
|  | ||
| var info = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5)); | ||
|  | ||
| Assert.IsNotNull(info); | ||
| Console.WriteLine(info); | ||
| } | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 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; | ||
|  | ||
| partial class BrowsingContextTest | ||
| { | ||
| [TestMethod] | ||
| public async Task BrowsingContextDestroyedEvent() | ||
| { | ||
| await using var bidi = await driver.AsBidirectionalAsync(); | ||
|  | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| TaskCompletionSource<BrowsingContextInfo> tcs = new(); | ||
|  | ||
| await bidi.OnContextDestroyedAsync(tcs.SetResult); | ||
|  | ||
| await context.CloseAsync(); | ||
|  | ||
| var contextInfo = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5)); | ||
|  | ||
| Assert.IsNotNull(contextInfo); | ||
| Assert.AreEqual(context, contextInfo.Context); | ||
| Console.WriteLine(contextInfo); | ||
| } | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| 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; | ||
|  | ||
| partial class BrowsingContextTest | ||
| { | ||
| [TestMethod] | ||
| public async Task BrowsingContextLoadedEvent() | ||
| { | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| TaskCompletionSource<NavigationInfo> tcs = new(); | ||
|  | ||
| await context.OnLoadAsync(tcs.SetResult); | ||
|  | ||
| await context.NavigateAsync("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html", new() { Wait = ReadinessState.Complete }); | ||
|  | ||
| var navigationInfo = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5)); | ||
|  | ||
| Assert.IsNotNull(navigationInfo); | ||
| Console.WriteLine(navigationInfo); | ||
| } | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| 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; | ||
|  | ||
| partial class BrowsingContextTest | ||
| { | ||
| [TestMethod] | ||
| public async Task DomContentLoadedEvent() | ||
| { | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| TaskCompletionSource<NavigationInfo> tcs = new(); | ||
|  | ||
| await context.OnDomContentLoadedAsync(tcs.SetResult); | ||
|  | ||
| await context.NavigateAsync("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html", new() { Wait = ReadinessState.Complete }); | ||
|  | ||
| var navigationInfo = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5)); | ||
|  | ||
| Assert.IsNotNull(navigationInfo); | ||
| Console.WriteLine(navigationInfo); | ||
| } | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| 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; | ||
|  | ||
| partial class BrowsingContextTest | ||
| { | ||
| [TestMethod] | ||
| public async Task FragmentNavigatedEvent() | ||
| { | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| await context.NavigateAsync("https://www.selenium.dev/selenium/web/linked_image.html", new() { Wait = ReadinessState.Complete }); | ||
|  | ||
| TaskCompletionSource<NavigationInfo> tcs = new(); | ||
|  | ||
| await context.OnFragmentNavigatedAsync(tcs.SetResult); | ||
|  | ||
| await context.NavigateAsync("https://www.selenium.dev/selenium/web/linked_image.html#linkToAnchorOnThisPage", new() { Wait = ReadinessState.Complete }); | ||
|  | ||
| var navigationInfo = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5)); | ||
|  | ||
| Assert.IsNotNull(navigationInfo); | ||
| Console.WriteLine(navigationInfo); | ||
| } | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| 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; | ||
|  | ||
| partial class BrowsingContextTest | ||
| { | ||
| [TestMethod] | ||
| public async Task NavigationStartedEvent() | ||
| { | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| TaskCompletionSource<NavigationInfo> tcs = new(); | ||
|  | ||
| await context.OnNavigationStartedAsync(tcs.SetResult); | ||
|  | ||
| await context.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); | ||
| } | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using OpenQA.Selenium; | ||
| using OpenQA.Selenium.BiDi; | ||
| using OpenQA.Selenium.BiDi.Modules.BrowsingContext; | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|  | ||
| namespace SeleniumDocs.BiDi.BrowsingContext; | ||
|  | ||
| partial class BrowsingContextTest | ||
| { | ||
| [TestMethod] | ||
| public async Task UserPromptOpenedEvent() | ||
| { | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| TaskCompletionSource<UserPromptOpenedEventArgs> tcs = new(); | ||
|  | ||
| await context.NavigateAsync("https://www.selenium.dev/selenium/web/alerts.html", new() { Wait = ReadinessState.Complete }); | ||
|  | ||
| //TODO; THhis event can be a part of context | ||
| await context.BiDi.OnUserPromptOpenedAsync(tcs.SetResult); | ||
|  | ||
| driver.FindElement(By.Id("prompt")).Click(); | ||
|  | ||
| var userPromptOpenedEventArgs = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5)); | ||
|  | ||
| Assert.IsNotNull(userPromptOpenedEventArgs); | ||
| Console.WriteLine(userPromptOpenedEventArgs); | ||
| } | ||
|  | ||
| [TestMethod] | ||
| public async Task UserPromptClosedEvent() | ||
| { | ||
| var context = await driver.AsBidirectionalContextAsync(); | ||
|  | ||
| TaskCompletionSource<UserPromptClosedEventArgs> tcs = new(); | ||
|  | ||
| await context.NavigateAsync("https://www.selenium.dev/selenium/web/alerts.html", new() { Wait = ReadinessState.Complete }); | ||
|  | ||
| //TODO; THhis event can be a part of context | ||
| await context.BiDi.OnUserPromptClosedAsync(tcs.SetResult); | ||
|  | ||
| driver.FindElement(By.Id("prompt")).Click(); | ||
|  | ||
| //await context.HandleUserPromptAsync(); | ||
|  | ||
| var userPromptClosedEventArgs = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5)); | ||
|  | ||
| Assert.IsNotNull(userPromptClosedEventArgs); | ||
| Console.WriteLine(userPromptClosedEventArgs); | ||
| } | ||
| } |