Skip to content
1 change: 1 addition & 0 deletions examples/dotnet/SeleniumDocs/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void Cleanup()
protected void StartDriver(string browserVersion = null)
{
ChromeOptions options = new ChromeOptions();
options.UseWebSocketUrl = true;
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
if (browserVersion != null)
{
options.BrowserVersion = browserVersion;
Expand Down
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
Comment thread
diemol marked this conversation as resolved.
{
[TestMethod]
public async Task Activate()
{
await context.ActivateAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.BiDi.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";
Comment thread
qodo-code-review[bot] marked this conversation as resolved.

var nodes = (await context.LocateNodesAsync(new CssLocator("#checky"))).Nodes;

Assert.IsTrue(nodes.Length > 0, "Expected to locate #checky");

var screenshot = await context.CaptureScreenshotAsync(new() { Clip = new ElementClipRectangle(nodes[0]) });

Assert.IsNotNull(screenshot);
Assert.IsNotNull(screenshot.Data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.BiDi.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)).Context;

await context.CloseAsync();
}

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

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.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)).Context;

Assert.IsNotNull(context);
}

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

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

Assert.IsNotNull(context);
}

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

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

Assert.IsNotNull(context2);
}

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

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

Assert.IsNotNull(context2);
}

[TestMethod]
public async Task UseExistingWindowHandle()
{
var context = (await bidi.BrowsingContext.GetTreeAsync()).Contexts[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.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

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

await bidi.BrowsingContext.ContextCreated.SubscribeAsync(args => tcs.TrySetResult(args));

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.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

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

await bidi.BrowsingContext.ContextDestroyed.SubscribeAsync(args => tcs.TrySetResult(args));
Comment thread
diemol marked this conversation as resolved.

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.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

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

await context.Load.SubscribeAsync(args => tcs.TrySetResult(args));

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.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

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

await context.DomContentLoaded.SubscribeAsync(args => tcs.TrySetResult(args));

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.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<FragmentNavigatedEventArgs> tcs = new();

await context.FragmentNavigated.SubscribeAsync(args => tcs.TrySetResult(args));

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.BrowsingContext;
using System;
using System.Threading.Tasks;

namespace SeleniumDocs.BiDi.BrowsingContext;

partial class BrowsingContextTest
{
[TestMethod]
public async Task NavigationStartedEvent()
{
TaskCompletionSource<NavigationStartedEventArgs> tcs = new();
Comment thread
qodo-code-review[bot] marked this conversation as resolved.

await context.NavigationStarted.SubscribeAsync(args => tcs.TrySetResult(args));

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,61 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.BiDi;
using OpenQA.Selenium.BiDi.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: this event can be a part of context
await bidi.BrowsingContext.UserPromptOpened.SubscribeAsync(args => tcs.TrySetResult(args));

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()
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
{
TaskCompletionSource<UserPromptOpenedEventArgs> opened = new();
TaskCompletionSource<UserPromptClosedEventArgs> closed = new();

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

// TODO: these events can be a part of context
await bidi.BrowsingContext.UserPromptOpened.SubscribeAsync(args => opened.TrySetResult(args));
await bidi.BrowsingContext.UserPromptClosed.SubscribeAsync(args => closed.TrySetResult(args));

driver.FindElement(By.Id("prompt")).Click();

await opened.Task.WaitAsync(TimeSpan.FromSeconds(5));

try
{
// Chrome's default unhandled prompt behavior may already have closed the
// prompt by this point; tolerate that instead of treating it as a failure.
await context.HandleUserPromptAsync(new() { Accept = true });
}
catch (BiDiException ex) when (ex.Message.Contains("no such alert"))
{
}

var userPromptClosedEventArgs = await closed.Task.WaitAsync(TimeSpan.FromSeconds(5));

Assert.IsNotNull(userPromptClosedEventArgs);
Console.WriteLine(userPromptClosedEventArgs);
}
}
Loading
Loading