Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Draft
nvborisenko wants to merge 24 commits into SeleniumHQ:trunk
base: trunk
Choose a base branch
Loading
from nvborisenko:dotnet-bidi-browsingcontext
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7d92ca5
Some tests for browsing context
nvborisenko Sep 10, 2024
34a0c78
More examples
nvborisenko Sep 10, 2024
2b3816b
Update BrowsingContextTest.FragmentNavigatedEvent.cs
nvborisenko Sep 10, 2024
d23ff62
Task completion source for events
nvborisenko Sep 10, 2024
a6b1e44
Add more
nvborisenko Sep 12, 2024
2a8aa0b
Get tree
nvborisenko Sep 14, 2024
1ba4589
Group events
nvborisenko Sep 14, 2024
2e02cc2
Finish examples
nvborisenko Sep 14, 2024
cf93f36
Simplify browsing context
nvborisenko Sep 14, 2024
a108d99
Add example how to print 7.. pages
nvborisenko Sep 14, 2024
adda703
Simplify browsing context type
nvborisenko Sep 15, 2024
12f9167
BiDi is exposed from browsing context
nvborisenko Sep 15, 2024
0cd9dc3
Consistent AsBiDirectional naming
nvborisenko Sep 16, 2024
c3d85b9
Make it even shorter
nvborisenko Sep 16, 2024
0e4bd13
Use stable selenium
nvborisenko Sep 20, 2024
04704a1
Do no show that BiDi object is disposable
nvborisenko Sep 20, 2024
a5bf483
No sandbox to be able to print a page
nvborisenko Sep 20, 2024
90aa4c1
Upgrade
nvborisenko Oct 31, 2024
8e9763c
Merge remote-tracking branch 'upstream/trunk' into dotnet-bidi-browsi...
nvborisenko Dec 14, 2024
6ef220a
Simplify capture element screenshot
nvborisenko Dec 14, 2024
7b0b273
Merge remote-tracking branch 'upstream/trunk' into dotnet-bidi-browsi...
nvborisenko Mar 22, 2025
b93f3c2
Actualize to 4.30
nvborisenko Mar 22, 2025
9a9bbd7
Break some of them
nvborisenko Mar 22, 2025
f121c9e
Simplify
nvborisenko Mar 22, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/dotnet/SeleniumDocs/BaseTest.cs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ protected void StartDriver(string browserVersion = "stable")
{
ChromeOptions options = new ChromeOptions
{
BrowserVersion = browserVersion
BrowserVersion = browserVersion,
UseWebSocketUrl = true,
};

options.AddArgument("--no-sandbox");

driver = new ChromeDriver(options);
}

Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

partial class BrowsingContextTest
{
[TestMethod]
public async Task Activate()
{
await context.ActivateAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

partial class BrowsingContextTest
{
[TestMethod]
public async Task CaptureScreenshot()
{
var screenshot = await context.CaptureScreenshotAsync();

Assert.IsNotNull(screenshot);
Assert.IsNotNull(screenshot.Data);
Assert.IsNotNull(screenshot.ToByteArray());
}

[TestMethod]
public async Task CaptureViewportScreenshot()
{
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()
{
driver.Url = "https://www.selenium.dev/selenium/web/formPage.html";

var element = (await context.LocateNodesAsync(new CssLocator("#checky")))[0];

var screenshot = await context.CaptureScreenshotAsync(new() { Clip = new ElementClipRectangle(element) });

Assert.IsNotNull(screenshot);
Assert.IsNotNull(screenshot.Data);
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

partial class BrowsingContextTest
{
[TestMethod]
public async Task CloseTab()
{
var context = await bidi.BrowsingContext.CreateAsync(ContextType.Tab);

await context.CloseAsync();
}

[TestMethod]
public async Task CloseWindow()
{
var context = await bidi.BrowsingContext.CreateAsync(ContextType.Window);

await context.CloseAsync();
}
}
View file Open in desktop
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()
{
var bidi = await driver.AsBiDiAsync();

var context = await bidi.BrowsingContext.CreateAsync(ContextType.Tab);

Assert.IsNotNull(context);
}

[TestMethod]
public async Task OpenNewWindow()
{
var bidi = await driver.AsBiDiAsync();

var context = await bidi.BrowsingContext.CreateAsync(ContextType.Window);

Assert.IsNotNull(context);
}

[TestMethod]
public async Task OpenTabWithReferenceBrowsingContext()
{
var context1 = context;

var context2 = await context1.BiDi.BrowsingContext.CreateAsync(ContextType.Tab, new() { ReferenceContext = context1 });

Assert.IsNotNull(context2);
}

[TestMethod]
public async Task OpenWindowWithReferenceBrowsingContext()
{
var context1 = context;

var context2 = await context1.BiDi.BrowsingContext.CreateAsync(ContextType.Window, new() { ReferenceContext = context1 });

Assert.IsNotNull(context2);
}

[TestMethod]
public async Task UseExistingWindowHandle()
{
var context = (await bidi.BrowsingContext.GetTreeAsync())[0].Context;

Assert.IsNotNull(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

partial class BrowsingContextTest
{
[TestMethod]
public async Task BrowsingContextCreatedEvent()
{
TaskCompletionSource<BrowsingContextInfo> tcs = new();

await bidi.BrowsingContext.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,25 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

partial class BrowsingContextTest
{
[TestMethod]
public async Task BrowsingContextDestroyedEvent()
{
TaskCompletionSource<BrowsingContextInfo> tcs = new();

await bidi.BrowsingContext.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,24 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

partial class BrowsingContextTest
{
[TestMethod]
public async Task BrowsingContextLoadedEvent()
{
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,24 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

partial class BrowsingContextTest
{
[TestMethod]
public async Task DomContentLoadedEvent()
{
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,26 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

partial class BrowsingContextTest
{
[TestMethod]
public async Task FragmentNavigatedEvent()
{
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,23 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

partial class BrowsingContextTest
{
[TestMethod]
public async Task NavigationStartedEvent()
{
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);
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

partial class BrowsingContextTest
{
[TestMethod]
public async Task UserPromptOpenedEvent()
{
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 bidi.BrowsingContext.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()
{
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 bidi.BrowsingContext.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);
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 GetBrowsingContextTree()
{
await context.NavigateAsync("https://www.selenium.dev/selenium/web/iframes.html", new() { Wait = ReadinessState.Complete });

var contexts = await context.GetTreeAsync();

Assert.AreEqual(1, contexts.Count);
Assert.IsNotNull(contexts[0].Children);
Assert.IsTrue(contexts[0].Children.Count >= 1, "Context should contain iframes as children");
}

[TestMethod]
public async Task GetBrowsingContextTreeWithDepth()
{
await context.NavigateAsync("https://www.selenium.dev/selenium/web/iframes.html", new() { Wait = ReadinessState.Complete });

var contexts = await context.GetTreeAsync(new() { MaxDepth = 0 });

Assert.AreEqual(1, contexts.Count);
Assert.IsNull(contexts[0].Children, "Context should not contain iframes as children since depth is 0");
}

[TestMethod]
public async Task GetAllTopLevelBrowingContexts()
{
var window = await bidi.BrowsingContext.CreateAsync(ContextType.Window);

var contexts = await bidi.BrowsingContext.GetTreeAsync();

Assert.AreEqual(2, contexts.Count);
Assert.AreEqual(contexts[1].Context, window);
}
}
Loading
Loading

AltStyle によって変換されたページ (->オリジナル) /