How to use PageEvaluateOnNewDocumentTests method of PuppeteerSharp.Tests.EvaluationTests.PageEvaluateOnNewDocumentTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.EvaluationTests.PageEvaluateOnNewDocumentTests.PageEvaluateOnNewDocumentTests

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

PageEvaluateOnNewDocumentTests.cs

Source: PageEvaluateOnNewDocumentTests.cs Github

copy
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using PuppeteerSharp.Tests.Attributes;
7using PuppeteerSharp.Xunit;
8using Xunit;
9using Xunit.Abstractions;
10
11namespace PuppeteerSharp.Tests.EvaluationTests
12{
13    [Collection(TestConstants.TestFixtureCollectionName)]
14    public class PageEvaluateOnNewDocumentTests : PuppeteerPageBaseTest
15    {
16        public PageEvaluateOnNewDocumentTests(ITestOutputHelper output) : base(output)
17        {
18        }
19
20        [PuppeteerTest("evaluation.spec.ts", "Page.evaluateOnNewDocument", "should evaluate before anything else on the page")]
21        [SkipBrowserFact(skipFirefox: true)]
22        public async Task ShouldEvaluateBeforeAnythingElseOnThePage()
23        {
24            await Page.EvaluateFunctionOnNewDocumentAsync(@"function(){
25                window.injected = 123;
26            }");
27            await Page.GoToAsync(TestConstants.ServerUrl + "/tamperable.html");
28            Assert.Equal(123, await Page.EvaluateExpressionAsync<int>("window.result"));
29        }
30
31        [PuppeteerTest("evaluation.spec.ts", "Page.evaluateOnNewDocument", "should work with CSP")]
32        [SkipBrowserFact(skipFirefox: true)]
33        public async Task ShouldWorkWithCSP()
34        {
35            Server.SetCSP("/empty.html", "script-src " + TestConstants.ServerUrl);
36            await Page.EvaluateFunctionOnNewDocumentAsync(@"function(){
37                window.injected = 123;
38            }");
39            await Page.GoToAsync(TestConstants.EmptyPage);
40            Assert.Equal(123, await Page.EvaluateExpressionAsync<int>("window.injected"));
41
42            // Make sure CSP works.
43            await Page.AddScriptTagAsync(new AddTagOptions
44            {
45                Content = "window.e = 10;"
46            }).ContinueWith(_ => Task.CompletedTask);
47            Assert.Null(await Page.EvaluateExpressionAsync("window.e"));
48        }
49
50        [SkipBrowserFact(skipFirefox: true)]
51        public async Task ShouldWorkWithExpressions()
52        {
53            await Page.EvaluateExpressionOnNewDocumentAsync("window.injected = 123;");
54            await Page.GoToAsync(TestConstants.ServerUrl + "/tamperable.html");
55            Assert.Equal(123, await Page.EvaluateExpressionAsync<int>("window.result"));
56        }
57    }
58}
59
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger PageEvaluateOnNewDocumentTests code on LambdaTest Cloud Grid

Execute automation tests with PageEvaluateOnNewDocumentTests on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)