Skip to content

Commit

Permalink
Upgrade to version 2.0.2
Browse files Browse the repository at this point in the history
- Improved DOM support for HtmlTable and strongly typed QuerySelectorAllAsync
  • Loading branch information
amaitland committed Apr 11, 2022
1 parent d7e2039 commit ba3214a
Show file tree
Hide file tree
Showing 17 changed files with 782 additions and 29 deletions.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ coreWebView2.NavigationCompleted += async (sender, args) =>
var htmlTextAreaElement = await devToolsContext.QuerySelectorAsync<HtmlImageElement>("#myTextAreaElementId");
var htmlButtonElement = await devToolsContext.QuerySelectorAsync<HtmlButtonElement>("#myButtonElementId");
var htmlParagraphElement = await devToolsContext.QuerySelectorAsync<HtmlParagraphElement>("#myParagraphElementId");
var htmlTableElement = await devToolsContext.QuerySelectorAsync<HtmlTableElement>("#myTableElementId");

// Get a custom attribute value
var customAttribute = await element.GetAttributeAsync<string>("data-customAttribute");
Expand Down Expand Up @@ -134,7 +135,7 @@ coreWebView2.NavigationCompleted += async (sender, args) =>
_ = jsAlertButton.AddEventListenerAsync("click", "jsAlertButtonClick");

//Get a collection of HtmlElements
var divElements = await devToolsContext.QuerySelectorAllAsync("div");
var divElements = await devToolsContext.QuerySelectorAllAsync<HtmlDivElement>("div");

foreach (var div in divElements)
{
Expand All @@ -147,10 +148,39 @@ coreWebView2.NavigationCompleted += async (sender, args) =>
await div.SetAttributeAsync("data-customAttribute", "123");
await div.SetInnerTextAsync("Updated Div innerText");
}

//Using standard array
var tableRows = await htmlTableElement.GetRowsAsync().ToArrayAsync();

foreach(var row in tableRows)
{
var cells = await row.GetCellsAsync().ToArrayAsync();
foreach(var cell in cells)
{
var newDiv = await devToolsContext.CreateHtmlElementAsync<HtmlDivElement>("div");
await newDiv.SetInnerTextAsync("New Div Added!");
await cell.AppendChildAsync(newDiv);
}
}

//Get a reference to the HtmlCollection and use async enumerable
//Requires Net Core 3.1 or higher
var tableRowsHtmlCollection = await htmlTableElement.GetRowsAsync();

await foreach (var row in tableRowsHtmlCollection)
{
var cells = await row.GetCellsAsync();
await foreach (var cell in cells)
{
var newDiv = await devToolsContext.CreateHtmlElementAsync<HtmlDivElement>("div");
await newDiv.SetInnerTextAsync("New Div Added!");
await cell.AppendChildAsync(newDiv);
}
}
}
};
```
<sup><a href='/WebView2.DevTools.Dom.Tests/QuerySelectorTests/DevToolsContextQuerySelectorTests.cs#L20-L112' title='Snippet source file'>snippet source</a> | <a href='#snippet-queryselector' title='Start of snippet'>anchor</a></sup>
<sup><a href='/WebView2.DevTools.Dom.Tests/QuerySelectorTests/DevToolsContextQuerySelectorTests.cs#L20-L142' title='Snippet source file'>snippet source</a> | <a href='#snippet-queryselector' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Inject HTML
Expand Down
10 changes: 5 additions & 5 deletions WebView2.DevTools.Dom.Tests/ClickTests/ClickTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public async Task ShouldSelectTheTextByTripleClicking()

await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/input/textarea.html");
await DevToolsContext.FocusAsync("textarea");

await DevToolsContext.Keyboard.TypeAsync(expected);
await DevToolsContext.ClickAsync("textarea");
await DevToolsContext.ClickAsync("textarea", new ClickOptions { ClickCount = 2 });
Expand Down Expand Up @@ -221,7 +221,7 @@ await DevToolsContext.EvaluateExpressionAsync(@"{
window.double = true;
});
}");
var button = await DevToolsContext.QuerySelectorAsync("button");
var button = await DevToolsContext.QuerySelectorAsync<HtmlButtonElement>("button");
await button.ClickAsync(new ClickOptions { ClickCount = 2 });
Assert.True(await DevToolsContext.EvaluateExpressionAsync<bool>("double"));
Assert.Equal("Clicked", await DevToolsContext.EvaluateExpressionAsync<string>("result"));
Expand Down Expand Up @@ -273,7 +273,7 @@ public async Task ShouldClickTheButtonInsideAnIframe()
await DevToolsContext.SetContentAsync("<div style=\"width:100px;height:100px\">spacer</div>");
await FrameUtils.AttachFrameAsync(DevToolsContext, "button-test", TestConstants.ServerUrl + "/input/button.html");
var frame = DevToolsContext.FirstChildFrame();
var button = await frame.QuerySelectorAsync("button");
var button = await frame.QuerySelectorAsync<HtmlButtonElement>("button");
await button.ClickAsync();
Assert.Equal("Clicked", await frame.EvaluateExpressionAsync<string>("window.result"));
}
Expand All @@ -295,15 +295,15 @@ await DevToolsContext.SetViewportAsync(new ViewPortOptions
Assert.Equal("Clicked", await frame.EvaluateExpressionAsync<string>("window.result"));
}

[WebView2ContextFact()]
[WebView2ContextFact]
public async Task ShouldClickTheButtonWithDeviceScaleFactorSet()
{
await DevToolsContext.SetViewportAsync(new ViewPortOptions { Width = 400, Height = 400, DeviceScaleFactor = 5 });
Assert.Equal(5, await DevToolsContext.EvaluateExpressionAsync<int>("window.devicePixelRatio"));
await DevToolsContext.SetContentAsync("<div style=\"width:100px;height:100px\">spacer</div>");
await FrameUtils.AttachFrameAsync(DevToolsContext, "button-test", TestConstants.ServerUrl + "/input/button.html");
var frame = DevToolsContext.FirstChildFrame();
var button = await frame.QuerySelectorAsync("button");
var button = await frame.QuerySelectorAsync<HtmlButtonElement>("button");
await button.ClickAsync();
Assert.Equal("Clicked", await frame.EvaluateExpressionAsync<string>("window.result"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Threading.Tasks;
using WebView2.DevTools.Dom.Tests.Attributes;
using Xunit;
using Xunit.Abstractions;

namespace WebView2.DevTools.Dom.Tests.DevToolsContextTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class AddHtmlElementTests : DevTooolsContextBaseTest
{
public AddHtmlElementTests(ITestOutputHelper output) : base(output)
{
}

[WebView2ContextFact]
public async Task ShouldCreateDiv()
{
const string expected = "";

await WebView.CoreWebView2.NavigateToAsync(TestConstants.EmptyPage);
var div = await DevToolsContext.CreateHtmlElementAsync<HtmlDivElement>("div");
Assert.NotNull(div);

var actual = await div.GetIdAsync();

Assert.Equal(expected, actual);
}

[WebView2ContextFact]
public async Task ShouldCreateDivWithId()
{
const string expected = "myDiv";

await WebView.CoreWebView2.NavigateToAsync(TestConstants.EmptyPage);
var div = await DevToolsContext.CreateHtmlElementAsync<HtmlDivElement>("div", expected);
Assert.NotNull(div);

var actual = await div.GetIdAsync();

Assert.Equal(expected, actual);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Text.Json;
using System;
using System.Threading.Tasks;
using WebView2.DevTools.Dom;
using Microsoft.Web.WebView2.Core;
using WebView2.DevTools.Dom.Tests.Attributes;
using Xunit;
using Xunit.Abstractions;

namespace WebView2.DevTools.Dom.Tests.DevToolsContextTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class PageEventsPageErrorTests : DevTooolsContextBaseTest
{
public PageEventsPageErrorTests(ITestOutputHelper output) : base(output)
{
}

[WebView2ContextFact(Skip = "Possibly implement this")]
public Task ShouldFire()
{
throw new System.NotImplementedException();
//string error = null;
//void EventHandler(object sender, PageErrorEventArgs e)
//{
// error = e.Message;
// Page.PageError -= EventHandler;
//}

//Page.PageError += EventHandler;

//var completion = new TaskCompletionSource<JsonElement>();

//var reciever = WebView.CoreWebView2.GetDevToolsProtocolEventReceiver("Runtime.exceptionThrown");

//reciever.DevToolsProtocolEventReceived += handler;

//void handler(object sender, CoreWebView2DevToolsProtocolEventReceivedEventArgs e)
//{
// var d = JsonDocument.Parse(e.ParameterObjectAsJson);

// if (d.RootElement.GetProperty("MessageId").GetString() != "Runtime.exceptionThrown")
// {
// return;
// }
// reciever.DevToolsProtocolEventReceived -= handler;
// completion.SetResult(d.RootElement.GetProperty("MessageData").GetString());
//}

//reciever.DevToolsProtocolEventReceived += handler;
//return completion.Task;

//await Task.WhenAll(
// WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/error.html"),
// WaitEvent(, )
//);

//Assert.Contains("Fancy", error);
}
}
}
19 changes: 15 additions & 4 deletions WebView2.DevTools.Dom.Tests/DevToolsContextTests/SelectTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using WebView2.DevTools.Dom;
using WebView2.DevTools.Dom.Tests.Attributes;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -48,7 +47,11 @@ await Task.WhenAll(
public async Task ShouldSelectMultipleOptions()
{
await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/input/select.html");
await DevToolsContext.EvaluateExpressionAsync("makeMultiple()");

var select = await DevToolsContext.QuerySelectorAsync<HtmlSelectElement>("select");

await select.SetMultipleAsync(true);

await DevToolsContext.SelectAsync("select", "blue", "green", "red");
Assert.Equal(new string[] { "blue", "green", "red" },
await DevToolsContext.EvaluateExpressionAsync<string[]>("result.onInput"));
Expand Down Expand Up @@ -85,7 +88,11 @@ public async Task ShouldReturnEmptyArrayOnNoMatchedValues()
public async Task ShouldReturnAnArrayOfMatchedValues()
{
await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/input/select.html");
await DevToolsContext.EvaluateExpressionAsync("makeMultiple()");

var select = await DevToolsContext.QuerySelectorAsync<HtmlSelectElement>("select");

await select.SetMultipleAsync(true);

var result = await DevToolsContext.SelectAsync("select", "blue", "black", "magenta");
Array.Sort(result);
Assert.Equal(new string[] { "black", "blue", "magenta" }, result);
Expand All @@ -109,7 +116,11 @@ public async Task ShouldReturnEmptyArrayOnNoValues()
public async Task ShouldDeselectAllOptionsWhenPassedNoValuesForAMultipleSelect()
{
await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/input/select.html");
await DevToolsContext.EvaluateExpressionAsync("makeMultiple()");

var select = await DevToolsContext.QuerySelectorAsync<HtmlSelectElement>("select");

await select.SetMultipleAsync(true);

await DevToolsContext.SelectAsync("select", "blue", "black", "magenta");
await DevToolsContext.SelectAsync("select");
Assert.True(await DevToolsContext.QuerySelectorAsync("select").EvaluateFunctionAsync<bool>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System.Threading.Tasks;
using WebView2.DevTools.Dom.Tests.Attributes;
using Xunit.Abstractions;
using Xunit;

namespace WebView2.DevTools.Dom.Tests.ElementHandleTests
{

[Collection(TestConstants.TestFixtureCollectionName)]
public class HtmlTableCellElementTests : DevTooolsContextBaseTest
{
public HtmlTableCellElementTests(ITestOutputHelper output) : base(output)
{
}

[WebView2ContextFact]
public async Task ShouldWork()
{
await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/table.html");
var element = await DevToolsContext.QuerySelectorAsync<HtmlTableCellElement>("td");

Assert.NotNull(element);
}

[WebView2ContextFact]
public async Task ShouldReturnNull()
{
await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/table.html");
var element = await DevToolsContext.QuerySelectorAsync<HtmlTableCellElement>("#table2 td");

Assert.Null(element);
}

[WebView2ContextFact]
public async Task ShouldGetIndex()
{
await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/table.html");
var element = await DevToolsContext.QuerySelectorAsync<HtmlTableCellElement>("td");

var index = await element.GetCellIndexAsync();

Assert.True(index > -1);
}

[WebView2ContextFact]
public async Task ShouldSetThenGetAbbr()
{
const string expected = "Testing";

await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/table.html");
var element = await DevToolsContext.QuerySelectorAsync<HtmlTableCellElement>("td");

await element.SetAbbrAsync(expected);
var actual = await element.GetAbbrAsync();

Assert.Equal(expected, actual);
}

[WebView2ContextFact]
public async Task ShouldGetScope()
{
await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/table.html");
var element = await DevToolsContext.QuerySelectorAsync<HtmlTableCellElement>("td");

var actual = await element.GetScopeAsync();

Assert.Equal(string.Empty, actual);
}

[WebView2ContextFact]
public async Task ShouldSetThenGetScope()
{
const string expected = "col";

await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/table.html");
var element = await DevToolsContext.QuerySelectorAsync<HtmlTableCellElement>("td");

await element.SetScopeAsync(expected);
var actual = await element.GetScopeAsync();

Assert.Equal(expected, actual);
}

[WebView2ContextFact]
public async Task ShouldSetThenGetRowSpan()
{
const int expected = 3;

await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/table.html");
var element = await DevToolsContext.QuerySelectorAsync<HtmlTableCellElement>("td");

await element.SetRowSpanAsync(expected);
var actual = await element.GetRowSpanAsync();

Assert.Equal(expected, actual);
}

[WebView2ContextFact]
public async Task ShouldSetThenGetColSpan()
{
const int expected = 3;

await WebView.CoreWebView2.NavigateToAsync(TestConstants.ServerUrl + "/table.html");
var element = await DevToolsContext.QuerySelectorAsync<HtmlTableCellElement>("td");

await element.SetColSpanAsync(expected);
var actual = await element.GetColSpanAsync();

Assert.Equal(expected, actual);
}
}
}
Loading

0 comments on commit ba3214a

Please # to comment.