Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageExposeFunctionTests.ShouldWork
PageExposeFunctionTests.cs
Source:PageExposeFunctionTests.cs
...31 ///<playwright-file>page-expose-function.spec.ts</playwright-file>32 public class PageExposeFunctionTests : PageTestEx33 {34 [PlaywrightTest("page-expose-function.spec.ts", "exposeBinding should work")]35 public async Task ExposeBindingShouldWork()36 {37 BindingSource bindingSource = null;38 await Page.ExposeBindingAsync("add", (BindingSource source, int a, int b) =>39 {40 bindingSource = source;41 return a + b;42 });43 int result = await Page.EvaluateAsync<int>("async function () { return add(5, 6); }");44 Assert.AreEqual(Context, bindingSource.Context);45 Assert.AreEqual(Page, bindingSource.Page);46 Assert.AreEqual(Page.MainFrame, bindingSource.Frame);47 Assert.AreEqual(11, result);48 }49 [PlaywrightTest("page-expose-function.spec.ts", "should work")]50 public async Task ShouldWork()51 {52 await Page.ExposeFunctionAsync("compute", (int a, int b) => a * b);53 int result = await Page.EvaluateAsync<int>(@"async function() {54 return await compute(9, 4);55 }");56 Assert.AreEqual(36, result);57 }58 [PlaywrightTest("page-expose-function.spec.ts", "should work with handles and complex objects")]59 public async Task ShouldWorkWithHandlesAndComplexObjects()60 {61 var fooHandle = await Page.EvaluateHandleAsync(@"() => {62 window['fooValue'] = { bar: 2 };63 return window['fooValue'];64 }");65 await Page.ExposeFunctionAsync("handle", () => new[] { new { foo = fooHandle } });66 Assert.True(await Page.EvaluateAsync<bool>(@"async function() {67 const value = await window['handle']();68 const [{ foo }] = value;69 return foo === window['fooValue'];70 }"));71 }72 [PlaywrightTest("page-expose-function.spec.ts", "should throw exception in page context")]73 public async Task ShouldThrowExceptionInPageContext()74 {75 await Page.ExposeFunctionAsync("woof", (System.Action)(() =>76 {77 throw new PlaywrightException("WOOF WOOF");78 }));79 var result = await Page.EvaluateAsync<JsonElement>(@"async () => {80 try81 {82 await woof();83 }84 catch (e)85 {86 return { message: e.message, stack: e.stack};87 }88 }");89 Assert.AreEqual("WOOF WOOF", result.GetProperty("message").GetString());90 StringAssert.Contains(nameof(PageExposeFunctionTests), result.GetProperty("stack").GetString());91 }92 [PlaywrightTest("page-expose-function.spec.ts", @"should support throwing ""null""")]93 public async Task ShouldSupportThrowingNull()94 {95 await Page.ExposeFunctionAsync("woof", () =>96 {97 throw null;98 });99 var result = await Page.EvaluateAsync(@"async () => {100 try {101 await window['woof']();102 } catch (e) {103 return e;104 }105 }");106 Assert.AreEqual(null, null);107 }108 [PlaywrightTest("page-expose-function.spec.ts", "should be callable from-inside addInitScript")]109 public async Task ShouldBeCallableFromInsideAddInitScript()110 {111 bool called = false;112 await Page.ExposeFunctionAsync("woof", () =>113 {114 called = true;115 });116 await Page.AddInitScriptAsync("woof()");117 await Page.ReloadAsync();118 Assert.True(called);119 }120 [PlaywrightTest("page-expose-function.spec.ts", "should survive navigation")]121 public async Task ShouldSurviveNavigation()122 {123 await Page.ExposeFunctionAsync("compute", (int a, int b) => a * b);124 await Page.GotoAsync(Server.EmptyPage);125 int result = await Page.EvaluateAsync<int>(@"async function() {126 return await compute(9, 4);127 }");128 Assert.AreEqual(36, result);129 }130 [PlaywrightTest("page-expose-function.spec.ts", "should await returned promise")]131 public async Task ShouldAwaitReturnedPromise()132 {133 await Page.ExposeFunctionAsync("compute", (int a, int b) => Task.FromResult(a * b));134 int result = await Page.EvaluateAsync<int>(@"async function() {135 return await compute(3, 5);136 }");137 Assert.AreEqual(15, result);138 }139 [PlaywrightTest("page-expose-function.spec.ts", "should work on frames")]140 public async Task ShouldWorkOnFrames()141 {142 await Page.ExposeFunctionAsync("compute", (int a, int b) => Task.FromResult(a * b));143 await Page.GotoAsync(Server.Prefix + "/frames/nested-frames.html");144 var frame = Page.Frames.ElementAt(1);145 int result = await frame.EvaluateAsync<int>(@"async function() {146 return await compute(3, 5);147 }");148 Assert.AreEqual(15, result);149 }150 [PlaywrightTest("page-expose-function.spec.ts", "should work on frames before navigation")]151 public async Task ShouldWorkOnFramesBeforeNavigation()152 {153 await Page.GotoAsync(Server.Prefix + "/frames/nested-frames.html");154 await Page.ExposeFunctionAsync("compute", (int a, int b) => Task.FromResult(a * b));155 var frame = Page.Frames.ElementAt(1);156 int result = await frame.EvaluateAsync<int>(@"async function() {157 return await compute(3, 5);158 }");159 Assert.AreEqual(15, result);160 }161 [PlaywrightTest("page-expose-function.spec.ts", "should work after cross origin navigation")]162 public async Task ShouldWorkAfterCrossOriginNavigation()163 {164 await Page.GotoAsync(Server.EmptyPage);165 await Page.ExposeFunctionAsync("compute", (int a, int b) => a * b);166 await Page.GotoAsync(Server.CrossProcessPrefix + "/empty.html");167 int result = await Page.EvaluateAsync<int>(@"async function() {168 return await compute(9, 4);169 }");170 Assert.AreEqual(36, result);171 }172 [PlaywrightTest("page-expose-function.spec.ts", "should work with complex objects")]173 public async Task ShouldWorkWithComplexObjects()174 {175 await Page.ExposeFunctionAsync("complexObject", (ComplexObject a, ComplexObject b) =>176 {177 return new ComplexObject { x = a.x + b.x };178 });179 var result = await Page.EvaluateAsync<ComplexObject>("async () => complexObject({ x: 5}, { x: 2})");180 Assert.AreEqual(7, result.x);181 }182 [PlaywrightTest("page-expose-function.spec.ts", "exposeBindingHandle should work")]183 public async Task ExposeBindingHandleShouldWork()184 {185 IJSHandle target = null;186 await Page.ExposeBindingAsync(187 "logme",188 (_, t) =>189 {190 target = t;191 return 17;192 });193 int result = await Page.EvaluateAsync<int>(@"async function() {194 return window['logme']({ foo: 42 });195 }");196 Assert.AreEqual(42, await target.EvaluateAsync<int>("x => x.foo"));197 Assert.AreEqual(17, result);...
ShouldWork
Using AI Code Generation
1var playwright = await Playwright.CreateAsync();2var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions3{4});5var context = await browser.NewContextAsync();6var page = await context.NewPageAsync();7await page.ExposeFunctionAsync("compute", (Func<int, int, int>)ShouldWork);8await page.EvalOnSelectorAsync("text=Get started", "button => button.click()");9await page.EvalOnSelectorAsync("text=Run in Playwright", "button => button.click()");10await page.ClickAsync("text=Close");11await page.ClickAsync("text=Close");12var playwright = await Playwright.CreateAsync();13var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions14{15});16var context = await browser.NewContextAsync();17var page = await context.NewPageAsync();18await page.ExposeFunctionAsync("compute", (Func<int, int, int>)ShouldWork);19await page.EvalOnSelectorAsync("text=Get started", "button => button.click()");20await page.EvalOnSelectorAsync("text=Run in Playwright", "button => button.click()");21await page.ClickAsync("text=Close");22await page.ClickAsync("text=Close");23var playwright = await Playwright.CreateAsync();24var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions25{26});27var context = await browser.NewContextAsync();28var page = await context.NewPageAsync();29await page.ExposeFunctionAsync("compute", (Func<int, int, int>)ShouldWork);30await page.EvalOnSelectorAsync("text=Get started", "button => button.click()");31await page.EvalOnSelectorAsync("text=Run in Playwright", "button => button.click()");32await page.ClickAsync("text=Close");33await page.ClickAsync("text=Close");
ShouldWork
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright.Tests;4using NUnit.Framework;5{6 [Parallelizable(ParallelScope.Self)]7 {8 public async Task ShouldWork()9 {10 await Page.ExposeFunctionAsync("woof", () => "doggo");11 var result = await Page.EvaluateAsync<string>("woof()");12 Assert.AreEqual("doggo", result);13 }14 }15}
ShouldWork
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright.Tests;4using Xunit;5using Xunit.Abstractions;6{7 {
ShouldWork
Using AI Code Generation
1 public async Task ShouldWork()2 {3 await Page.ExposeFunctionAsync("compute", (int a, int b) => a * b);4 var result = await Page.EvaluateAsync<int>("async function() { return await compute(9, 4); }");5 Assert.Equal(36, result);6 }7 }8}
ShouldWork
Using AI Code Generation
1var playwright = await Playwright.CreateAsync();2var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions { Headless = true });3var page = await browser.NewPageAsync();4await page.ExposeFunctionAsync("add", (int a, int b) => a + b);5var result = await page.EvaluateAsync<int>("async () => { return await add(5, 6); }");6Console.WriteLine(result);7await browser.CloseAsync();8var playwright = await Playwright.CreateAsync();9var browser = await playwright.Firefox.LaunchAsync(new LaunchOptions { Headless = true });10var page = await browser.NewPageAsync();11await page.ExposeFunctionAsync("add", (int a, int b) => a + b);12var result = await page.EvaluateAsync<int>("async () => { return await add(5, 6); }");13Console.WriteLine(result);14await browser.CloseAsync();15var playwright = await Playwright.CreateAsync();16var browser = await playwright.Webkit.LaunchAsync(new LaunchOptions { Headless = true });17var page = await browser.NewPageAsync();18await page.ExposeFunctionAsync("add", (int a, int b) => a + b);19var result = await page.EvaluateAsync<int>("async () => { return await add(5, 6); }");20Console.WriteLine(result);21await browser.CloseAsync();22var playwright = await Playwright.CreateAsync();23var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions { Headless = true });24var page = await browser.NewPageAsync();25await page.ExposeFunctionAsync("add", (int a, int b) => a + b);26var result = await page.EvaluateAsync<int>("async () => { return await add(5, 6); }");27Console.WriteLine(result);28await browser.CloseAsync();
ShouldWork
Using AI Code Generation
1using Microsoft.Playwright.Tests;2using System;3using System.Collections.Generic;4using System.Text;5using System.Threading.Tasks;6{7 {8 [PlaywrightTest("page-expose-function.spec.ts", "should work")]9 public async Task ShouldWork()10 {11 await Page.ExposeFunctionAsync("compute", new Func<int, int, int>((a, b) => a * b));12 var result = await Page.EvaluateAsync<int>("async function() { return await compute(9, 4); }");13 Assert.AreEqual(36, result);14 }15 }16}17{18 {19 private async Task ShouldWork()20 {21 await Page.ExposeFunctionAsync("compute", new Func<int, int, int>((a, b) => a * b));22 var result = await Page.EvaluateAsync<int>("async function() { return await compute(9, 4); }");23 Assert.AreEqual(36, result);
ShouldWork
Using AI Code Generation
1var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");2var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");3var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");4var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");5var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");6var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");7var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");8var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");9var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");10var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");11var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");12var result = await page.EvaluateAsync<bool>("() => window.__playwright_target__['ShouldWork']()");
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!