How to use EvaluateFunctionAsync method of PuppeteerSharp.Frame class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Frame.EvaluateFunctionAsync

ExecutionContext.cs

Source:ExecutionContext.cs Github

copy

Full Screen

...43 /// <param name="script">Script to be evaluated in browser context</param>44 /// <remarks>45 /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.46 /// </remarks>47 /// <seealso cref="EvaluateFunctionAsync{T}(string, object[])"/>48 /// <seealso cref="EvaluateExpressionHandleAsync(string)"/>49 /// <returns>Task which resolves to script return value</returns>50 public Task<JToken> EvaluateExpressionAsync(string script) => EvaluateExpressionAsync<JToken>(script);51 /// <summary>52 /// Executes a script in browser context53 /// </summary>54 /// <typeparam name="T">The type to deserialize the result to</typeparam>55 /// <param name="script">Script to be evaluated in browser context</param>56 /// <remarks>57 /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.58 /// </remarks>59 /// <seealso cref="EvaluateFunctionAsync{T}(string, object[])"/>60 /// <seealso cref="EvaluateExpressionHandleAsync(string)"/>61 /// <returns>Task which resolves to script return value</returns>62 public Task<T> EvaluateExpressionAsync<T>(string script)63 => RemoteObjectTaskToObject<T>(EvaluateExpressionInternalAsync(true, script));64 internal async Task<JSHandle> EvaluateExpressionHandleAsync(string script)65 => CreateJSHandle(await EvaluateExpressionInternalAsync(false, script).ConfigureAwait(false));66 /// <summary>67 /// Executes a function in browser context68 /// </summary>69 /// <param name="script">Script to be evaluated in browser context</param>70 /// <param name="args">Arguments to pass to script</param>71 /// <remarks>72 /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.73 /// <see cref="JSHandle"/> instances can be passed as arguments74 /// </remarks>75 /// <seealso cref="EvaluateExpressionAsync{T}(string)"/>76 /// <returns>Task which resolves to script return value</returns>77 public Task<JToken> EvaluateFunctionAsync(string script, params object[] args) => EvaluateFunctionAsync<JToken>(script, args);78 /// <summary>79 /// Executes a function in browser context80 /// </summary>81 /// <typeparam name="T">The type to deserialize the result to</typeparam>82 /// <param name="script">Script to be evaluated in browser context</param>83 /// <param name="args">Arguments to pass to script</param>84 /// <remarks>85 /// If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.86 /// <see cref="JSHandle"/> instances can be passed as arguments87 /// </remarks>88 /// <seealso cref="EvaluateExpressionAsync{T}(string)"/>89 /// <returns>Task which resolves to script return value</returns>90 public Task<T> EvaluateFunctionAsync<T>(string script, params object[] args)91 => RemoteObjectTaskToObject<T>(EvaluateFunctionInternalAsync(true, script, args));92 internal async Task<JSHandle> EvaluateFunctionHandleAsync(string script, params object[] args)93 => CreateJSHandle(await EvaluateFunctionInternalAsync(false, script, args).ConfigureAwait(false));94 /// <summary>95 /// The method iterates JavaScript heap and finds all the objects with the given prototype.96 /// </summary>97 /// <returns>A task which resolves to a handle to an array of objects with this prototype.</returns>98 /// <param name="prototypeHandle">A handle to the object prototype.</param>99 public async Task<JSHandle> QueryObjectsAsync(JSHandle prototypeHandle)100 {101 if (prototypeHandle.Disposed)102 {103 throw new PuppeteerException("Prototype JSHandle is disposed!");104 }...

Full Screen

Full Screen

BlazorTest.cs

Source:BlazorTest.cs Github

copy

Full Screen

...266 Assert.Equal("puppeteer-sharp", innerHtml);267 Assert.Equal("<a data-pjax=\"#js-repo-pjax-container\" href=\"/kblok/puppeteer-sharp\">puppeteer-sharp</a>", outerHtml);268 Assert.Equal("https://github.com/kblok/puppeteer-sharp", url);269270 var innerText = await page.QuerySelectorAsync("h1 > strong > a").EvaluateFunctionAsync<string>("node => node.innerText");271 var outerText = await page.QuerySelectorAsync("h1 > strong > a").EvaluateFunctionAsync<string>("node => node.outerText");272 var textContent = await page.QuerySelectorAsync("h1 > strong > a").EvaluateFunctionAsync<string>("node => node.textContent");273 var hasContent = await page.QuerySelectorAsync("h1 > strong > a").EvaluateFunctionAsync<bool>("(node, value) => node.textContent.includes(value)", "puppeteer-sharp");274275 Assert.Equal("puppeteer-sharp", innerText);276 Assert.Equal("puppeteer-sharp", outerText);277 Assert.Equal("puppeteer-sharp", textContent);278 Assert.True(hasContent);279280 innerHtml = await page.QuerySelectorAsync("h1 > strong > a").EvaluateFunctionAsync<string>("element => element.innerHTML");281 outerHtml = await page.QuerySelectorAsync("h1 > strong > a").EvaluateFunctionAsync<string>("element => element.outerHTML");282 url = await page.QuerySelectorAsync("h1 > strong > a").EvaluateFunctionAsync<string>("element => element.getAttribute('href')");283284 Assert.Equal("puppeteer-sharp", innerHtml);285 Assert.Equal("<a data-pjax=\"#js-repo-pjax-container\" href=\"/kblok/puppeteer-sharp\">puppeteer-sharp</a>", outerHtml);286 Assert.Equal("/kblok/puppeteer-sharp", url);287288 innerText = await page.EvaluateFunctionAsync<string>("node => node.innerText", element);289 outerText = await page.EvaluateFunctionAsync<string>("node => node.outerText", element);290 textContent = await page.EvaluateFunctionAsync<string>("node => node.textContent", element);291292 Assert.Equal("puppeteer-sharp", innerText);293 Assert.Equal("puppeteer-sharp", outerText);294 Assert.Equal("puppeteer-sharp", textContent);295 }296 }*/ ...

Full Screen

Full Screen

Examples.cs

Source:Examples.cs Github

copy

Full Screen

...135 public async Task evaluate()136 {137 await _page.GoToAsync("https://github.com/hardkoded/puppeteer-sharp");138 var element = await _page.QuerySelectorAsync("h1 > strong > a");139 var outerHtml = await element.EvaluateFunctionAsync<string>("e => e.outerHTML");140 var innerText = await element.EvaluateFunctionAsync<string>("e => e.innerText");141 var url = await element.EvaluateFunctionAsync<string>("e => e.getAttribute('href')");142 var hasContent = await element.EvaluateFunctionAsync<bool>("(e, value) => e.textContent.includes(value)", "puppeteer-sharp");143 Assert.Equal("<a data-pjax=\"#js-repo-pjax-container\" href=\"/hardkoded/puppeteer-sharp\">puppeteer-sharp</a>", outerHtml);144 Assert.Equal("puppeteer-sharp", innerText);145 Assert.Equal("/hardkoded/puppeteer-sharp", url);146 Assert.True(hasContent);147 outerHtml = await _page.EvaluateFunctionAsync<string>("e => e.outerHTML", element);148 innerText = await _page.EvaluateFunctionAsync<string>("e => e.innerText", element);149 url = await _page.EvaluateFunctionAsync<string>("e => e.getAttribute('href')", element);150 hasContent = await _page.EvaluateFunctionAsync<bool>("(e, value) => e.textContent.includes(value)", element, "puppeteer-sharp");151 Assert.Equal("<a data-pjax=\"#js-repo-pjax-container\" href=\"/hardkoded/puppeteer-sharp\">puppeteer-sharp</a>", outerHtml);152 Assert.Equal("puppeteer-sharp", innerText);153 Assert.Equal("/hardkoded/puppeteer-sharp", url);154 Assert.True(hasContent);155 outerHtml = await _page.EvaluateExpressionAsync<string>($"document.querySelector({"'h1 > strong > a'"}).outerHTML");156 innerText = await _page.EvaluateExpressionAsync<string>($"document.querySelector({"'h1 > strong > a'"}).innerText");157 url = await _page.EvaluateExpressionAsync<string>($"document.querySelector({"'h1 > strong > a'"}).getAttribute('href')");158 hasContent = await _page.EvaluateExpressionAsync<bool>($"document.querySelector({"'h1 > strong > a'"}).textContent.includes({"'puppeteer-sharp'"})");159 Assert.Equal("<a data-pjax=\"#js-repo-pjax-container\" href=\"/hardkoded/puppeteer-sharp\">puppeteer-sharp</a>", outerHtml);160 Assert.Equal("puppeteer-sharp", innerText);161 Assert.Equal("/hardkoded/puppeteer-sharp", url);162 Assert.True(hasContent);163 outerHtml = await (await element.GetPropertyAsync("outerHTML")).JsonValueAsync<string>();164 innerText = await (await element.GetPropertyAsync("innerText")).JsonValueAsync<string>();165 url = await (await element.GetPropertyAsync("href")).JsonValueAsync<string>();166 hasContent = await (await element.GetPropertyAsync("textContent")).EvaluateFunctionAsync<bool>("(p, value) => p.includes(value)", "puppeteer-sharp");167 Assert.Equal("<a data-pjax=\"#js-repo-pjax-container\" href=\"/hardkoded/puppeteer-sharp\">puppeteer-sharp</a>", outerHtml);168 Assert.Equal("puppeteer-sharp", innerText);169 Assert.Equal("https://github.com/hardkoded/puppeteer-sharp", url);170 Assert.True(hasContent);171 }172 }173}...

Full Screen

Full Screen

Extensions.cs

Source:Extensions.cs Github

copy

Full Screen

...13 /// <param name="pageFunction">Function to be evaluated in browser context</param>14 /// <param name="args">Arguments to pass to <c>pageFunction</c></param>15 /// <returns>Task</returns>16 /// <exception cref="SelectorException">If <paramref name="elementHandleTask"/> resolves to <c>null</c></exception>17 public static Task EvaluateFunctionAsync(this Task<ElementHandle> elementHandleTask, string pageFunction, params object[] args)18 => elementHandleTask.EvaluateFunctionAsync<object>(pageFunction, args);19 /// <summary>20 /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="elementHandleTask"/> as the first argument21 /// </summary>22 /// <typeparam name="T">The type of the response</typeparam>23 /// <param name="elementHandleTask">A task that returns an <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>24 /// <param name="pageFunction">Function to be evaluated in browser context</param>25 /// <param name="args">Arguments to pass to <c>pageFunction</c></param>26 /// <returns>Task which resolves to the return value of <c>pageFunction</c></returns>27 /// <exception cref="SelectorException">If <paramref name="elementHandleTask"/> resolves to <c>null</c></exception>28 public static async Task<T> EvaluateFunctionAsync<T>(this Task<ElementHandle> elementHandleTask, string pageFunction, params object[] args)29 {30 var elementHandle = await elementHandleTask.ConfigureAwait(false);31 if (elementHandle == null)32 {33 throw new SelectorException("Error: failed to find element matching selector");34 }35 return await elementHandle.EvaluateFunctionAsync<T>(pageFunction, args).ConfigureAwait(false);36 }37 /// <summary>38 /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome the <paramref name="elementHandle"/> as the first argument39 /// </summary>40 /// <typeparam name="T">The type of the response</typeparam>41 /// <param name="elementHandle">An <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>42 /// <param name="pageFunction">Function to be evaluated in browser context</param>43 /// <param name="args">Arguments to pass to <c>pageFunction</c></param>44 /// <returns>Task which resolves to the return value of <c>pageFunction</c></returns>45 /// <exception cref="SelectorException">If <paramref name="elementHandle"/> is <c>null</c></exception>46 public static async Task<T> EvaluateFunctionAsync<T>(this ElementHandle elementHandle, string pageFunction, params object[] args)47 {48 if (elementHandle == null)49 {50 throw new SelectorException("Error: failed to find element matching selector");51 }52 var result = await elementHandle.EvaluateFunctionAsync<T>(pageFunction, args).ConfigureAwait(false);53 await elementHandle.DisposeAsync().ConfigureAwait(false);54 return result;55 }56 /// <summary>57 /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="arrayHandleTask"/> as the first argument. Use only after <see cref="Page.QuerySelectorAllHandleAsync(string)"/>58 /// </summary>59 /// <param name="arrayHandleTask">A task that returns an <see cref="JSHandle"/> that represents an array of <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>60 /// <param name="pageFunction">Function to be evaluated in browser context</param>61 /// <param name="args">Arguments to pass to <c>pageFunction</c></param>62 /// <returns>Task</returns>63 public static Task EvaluateFunctionAsync(this Task<JSHandle> arrayHandleTask, string pageFunction, params object[] args)64 => arrayHandleTask.EvaluateFunctionAsync<object>(pageFunction, args);65 /// <summary>66 /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="arrayHandleTask"/> as the first argument. Use only after <see cref="Page.QuerySelectorAllHandleAsync(string)"/>67 /// </summary>68 /// <typeparam name="T"></typeparam>69 /// <param name="arrayHandleTask">A task that returns an <see cref="JSHandle"/> that represents an array of <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>70 /// <param name="pageFunction">Function to be evaluated in browser context</param>71 /// <param name="args">Arguments to pass to <c>pageFunction</c></param>72 /// <returns>Task which resolves to the return value of <c>pageFunction</c></returns>73 public static async Task<T> EvaluateFunctionAsync<T>(this Task<JSHandle> arrayHandleTask, string pageFunction, params object[] args)74 => await (await arrayHandleTask.ConfigureAwait(false)).EvaluateFunctionAsync<T>(pageFunction, args).ConfigureAwait(false);75 /// <summary>76 /// Runs <paramref name="pageFunction"/> within the frame and passes it the outcome of <paramref name="arrayHandle"/> as the first argument. Use only after <see cref="Page.QuerySelectorAllHandleAsync(string)"/>77 /// </summary>78 /// <typeparam name="T"></typeparam>79 /// <param name="arrayHandle">An <see cref="JSHandle"/> that represents an array of <see cref="ElementHandle"/> that will be used as the first argument in <paramref name="pageFunction"/></param>80 /// <param name="pageFunction">Function to be evaluated in browser context</param>81 /// <param name="args">Arguments to pass to <c>pageFunction</c></param>82 /// <returns>Task which resolves to the return value of <c>pageFunction</c></returns>83 public static async Task<T> EvaluateFunctionAsync<T>(this JSHandle arrayHandle, string pageFunction, params object[] args)84 {85 var result = await arrayHandle.EvaluateFunctionAsync<T>(pageFunction, args).ConfigureAwait(false);86 await arrayHandle.DisposeAsync().ConfigureAwait(false);87 return result;88 }89 }90}

Full Screen

Full Screen

EvaluateTests.cs

Source:EvaluateTests.cs Github

copy

Full Screen

...23 [InlineData("() => 7 * 3", 21)] //ShouldWork24 [InlineData("() => Promise.resolve(8 * 7)", 56)] //ShouldAwaitPromise25 public async Task BasicIntFunctionEvaluationTest(string script, object expected)26 {27 var result = await Page.EvaluateFunctionAsync<int>(script);28 Assert.Equal(expected, result);29 }30 [Fact]31 public async Task ShouldWorkRightAfterFrameNavigated()32 {33 Task<int> frameEvaluation = null;34 Page.FrameNavigated += (sender, e) =>35 {36 frameEvaluation = e.Frame.EvaluateFunctionAsync<int>("() => 6 * 7");37 };38 await Page.GoToAsync(TestConstants.EmptyPage);39 Assert.Equal(42, await frameEvaluation);40 }41 [Fact]42 public async Task ShouldRejectPromiseWithExeption()43 {44 var exception = await Assert.ThrowsAsync<EvaluationFailedException>(() =>45 {46 return Page.EvaluateFunctionAsync<object>("() => not.existing.object.property");47 });48 Assert.Contains("not is not defined", exception.Message);49 }50 [Fact]51 public async Task SouldReturnComplexObjects()52 {53 dynamic obj = new54 {55 foo = "bar!"56 };57 dynamic result = await Page.EvaluateFunctionAsync("a => a", obj);58 Assert.Equal("bar!", result.foo.ToString());59 }60 [Theory]61 [InlineData("() => NaN", double.NaN)] //ShouldReturnNaN62 [InlineData("() => -0", -0)] //ShouldReturnNegative063 [InlineData("() => Infinity", double.PositiveInfinity)] //ShouldReturnInfinity64 [InlineData("() => -Infinity", double.NegativeInfinity)] //ShouldReturnNegativeInfinty65 public async Task BasicEvaluationTest(string script, object expected)66 {67 dynamic result = await Page.EvaluateFunctionAsync(script);68 Assert.Equal(expected, result);69 }70 [Fact]71 public async Task ShouldAcceptNullAsOneOfMultipleParameters()72 {73 bool result = await Page.EvaluateFunctionAsync<bool>("(a, b) => Object.is(a, null) && Object.is(b, 'foo')", null, "foo");74 Assert.True(result);75 }76 [Fact]77 public async Task ShouldProperlyIgnoreUndefinedFields()78 {79 var result = await Page.EvaluateFunctionAsync<Dictionary<string, object>>("() => ({a: undefined})");80 Assert.Empty(result);81 }82 [Fact]83 public async Task ShouldProperlySerializeNullFields()84 {85 var result = await Page.EvaluateFunctionAsync<Dictionary<string, object>>("() => ({a: null})");86 Assert.True(result.ContainsKey("a"));87 Assert.Null(result["a"]);88 }89 [Fact]90 public async Task ShouldFailForWindowObjectUsingEvaluateExpression()91 {92 var window = await Page.EvaluateExpressionAsync("window");93 Assert.Null(window);94 }95 [Fact]96 public async Task ShouldFailForWindowObjectUsingEvaluateFunction()97 {98 var window = await Page.EvaluateFunctionAsync("() => window");99 Assert.Null(window);100 }101 102 [Fact]103 public async Task ShouldAcceptElementHandleAsAnArgument()104 {105 await Page.SetContentAsync("<section>42</section>");106 var element = await Page.QuerySelectorAsync("section");107 var text = await Page.EvaluateFunctionAsync<string>("e => e.textContent", element);108 Assert.Equal("42", text);109 }110 [Fact]111 public async Task ShouldThrowIfUnderlyingElementWasDisposed()112 {113 await Page.SetContentAsync("<section>39</section>");114 var element = await Page.QuerySelectorAsync("section");115 Assert.NotNull(element);116 await element.DisposeAsync();117 var exception = await Assert.ThrowsAsync<PuppeteerException>(()118 => Page.EvaluateFunctionAsync<string>("e => e.textContent", element));119 Assert.Contains("JSHandle is disposed", exception.Message);120 }121 [Fact]122 public async Task ShouldThrowIfElementHandlesAreFromOtherFrames()123 {124 await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);125 var bodyHandle = await Page.Frames[1].QuerySelectorAsync("body");126 var exception = await Assert.ThrowsAsync<PuppeteerException>(()127 => Page.EvaluateFunctionAsync<string>("body => body.innerHTML", bodyHandle));128 Assert.Contains("JSHandles can be evaluated only in the context they were created", exception.Message);129 }130 [Fact]131 public async Task ShouldAcceptObjectHandleAsAnArgument()132 {133 var navigatorHandle = await Page.EvaluateExpressionHandleAsync("navigator");134 var text = await Page.EvaluateFunctionAsync<string>("e => e.userAgent", navigatorHandle);135 Assert.Contains("Mozilla", text);136 }137 [Fact]138 public async Task ShouldAcceptObjectHandleToPrimitiveTypes()139 {140 var aHandle = await Page.EvaluateExpressionHandleAsync("5");141 var isFive = await Page.EvaluateFunctionAsync<bool>("e => Object.is(e, 5)", aHandle);142 Assert.True(isFive);143 }144 [Fact]145 public async Task ShouldWorkFromInsideAnExposedFunction()146 {147 await Page.ExposeFunctionAsync("callController", async (int a, int b) =>148 {149 return await Page.EvaluateFunctionAsync("(a, b) => a * b", a, b);150 });151 var result = await Page.EvaluateFunctionAsync<int>(@"async function() {152 return await callController(9, 3);153 }");154 Assert.Equal(27, result);155 }156 }157}...

Full Screen

Full Screen

WaitForXPathTests.cs

Source:WaitForXPathTests.cs Github

copy

Full Screen

...17 public async Task ShouldSupportSomeFancyXpath()18 {19 await Page.SetContentAsync("<p>red herring</p><p>hello world </p>");20 var waitForXPath = Page.WaitForXPathAsync("//p[normalize-space(.)=\"hello world\"]");21 Assert.Equal("hello world ", await Page.EvaluateFunctionAsync<string>("x => x.textContent", await waitForXPath));22 }23 [Fact]24 public async Task ShouldRunInSpecifiedFrame()25 {26 await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);27 await FrameUtils.AttachFrameAsync(Page, "frame2", TestConstants.EmptyPage);28 var frame1 = Page.Frames.First(f => f.Name == "frame1");29 var frame2 = Page.Frames.First(f => f.Name == "frame2");30 var waitForXPathPromise = frame2.WaitForXPathAsync("//div");31 await frame1.EvaluateFunctionAsync(addElement, "div");32 await frame2.EvaluateFunctionAsync(addElement, "div");33 var eHandle = await waitForXPathPromise;34 Assert.Equal(frame2, eHandle.ExecutionContext.Frame);35 }36 [Fact]37 public async Task ShouldThrowWhenFrameIsDetached()38 {39 await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);40 var frame = Page.FirstChildFrame();41 var waitPromise = frame.WaitForXPathAsync("//*[@class=\"box\"]");42 await FrameUtils.DetachFrameAsync(Page, "frame1");43 var exception = await Assert.ThrowsAnyAsync<Exception>(() => waitPromise);44 Assert.Contains("waitForFunction failed: frame got detached.", exception.Message);45 }46 [Fact]47 public async Task HiddenShouldWaitForDisplayNone()48 {49 var divHidden = false;50 await Page.SetContentAsync("<div style='display: block;'></div>");51 var waitForXPath = Page.WaitForXPathAsync("//div", new WaitForSelectorOptions { Hidden = true })52 .ContinueWith(_ => divHidden = true);53 await Page.WaitForXPathAsync("//div"); // do a round trip54 Assert.False(divHidden);55 await Page.EvaluateExpressionAsync("document.querySelector('div').style.setProperty('display', 'none')");56 Assert.True(await waitForXPath.WithTimeout());57 Assert.True(divHidden);58 }59 [Fact]60 public async Task ShouldReturnTheElementHandle()61 {62 var waitForXPath = Page.WaitForXPathAsync("//*[@class=\"zombo\"]");63 await Page.SetContentAsync("<div class='zombo'>anything</div>");64 Assert.Equal("anything", await Page.EvaluateFunctionAsync<string>("x => x.textContent", await waitForXPath));65 }66 [Fact]67 public async Task ShouldAllowYouToSelectATextNode()68 {69 await Page.SetContentAsync("<div>some text</div>");70 var text = await Page.WaitForXPathAsync("//div/text()");71 Assert.Equal(3 /* Node.TEXT_NODE */, await (await text.GetPropertyAsync("nodeType")).JsonValueAsync<int>());72 }73 [Fact]74 public async Task ShouldAllowYouToSelectAnElementWithSingleSlash()75 {76 await Page.SetContentAsync("<div>some text</div>");77 var waitForXPath = Page.WaitForXPathAsync("/html/body/div");78 Assert.Equal("some text", await Page.EvaluateFunctionAsync<string>("x => x.textContent", await waitForXPath));79 }80 [Fact]81 public async Task ShouldRespectTimeout()82 {83 var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(()84 => Page.WaitForXPathAsync("//div", new WaitForSelectorOptions { Timeout = 10 }));85 Assert.Contains("waiting for XPath '//div' failed: timeout", exception.Message);86 }87 }88}...

Full Screen

Full Screen

FrameUtils.cs

Source:FrameUtils.cs Github

copy

Full Screen

...6 public static class FrameUtils7 {8 public static async Task AttachFrameAsync(Page page, string frameId, string url)9 {10 await page.EvaluateFunctionAsync(@"(frameId, url) => {11 const frame = document.createElement('iframe');12 frame.src = url;13 frame.id = frameId;14 document.body.appendChild(frame);15 return new Promise(x => frame.onload = x);16 }", frameId, url);17 }18 public static async Task DetachFrameAsync(Page page, string frameId)19 {20 await page.EvaluateFunctionAsync(@"function detachFrame(frameId) {21 const frame = document.getElementById(frameId);22 frame.remove();23 }", frameId);24 }25 public static string DumpFrames(Frame frame, string indentation = "")26 {27 var result = indentation + Regex.Replace(frame.Url, @":\d{4}", ":<PORT>");28 foreach (var child in frame.ChildFrames)29 {30 result += "\n" + DumpFrames(child, " " + indentation);31 }32 return result;33 }34 internal static async Task NavigateFrameAsync(Page page, string frameId, string url)35 {36 await page.EvaluateFunctionAsync(@"function navigateFrame(frameId, url) {37 const frame = document.getElementById(frameId);38 frame.src = url;39 return new Promise(x => frame.onload = x);40 }", frameId, url);41 }42 }43}...

Full Screen

Full Screen

PuppeteerExtensions.cs

Source:PuppeteerExtensions.cs Github

copy

Full Screen

...21 return await frame.GetInnerText(element);22 }23 public static async Task<string> GetInnerText(this Frame frame, ElementHandle handle)24 {25 return await frame.EvaluateFunctionAsync<string>("el => el.textContent", handle);26 }27 public static async Task<bool> NavigateAndWait(this Page page, string url)28 {29 try30 {31 return (await page.GoToAsync(url, 15000, new[] { WaitUntilNavigation.Networkidle2 })).Ok;32 }33 catch (System.Exception)34 {35 return false;36 }37 }38 }39}...

Full Screen

Full Screen

EvaluateFunctionAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 MainAsync().GetAwaiter().GetResult();9 }10 static async Task MainAsync()11 {12 var options = new LaunchOptions { Headless = false };13 using (var browser = await Puppeteer.LaunchAsync(options))14 using (var page = await browser.NewPageAsync())15 {16 string result = await page.EvaluateFunctionAsync<string>("() => {return 'hello from puppeteer!';}");17 Console.WriteLine(result);18 }19 }20 }21}

Full Screen

Full Screen

EvaluateFunctionAsync

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2var result = await page.EvaluateFunctionAsync<int>("() => 7 * 3");3var page = await browser.NewPageAsync();4var result = await page.EvaluateFunctionAsync<int>("(a, b) => a * b", 7, 3);5var page = await browser.NewPageAsync();6var result = await page.EvaluateFunctionAsync<int>("(a, b) => a * b", new { a = 7, b = 3 });7var page = await browser.NewPageAsync();8var result = await page.EvaluateFunctionAsync<int>("() => Promise.resolve(7 * 3)");9var page = await browser.NewPageAsync();10var result = await page.EvaluateFunctionAsync<int>("() => Promise.resolve(7 * 3)", new { a = 7, b = 3 });11var page = await browser.NewPageAsync();12var result = await page.EvaluateFunctionAsync<int>("() => Promise.resolve(7 * 3)", new[] { 7, 3 });13var page = await browser.NewPageAsync();

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful