Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time


build CodeFactor

PuppeteerSharp.Contrib.Extensions is a library with convenient extension methods for writing browser tests with the Puppeteer Sharp API.



NuGet PuppeteerSharp.Contrib.Extensions
Package Manager PM> Install-Package PuppeteerSharp.Contrib.Extensions -Version 5.0.0
.NET CLI > dotnet add package PuppeteerSharp.Contrib.Extensions --version 5.0.0
PackageReference <PackageReference Include="PuppeteerSharp.Contrib.Extensions" Version="5.0.0" />
Paket CLI > paket add PuppeteerSharp.Contrib.Extensions --version 5.0.0

Extensions for Page


  • QuerySelectorWithContentAsync
  • QuerySelectorAllWithContentAsync


  • HasContentAsync
  • HasTitleAsync

Extensions for ElementHandle


  • ClassListAsync
  • ClassNameAsync
  • GetAttributeAsync
  • HrefAsync
  • IdAsync
  • NameAsync
  • SrcAsync
  • ValueAsync


  • InnerHtmlAsync
  • InnerTextAsync
  • OuterHtmlAsync
  • TextContentAsync


  • Exists
  • HasAttributeAsync
  • HasClassAsync
  • HasContentAsync
  • HasFocusAsync
  • IsCheckedAsync
  • IsDisabledAsync
  • IsEnabledAsync
  • IsHiddenAsync
  • IsReadOnlyAsync
  • IsRequiredAsync
  • IsSelectedAsync
  • IsVisibleAsync


  • QuerySelectorWithContentAsync
  • QuerySelectorAllWithContentAsync


Sample projects are located in the samples folder.

This is an example with NUnit:

using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using PuppeteerSharp.Contrib.Extensions;

namespace PuppeteerSharp.Contrib.Sample
    public class ExtensionsTests
        Browser Browser { get; set; }
        Page Page { get; set; }

        public async Task SetUp()
            await new BrowserFetcher().DownloadAsync();
            Browser = await Puppeteer.LaunchAsync(new LaunchOptions
                Headless = true
            Page = await Browser.NewPageAsync();

        public async Task TearDown()
            await Browser.CloseAsync();

        public async Task Query()
            await Page.SetContentAsync(@"
  <div id='foo'>Foo</div>
  <div id='bar'>Bar</div>
  <div id='baz'>Baz</div>

            var html = await Page.QuerySelectorAsync("html");

            var div = await Page.QuerySelectorWithContentAsync("div", "Ba.");
            Assert.AreEqual("bar", await div.IdAsync());

            div = await html.QuerySelectorWithContentAsync("div", "Ba.");
            Assert.AreEqual("bar", await div.IdAsync());

            var divs = await Page.QuerySelectorAllWithContentAsync("div", "Ba.");
            Assert.AreEqual(new[] { "bar", "baz" }, await Task.WhenAll(divs.Select(x => x.IdAsync())));

            divs = await html.QuerySelectorAllWithContentAsync("div", "Ba.");
            Assert.AreEqual(new[] { "bar", "baz" }, await Task.WhenAll(divs.Select(x => x.IdAsync())));

        public async Task Attributes()
            await Page.SetContentAsync(@"
  <form method='post'>
      Name: <input type='text' name='name' id='name' required>
      Email: <input type='email' name='email' id='email' required>
      <input type='submit' value='Subscribe!'>
  <img src='unsubscribe.png' />
  <a href='/unsubscribe/'>Unsubscribe</a>

            var form = await Page.QuerySelectorAsync("form");
            Assert.AreEqual("post", await form.GetAttributeAsync("method"));

            var input = await Page.QuerySelectorAsync("#name");
            Assert.True(await input.HasAttributeAsync("required"));

            var link = await Page.QuerySelectorAsync("a");
            Assert.AreEqual("/unsubscribe/", await link.HrefAsync());

            input = await Page.QuerySelectorAsync("input[type=email]");
            Assert.AreEqual("email", await input.IdAsync());

            input = await Page.QuerySelectorAsync("#email");
            Assert.AreEqual("email", await input.NameAsync());

            var img = await Page.QuerySelectorAsync("img");
            Assert.AreEqual("unsubscribe.png", await img.SrcAsync());

            input = await Page.QuerySelectorAsync("input[type=submit]");
            Assert.AreEqual("Subscribe!", await input.ValueAsync());

        public async Task Class()
            await Page.SetContentAsync("<div class='foo bar' />");

            var div = await Page.QuerySelectorAsync("div");
            Assert.AreEqual("foo bar", await div.ClassNameAsync());
            Assert.AreEqual(new[] { "foo", "bar" }, await div.ClassListAsync());
            Assert.True(await div.HasClassAsync("bar"));

        public async Task Content()
            await Page.SetContentAsync(@"

            var html = await Page.QuerySelectorAsync("html");
            Assert.True(await html.HasContentAsync("Foo"));

            var div = await Page.QuerySelectorAsync("div");
            Assert.AreEqual("\n    Foo\n    <span>Bar</span>\n  ", await div.InnerHtmlAsync());
            Assert.AreEqual("<div>\n    Foo\n    <span>Bar</span>\n  </div>", await div.OuterHtmlAsync());
            Assert.AreEqual("Foo Bar", await div.InnerTextAsync());
            Assert.AreEqual("\n    Foo\n    Bar\n  ", await div.TextContentAsync());

        public async Task Visibility()
            await Page.SetContentAsync("<div>Foo</div>");

            var div = await Page.QuerySelectorAsync("div");
            Assert.False(await div.IsHiddenAsync());
            Assert.True(await div.IsVisibleAsync());

        public async Task Input()
            await Page.SetContentAsync(@"
  <input type='text' autofocus required>
  <input type='radio' readonly>
  <input type='checkbox' checked>
    <option id='foo'>Foo</option>
    <option id='bar'>Bar</option>
", new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.Networkidle0 } });

            var input = await Page.QuerySelectorAsync("input[type=text]");
            Assert.True(await input.HasFocusAsync());
            Assert.True(await input.IsRequiredAsync());

            input = await Page.QuerySelectorAsync("input[type=radio]");
            Assert.False(await input.IsDisabledAsync());
            Assert.True(await input.IsEnabledAsync());
            Assert.True(await input.IsReadOnlyAsync());

            input = await Page.QuerySelectorAsync("input[type=checkbox]");
            Assert.True(await input.IsCheckedAsync());

            input = await Page.QuerySelectorAsync("#foo");
            Assert.True(await input.IsSelectedAsync());