Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.TestUtils.AssertCSPError
TestUtils.cs
Source:TestUtils.cs
...61 {62 StringAssert.Contains("SSL_ERROR_UNKNOWN", errorMessage);63 }64 }65 internal static void AssertCSPError(string errorMessage)66 {67 if (TestConstants.IsWebKit)68 {69 StringAssert.StartsWith("Refused to execute a script because its hash, its nonce, or 'unsafe-inline' appears in neither the script-src directive nor the default-src directive of the Content Security Policy.", errorMessage);70 }71 else if (TestConstants.IsFirefox)72 {73 StringAssert.StartsWith("[JavaScript Error: \"Content Security Policy: The pageâs settings blocked the loading of a resource at inline (âdefault-srcâ).\"", errorMessage);74 }75 else76 {77 StringAssert.StartsWith("Refused to execute inline script because it violates the following Content Security Policy directive: \"default-src 'self'\".", errorMessage);78 }79 }...
BrowserContextCSPTests.cs
Source:BrowserContextCSPTests.cs
...36 {37 var page = await context.NewPageAsync();38 await page.GotoAsync(Server.Prefix + "/csp.html");39 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => page.AddScriptTagAsync(new() { Content = "window.__injected = 42;" }));40 TestUtils.AssertCSPError(exception.Message);41 Assert.Null(await page.EvaluateAsync("window.__injected"));42 }43 // By-pass CSP and try one more time.44 await using (var context = await Browser.NewContextAsync(new() { BypassCSP = true }))45 {46 var page = await context.NewPageAsync();47 await page.GotoAsync(Server.Prefix + "/csp.html");48 await page.AddScriptTagAsync(new() { Content = "window.__injected = 42;" });49 Assert.AreEqual(42, await page.EvaluateAsync<int>("window.__injected"));50 }51 }52 [PlaywrightTest("browsercontext-csp.spec.ts", "should bypass CSP header")]53 public async Task ShouldBypassCSPHeader()54 {55 // Make sure CSP prohibits addScriptTag.56 Server.SetCSP("/empty.html", "default-src 'self'");57 await using (var context = await Browser.NewContextAsync())58 {59 var page = await context.NewPageAsync();60 await page.GotoAsync(Server.EmptyPage);61 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => page.AddScriptTagAsync(new() { Content = "window.__injected = 42;" }));62 TestUtils.AssertCSPError(exception.Message);63 Assert.Null(await page.EvaluateAsync("window.__injected"));64 }65 // By-pass CSP and try one more time.66 await using (var context = await Browser.NewContextAsync(new() { BypassCSP = true }))67 {68 var page = await context.NewPageAsync();69 await page.GotoAsync(Server.EmptyPage);70 await page.AddScriptTagAsync(new() { Content = "window.__injected = 42;" });71 Assert.AreEqual(42, await page.EvaluateAsync<int>("window.__injected"));72 }73 }74 [PlaywrightTest("browsercontext-csp.spec.ts", "should bypass after cross-process navigation")]75 public async Task ShouldBypassAfterCrossProcessNavigation()76 {77 await using var context = await Browser.NewContextAsync(new() { BypassCSP = true });78 var page = await context.NewPageAsync();79 await page.GotoAsync(Server.Prefix + "/csp.html");80 await page.AddScriptTagAsync(new() { Content = "window.__injected = 42;" });81 Assert.AreEqual(42, await page.EvaluateAsync<int>("window.__injected"));82 await page.GotoAsync(Server.CrossProcessPrefix + "/csp.html");83 await page.AddScriptTagAsync(new() { Content = "window.__injected = 42;" });84 Assert.AreEqual(42, await page.EvaluateAsync<int>("window.__injected"));85 }86 [PlaywrightTest("browsercontext-csp.spec.ts", "should bypass CSP in iframes as well")]87 public async Task ShouldBypassCSPInIframesAsWell()88 {89 await using (var context = await Browser.NewContextAsync())90 {91 var page = await context.NewPageAsync();92 await page.GotoAsync(Server.EmptyPage);93 // Make sure CSP prohibits addScriptTag in an iframe.94 var frame = await FrameUtils.AttachFrameAsync(page, "frame1", Server.Prefix + "/csp.html");95 var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => frame.AddScriptTagAsync(new() { Content = "window.__injected = 42;" }));96 TestUtils.AssertCSPError(exception.Message);97 Assert.Null(await frame.EvaluateAsync<int?>("() => window.__injected"));98 }99 // By-pass CSP and try one more time.100 await using (var context = await Browser.NewContextAsync(new() { BypassCSP = true }))101 {102 var page = await context.NewPageAsync();103 await page.GotoAsync(Server.EmptyPage);104 // Make sure CSP prohibits addScriptTag in an iframe.105 var frame = await FrameUtils.AttachFrameAsync(page, "frame1", Server.Prefix + "/csp.html");106 await frame.AddScriptTagAsync(new() { Content = "window.__injected = 42;" }).ContinueWith(_ => Task.CompletedTask);107 Assert.AreEqual(42, await frame.EvaluateAsync<int?>("() => window.__injected"));108 }109 }110 }...
AssertCSPError
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Text;4using System.Threading.Tasks;5using Microsoft.Playwright;6using Xunit;7using Xunit.Abstractions;8{9 {10 public CSPTests(ITestOutputHelper output) : base(output)11 {12 }13 [PlaywrightTest("csp.spec.ts", "should report CSP violations")]14 [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]15 public async Task ShouldReportCSPViolations()16 {17 await Page.GotoAsync(Server.EmptyPage);18 await Page.AddScriptTagAsync(new()19 {20 Content = "var somethingveryevil = true;"21 });22 await Page.EvaluateAsync("() => window.__injected = 42");23 var errors = await Page.CspErrorsAsync();24 Assert.Single(errors);25 Assert.Equal("script-src", errors[0].Directive);26 Assert.Equal("script-src", errors[0].EffectiveDirective);27 Assert.Equal("script", errors[0].ViolatedDirective);28 Assert.Equal("unsafe-eval", errors[0].Disposition);29 Assert.Equal("var somethingveryevil = true;", errors[0].BlockedURL);30 Assert.Equal("42", errors[0].StatusCode.ToString());31 Assert.Equal("script", errors[0].SourceFile);32 Assert.Equal(1, errors[0].LineNumber);33 Assert.Equal(0, errors[0].ColumnNumber);34 }35 [PlaywrightTest("csp.spec.ts", "should report CSP violations with a frame")]36 [Fact(Timeout = PlaywrightSharp.Playwright.DefaultTimeout)]37 public async Task ShouldReportCSPViolationsWithAFrame()38 {39 await Page.GotoAsync(Server.EmptyPage);40 await Page.SetContentAsync($@"41 <iframe src='{Server.CrossProcessPrefix}/csp.html'></iframe>42 ");43 var frame = Page.FirstChildFrame();44 await frame.AddScriptTagAsync(new()45 {46 Content = "var somethingveryevil = true;"47 });48 await frame.EvaluateAsync("() => window.__injected = 42");49 var errors = await frame.CspErrorsAsync();50 Assert.Single(errors);51 Assert.Equal("script-src", errors[0].Directive);52 Assert.Equal("script-src", errors[0].EffectiveDirective);
AssertCSPError
Using AI Code Generation
1var page = await browser.NewPageAsync();2await page.GotoAsync(Server.Prefix + "/csp.html");3await page.EvaluateAsync("() => {4 var script = document.createElement('script');5 script.src = '/injectedfile.js';6 script.onload = () => console.log('injectedfile.js loaded');7 document.body.appendChild(script);8}");9await TestUtils.AssertCSPError(page, "script-src 'self' 'sha256-5XVX1Hx9XhVzYkY7lZJzFZl7KZg='");10await page.CloseAsync();
AssertCSPError
Using AI Code Generation
1var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions2{3 Args = new[] { "--enable-logging", "--v=1" },4 Args = new[] { "--enable-logging", "--v=1" },5});6var context = await browser.NewContextAsync();7var page = await context.NewPageAsync();
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!!