How to use ShouldWorkWithContent method of PuppeteerSharp.Tests.PageTests.AddStyleTagTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.PageTests.AddStyleTagTests.ShouldWorkWithContent

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

AddStyleTagTests.cs

Source: AddStyleTagTests.cs Github

copy
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using Xunit;
5using Xunit.Abstractions;
6
7namespace PuppeteerSharp.Tests.PageTests
8{
9    [Collection("PuppeteerLoaderFixture collection")]
10    public class AddStyleTagTests : PuppeteerPageBaseTest
11    {
12        public AddStyleTagTests(ITestOutputHelper output) : base(output)
13        {
14        }
15
16        [Fact]
17        public async Task ShouldThrowAnErrorIfNoOptionsAreProvided()
18        {
19            var exception = await Assert.ThrowsAsync<ArgumentException>(()
20                => Page.AddStyleTagAsync(new AddTagOptions()));
21            Assert.Equal("Provide options with a `Url`, `Path` or `Content` property", exception.Message);
22        }
23
24        [Fact]
25        public async Task ShouldWorkWithAUrl()
26        {
27            await Page.GoToAsync(TestConstants.EmptyPage);
28            var styleHandle = await Page.AddStyleTagAsync(new AddTagOptions { Url = "/injectedstyle.css" });
29            Assert.NotNull(styleHandle as ElementHandle);
30            Assert.Equal("rgb(255, 0, 0)", await Page.EvaluateExpressionAsync(
31                "window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')"));
32        }
33
34        [Fact]
35        public async Task ShouldThrowAnErrorIfLoadingFromUrlFail()
36        {
37            await Page.GoToAsync(TestConstants.EmptyPage);
38            var exception = await Assert.ThrowsAsync<PuppeteerException>(()
39                => Page.AddStyleTagAsync(new AddTagOptions { Url = "/nonexistfile.js" }));
40            Assert.Equal("Loading style from /nonexistfile.js failed", exception.Message);
41        }
42
43        [Fact]
44        public async Task ShouldWorkWithAPath()
45        {
46            await Page.GoToAsync(TestConstants.EmptyPage);
47            var styleHandle = await Page.AddStyleTagAsync(new AddTagOptions { Path = "assets/injectedstyle.css" });
48            Assert.NotNull(styleHandle as ElementHandle);
49            Assert.Equal("rgb(255, 0, 0)", await Page.EvaluateExpressionAsync(
50                "window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')"));
51        }
52
53        [Fact]
54        public async Task ShouldIncludeSourcemapWhenPathIsProvided()
55        {
56            await Page.GoToAsync(TestConstants.EmptyPage);
57            await Page.AddStyleTagAsync(new AddTagOptions
58            {
59                Path = Path.Combine(Directory.GetCurrentDirectory(), Path.Combine("assets", "injectedstyle.css"))
60            });
61            var styleHandle = await Page.QuerySelectorAsync("style");
62            var styleContent = await Page.EvaluateFunctionAsync<string>("style => style.innerHTML", styleHandle);
63            Assert.Contains(Path.Combine("assets", "injectedstyle.css"), styleContent);
64        }
65
66        [Fact]
67        public async Task ShouldWorkWithContent()
68        {
69            await Page.GoToAsync(TestConstants.EmptyPage);
70            var styleHandle = await Page.AddStyleTagAsync(new AddTagOptions { Content = "body { background-color: green; }" });
71            Assert.NotNull(styleHandle as ElementHandle);
72            Assert.Equal("rgb(0, 128, 0)", await Page.EvaluateExpressionAsync(
73                "window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')"));
74        }
75    }
76}
77
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 ShouldWorkWithContent code on LambdaTest Cloud Grid

Execute automation tests with ShouldWorkWithContent 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)