Skip to content
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

[dotnet] Add examples for BiDi W3C Browsing Context #1940

Draft
wants to merge 18 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
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.AsBiDiContextAsync();

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.AsBiDiContextAsync();

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.AsBiDiContextAsync();

var screenshot = await context.CaptureScreenshotAsync(new() { Clip = new ClipRectangle.Box(5, 5, 10, 10) });

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

[TestMethod]
public async Task CaptureElementScreenshot()
{
var context = await driver.AsBiDiContextAsync();

driver.Url = "https://www.selenium.dev/selenium/web/formPage.html";

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

//TODO: ShareId is a type, not string
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we can implicitly convert RemoteNodeValue (if I remember correctly) to SharedId.

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

And moving forfard:

await context.CaptureScreenshotAsync(new() { Clip = ClipRectangle.Element(element) });

var screenshot = await context.CaptureScreenshotAsync(new() { Clip = new ClipRectangle.Element(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()
{
var bidi = await driver.AsBiDiAsync();

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

await context.CloseAsync();
}

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

var context = await bidi.BrowsingContext.CreateAsync(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()
{
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 = await driver.AsBiDiContextAsync();

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

Assert.IsNotNull(context2);
}

[TestMethod]
public async Task OpenWindowWithReferenceBrowsingContext()
{
var context1 = await driver.AsBiDiContextAsync();

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

Assert.IsNotNull(context2);
}

[TestMethod]
public async Task UseExistingWindowHandle()
{
var context = await driver.AsBiDiContextAsync();

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()
{
var bidi = await driver.AsBiDiAsync();

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,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()
{
var bidi = await driver.AsBiDiAsync();

var context = await driver.AsBiDiContextAsync();

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,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.AsBiDiContextAsync();

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.AsBiDiContextAsync();

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.AsBiDiContextAsync();

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.AsBiDiContextAsync();

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.AsBiDiContextAsync();

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.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()
{
var context = await driver.AsBiDiContextAsync();

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.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);
}
}
Loading