@@ -186,7 +186,132 @@ coreWebView2.NavigationCompleted += async (sender, args) =>
186186 }
187187};
188188```
189- <sup ><a href =' /WebView2.DevTools.Dom.Tests/QuerySelectorTests/DevToolsContextQuerySelectorTests.cs#L20-L144 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-queryselector ' title =' Start of snippet ' >anchor</a ></sup >
189+ <sup ><a href =' /src/WebView2.DevTools.Dom.Tests/QuerySelectorTests/DevToolsContextQuerySelectorTests.cs#L20-L144 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-queryselector ' title =' Start of snippet ' >anchor</a ></sup >
190+ <a id =' snippet-queryselector-1 ' ></a >
191+ ``` cs
192+ // Add using WebView2.DevTools.Dom; to get access to the
193+ // CreateDevToolsContextAsync extension method
194+
195+ coreWebView2 .NavigationCompleted += async (sender , args ) =>
196+ {
197+ if (args .IsSuccess )
198+ {
199+ // WebView2DevToolsContext implements IAsyncDisposable and can be Disposed
200+ // via await using or await devToolsContext.DisposeAsync();
201+ // https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync#using-async-disposable
202+ await using var devToolsContext = await coreWebView2 .CreateDevToolsContextAsync ();
203+
204+ // Get element by Id
205+ // https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
206+ var element = await devToolsContext .QuerySelectorAsync <HtmlElement >(" #myElementId" );
207+
208+ // Strongly typed element types
209+ // Only a subset of element types have been added so far, use HtmlElement as a generic type for all others
210+ var htmlDivElement = await devToolsContext .QuerySelectorAsync <HtmlDivElement >(" #myDivElementId" );
211+ var htmlSpanElement = await devToolsContext .QuerySelectorAsync <HtmlSpanElement >(" #mySpanElementId" );
212+ var htmlSelectElement = await devToolsContext .QuerySelectorAsync <HtmlSelectElement >(" #mySelectElementId" );
213+ var htmlInputElement = await devToolsContext .QuerySelectorAsync <HtmlInputElement >(" #myInputElementId" );
214+ var htmlFormElement = await devToolsContext .QuerySelectorAsync <HtmlFormElement >(" #myFormElementId" );
215+ var htmlAnchorElement = await devToolsContext .QuerySelectorAsync <HtmlAnchorElement >(" #myAnchorElementId" );
216+ var htmlImageElement = await devToolsContext .QuerySelectorAsync <HtmlImageElement >(" #myImageElementId" );
217+ var htmlTextAreaElement = await devToolsContext .QuerySelectorAsync <HtmlImageElement >(" #myTextAreaElementId" );
218+ var htmlButtonElement = await devToolsContext .QuerySelectorAsync <HtmlButtonElement >(" #myButtonElementId" );
219+ var htmlParagraphElement = await devToolsContext .QuerySelectorAsync <HtmlParagraphElement >(" #myParagraphElementId" );
220+ var htmlTableElement = await devToolsContext .QuerySelectorAsync <HtmlTableElement >(" #myTableElementId" );
221+
222+ // Get a custom attribute value
223+ var customAttribute = await element .GetAttributeAsync <string >(" data-customAttribute" );
224+
225+ // Set innerText property for the element
226+ await element .SetInnerTextAsync (" Welcome!" );
227+
228+ // Get innerText property for the element
229+ var innerText = await element .GetInnerTextAsync ();
230+ // Can also be acessed via calling GetPropertyValueAsync
231+ // Can use this method to get any property that isn't currently mapped
232+ innerText = await element .GetInnerTextAsync ();
233+
234+ // Get all child elements
235+ var childElements = await element .QuerySelectorAllAsync (" div" );
236+
237+ // Change CSS style background colour
238+ await element .EvaluateFunctionAsync (" e => e.style.backgroundColor = 'yellow'" );
239+
240+ // Type text in an input field
241+ await element .TypeAsync (" Welcome to my Website!" );
242+
243+ // Scroll Element into View (if needed)
244+ // Can optional specify a Rect to be scrolled into view, relative to the node's border box,
245+ // in CSS pixels. When omitted, center of the node will be used
246+ await element .ScrollIntoViewIfNeededAsync ();
247+
248+ // Click The element
249+ await element .ClickAsync ();
250+
251+ // Simple way of chaining method calls together when you don't need a handle to the HtmlElement
252+ var htmlButtonElementInnerText = await devToolsContext .QuerySelectorAsync <HtmlButtonElement >(" #myButtonElementId" )
253+ .AndThen (x => x .GetInnerTextAsync ());
254+
255+ // Event Handler
256+ // Expose a function to javascript, functions persist across navigations
257+ // So only need to do this once
258+ await devToolsContext .ExposeFunctionAsync (" jsAlertButtonClick" , () =>
259+ {
260+ _ = devToolsContext .EvaluateExpressionAsync (" window.alert('Hello! You invoked window.alert()');" );
261+ });
262+
263+ var jsAlertButton = await devToolsContext .QuerySelectorAsync (" #jsAlertButton" );
264+
265+ // Write up the click event listner to call our exposed function
266+ _ = jsAlertButton .AddEventListenerAsync (" click" , " jsAlertButtonClick" );
267+
268+ // Get a collection of HtmlElements
269+ var divElements = await devToolsContext .QuerySelectorAllAsync <HtmlDivElement >(" div" );
270+
271+ foreach (var div in divElements )
272+ {
273+ // Get a reference to the CSSStyleDeclaration
274+ var style = await div .GetStyleAsync ();
275+
276+ // Set the border to 1px solid red
277+ await style .SetPropertyAsync (" border" , " 1px solid red" , important : true );
278+
279+ await div .SetAttributeAsync (" data-customAttribute" , " 123" );
280+ await div .SetInnerTextAsync (" Updated Div innerText" );
281+ }
282+
283+ // Using standard array
284+ var tableRows = await htmlTableElement .GetRowsAsync ().ToArrayAsync ();
285+
286+ foreach (var row in tableRows )
287+ {
288+ var cells = await row .GetCellsAsync ().ToArrayAsync ();
289+ foreach (var cell in cells )
290+ {
291+ var newDiv = await devToolsContext .CreateHtmlElementAsync <HtmlDivElement >(" div" );
292+ await newDiv .SetInnerTextAsync (" New Div Added!" );
293+ await cell .AppendChildAsync (newDiv );
294+ }
295+ }
296+
297+ // Get a reference to the HtmlCollection and use async enumerable
298+ // Requires Net Core 3.1 or higher
299+ var tableRowsHtmlCollection = await htmlTableElement .GetRowsAsync ();
300+
301+ await foreach (var row in tableRowsHtmlCollection )
302+ {
303+ var cells = await row .GetCellsAsync ();
304+ await foreach (var cell in cells )
305+ {
306+ var newDiv = await devToolsContext .CreateHtmlElementAsync <HtmlDivElement >(" div" );
307+ await newDiv .SetInnerTextAsync (" New Div Added!" );
308+ await cell .AppendChildAsync (newDiv );
309+ }
310+ }
311+ }
312+ };
313+ ```
314+ <sup ><a href =' /WebView2.DevTools.Dom.Tests/QuerySelectorTests/DevToolsContextQuerySelectorTests.cs#L20-L144 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-queryselector-1 ' title =' Start of snippet ' >anchor</a ></sup >
190315<!-- endSnippet -->
191316
192317## Inject HTML
@@ -207,7 +332,24 @@ await using var devtoolsContext = await coreWebView2.CreateDevToolsContextAsync(
207332await devtoolsContext .SetContentAsync (" <div>My Receipt</div>" );
208333var result = await devtoolsContext .GetContentAsync ();
209334```
210- <sup ><a href =' /WebView2.DevTools.Dom.Tests/DevToolsContextTests/SetContentTests.cs#L22-L37 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-setcontentasync ' title =' Start of snippet ' >anchor</a ></sup >
335+ <sup ><a href =' /src/WebView2.DevTools.Dom.Tests/DevToolsContextTests/SetContentTests.cs#L22-L37 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-setcontentasync ' title =' Start of snippet ' >anchor</a ></sup >
336+ <a id =' snippet-setcontentasync-1 ' ></a >
337+ ``` cs
338+ // Add using WebView2.DevTools.Dom; to get access to the
339+ // CreateDevToolsContextAsync extension method
340+
341+ // WebView2DevToolsContext implements IAsyncDisposable and can be Disposed
342+ // via await using or await devToolsContext.DisposeAsync();
343+ // Only DisposeAsync is supported. It's very important the WebView2DevToolsContext is Disposed
344+ // When you have finished. Only create a single instance at a time, reuse an instance rather than
345+ // creaeting a new WebView2DevToolsContext. Dispose the old WebView2DevToolsContext instance before
346+ // creating a new instance if you need to manage the lifespan manually.
347+ // https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync#using-async-disposable
348+ await using var devtoolsContext = await coreWebView2 .CreateDevToolsContextAsync ();
349+ await devtoolsContext .SetContentAsync (" <div>My Receipt</div>" );
350+ var result = await devtoolsContext .GetContentAsync ();
351+ ```
352+ <sup ><a href =' /WebView2.DevTools.Dom.Tests/DevToolsContextTests/SetContentTests.cs#L22-L37 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-setcontentasync-1 ' title =' Start of snippet ' >anchor</a ></sup >
211353<!-- endSnippet -->
212354
213355## Evaluate Javascript
@@ -232,7 +374,27 @@ var fourtyTwo = await devToolsContext.EvaluateFunctionAsync<int>("() => Promise.
232374var someObject = await devToolsContext .EvaluateFunctionAsync <dynamic >(" (value) => ({a: value})" , 5 );
233375System .Console .WriteLine (someObject .a );
234376```
235- <sup ><a href =' /WebView2.DevTools.Dom.Tests/QuerySelectorTests/ElementHandleQuerySelectorEvalTests.cs#L17-L35 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-evaluate ' title =' Start of snippet ' >anchor</a ></sup >
377+ <sup ><a href =' /src/WebView2.DevTools.Dom.Tests/QuerySelectorTests/ElementHandleQuerySelectorEvalTests.cs#L17-L35 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-evaluate ' title =' Start of snippet ' >anchor</a ></sup >
378+ <a id =' snippet-evaluate-1 ' ></a >
379+ ``` cs
380+ // Add using WebView2.DevTools.Dom; to get access to the
381+ // CreateDevToolsContextAsync extension method
382+
383+ await webView2Browser .EnsureCoreWebView2Async ();
384+
385+ // WebView2DevToolsContext implements IAsyncDisposable and can be Disposed
386+ // via await using or await devToolsContext.DisposeAsync();
387+ // https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync#using-async-disposable
388+ await using var devToolsContext = await webView2Browser .CoreWebView2 .CreateDevToolsContextAsync ();
389+ await devToolsContext .IgnoreCertificateErrorsAsync (true );
390+ var seven = await devToolsContext .EvaluateExpressionAsync <int >(" 4 + 3" );
391+ // Can evaluate a function that returns a Promise
392+ var fourtyTwo = await devToolsContext .EvaluateFunctionAsync <int >(" () => Promise.resolve(42)" );
393+ // Pass in arguments to a function, including references to HtmlElements and JavascriptHandles
394+ var someObject = await devToolsContext .EvaluateFunctionAsync <dynamic >(" (value) => ({a: value})" , 5 );
395+ System .Console .WriteLine (someObject .a );
396+ ```
397+ <sup ><a href =' /WebView2.DevTools.Dom.Tests/QuerySelectorTests/ElementHandleQuerySelectorEvalTests.cs#L17-L35 ' title =' Snippet source file ' >snippet source</a > | <a href =' #snippet-evaluate-1 ' title =' Start of snippet ' >anchor</a ></sup >
236398<!-- endSnippet -->
237399
238400## NOT YET SUPPORTED
0 commit comments