Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into protocol-using-webd…
Browse files Browse the repository at this point in the history
…river-driver

# Conflicts:
#	.github/workflows/dotnet.yml
#	lib/PuppeteerSharp.Nunit/PuppeteerTestAttribute.cs
#	lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json
#	lib/PuppeteerSharp.Tests/BrowserTests/ProcessTests.cs
#	lib/PuppeteerSharp.Tests/LauncherTests/PuppeteerConnectTests.cs
#	lib/PuppeteerSharp.Tests/PuppeteerSharp.Tests.csproj
#	lib/PuppeteerSharp.Tooling/PuppeteerSharp.Tooling.csproj
#	lib/PuppeteerSharp/ConnectOptions.cs
#	lib/PuppeteerSharp/Launcher.cs
#	lib/PuppeteerSharp/PuppeteerSharp.csproj
  • Loading branch information
kblok committed Aug 22, 2024
2 parents da707c7 + 3315b38 commit d451054
Show file tree
Hide file tree
Showing 390 changed files with 3,989 additions and 3,520 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ on:
- master
- release-*
pull_request:
branches: [master]
paths:
- "**.cs"
- "**.csproj"
Expand Down Expand Up @@ -35,7 +34,18 @@ jobs:
working-directory: ./demos/PuppeteerSharpPdfDemo
run: |
dotnet restore PuppeteerSharpPdfDemo-Local.csproj
- name: Run Project
- name: Run on .NET
working-directory: ./demos/PuppeteerSharpPdfDemo
run: |
dotnet run --project PuppeteerSharpPdfDemo-Local.csproj auto-exit
dotnet run --project PuppeteerSharpPdfDemo-Local.csproj auto-exit -f net8.0
- name: Run with AOT
if: matrix.os == 'windows-2022'
working-directory: ./demos/PuppeteerSharpPdfDemo
run: |
dotnet publish PuppeteerSharpPdfDemo-Local.csproj -r win-x64 -o build -f net8.0
build/PuppeteerSharpPdfDemo-Local.exe auto-exit
- name: Run on .NET Framework
if: matrix.os == 'windows-2022'
working-directory: ./demos/PuppeteerSharpPdfDemo
run: |
dotnet run --project PuppeteerSharpPdfDemo-Local.csproj auto-exit -f net471
7 changes: 3 additions & 4 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ on:
- release-*
merge_group:
pull_request:
branches: [ master ]
paths:
- '**.yml'
- '**.cs'
Expand Down Expand Up @@ -98,14 +97,14 @@ jobs:
Get-ChildItem -Path cert:\CurrentUSer\my | where { $_.friendlyname -eq "Puppeteer" } | Export-Certificate -FilePath $env:GITHUB_WORKSPACE\lib\PuppeteerSharp.TestServer\testCert.cer
- name: Check formatting
if: ${{ matrix.os == 'ubuntu-latest' && matrix.browser == 'CHROME' && matrix.mode == 'headless' && matrix.protocol == 'cdp' }}
run: dotnet format ./lib/PuppeteerSharp.sln --verify-no-changes
run: dotnet format ./lib/PuppeteerSharp.sln --verify-no-changes --exclude-diagnostics CA1865
- name: Build
working-directory: lib
run: dotnet build PuppeteerSharp.sln
- name: Test (Linux)
if: matrix.os == 'ubuntu-latest'
env:
PRODUCT: ${{ matrix.browser }}
BROWSER: ${{ matrix.browser }}
HEADLESS_MODE: ${{ matrix.mode }}
PROTOCOL: ${{ matrix.protocol }}
run: |
Expand All @@ -116,7 +115,7 @@ jobs:
- name: Test (Windows)
if: matrix.os == 'windows-latest'
env:
PRODUCT: ${{ matrix.browser }}
BROWSER: ${{ matrix.browser }}
HEADLESS_MODE: ${{ matrix.mode }}
PROTOCOL: ${{ matrix.protocol }}
run: |
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Puppeteer Sharp is a .NET port of the official [Node.JS Puppeteer API](https://g

## Recent news

* [Puppeteer-Sharp 2023 recap](https://www.hardkoded.com/blog/puppeteer-sharp-2023-recap).
PuppeteerSharp now supports AOT compilation! Check the [PuppeteerSharp 19 release notes!](https://github.com/hardkoded/puppeteer-sharp/releases/tag/v19.0.0).

## Useful links

Expand All @@ -28,7 +28,7 @@ Puppeteer Sharp is a .NET port of the official [Node.JS Puppeteer API](https://g

## Prerequisites

* As Puppeteer-Sharp is a NetStandard 2.0 library, the minimum platform versions are .NET Framework 4.6.1 and .NET Core 2.0. [Read more](https://docs.microsoft.com/en-us/dotnet/standard/net-standard).
* Puppeteer-Sharp comes in two flavors: a NetStandard 2.0 library for .NET Framework 4.6.1 and .NET Core 2.0 or greater and a .NET 8 version.
* If you have issues running Chrome on Linux, the Puppeteer repo has a [great troubleshooting guide](https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md).
* X-server is required on Linux.

Expand Down Expand Up @@ -60,7 +60,7 @@ await using var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.ScreenshotAsync(outputFile);
```
<sup><a href='https://github.com/hardkoded/puppeteer-sharp/blob/master/lib/PuppeteerSharp.Tests/ScreenshotTests/PageScreenshotTests.cs#L53-L61' title='Snippet source file'>snippet source</a> | <a href='#snippet-ScreenshotAsync' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/hardkoded/puppeteer-sharp/blob/master/lib/PuppeteerSharp.Tests/ScreenshotTests/PageScreenshotTests.cs#L54-L62' title='Snippet source file'>snippet source</a> | <a href='#snippet-ScreenshotAsync' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

You can also change the view port before generating the screenshot
Expand Down Expand Up @@ -112,10 +112,10 @@ var result = await page.GetContentAsync();
```cs
await using var page = await browser.NewPageAsync();
var seven = await page.EvaluateExpressionAsync<int>("4 + 3");
var someObject = await page.EvaluateFunctionAsync<dynamic>("(value) => ({a: value})", 5);
Console.WriteLine(someObject.a);
var someObject = await page.EvaluateFunctionAsync<JsonElement>("(value) => ({a: value})", 5);
Console.WriteLine(someObject.GetProperty("a").GetString());
```
<sup><a href='https://github.com/hardkoded/puppeteer-sharp/blob/master/lib/PuppeteerSharp.Tests/QuerySelectorTests/ElementHandleQuerySelectorEvalTests.cs#L16-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-Evaluate' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/hardkoded/puppeteer-sharp/blob/master/lib/PuppeteerSharp.Tests/QuerySelectorTests/ElementHandleQuerySelectorEvalTests.cs#L17-L22' title='Snippet source file'>snippet source</a> | <a href='#snippet-Evaluate' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

### Wait For Selector
Expand Down Expand Up @@ -172,6 +172,6 @@ A massive thanks to [AWS](https://github.com/aws), who sponsored Puppeteer-sharp

And a huge thanks to everyone who sponsors this project through [Github sponsors](https://github.com/sponsors/hardkoded):

<!-- sponsors --><a href="https://github.com/tolgabalci"><img src="https://github.com/tolgabalci.png" width="60px" alt="Tolga Balci" /></a><a href="https://github.com/nogginbox"><img src="https://github.com/nogginbox.png" width="60px" alt="Richard Garside" /></a><a href="https://github.com/aws"><img src="https://github.com/aws.png" width="60px" alt="Amazon Web Services" /></a><a href="https://github.com/MarkPflug"><img src="https://github.com/MarkPflug.png" width="60px" alt="Mark Pflug" /></a><!-- sponsors -->
<!-- sponsors --><a href="https://github.com/tolgabalci"><img src="https://github.com/tolgabalci.png" width="60px" alt="Tolga Balci" /></a><a href="https://github.com/nogginbox"><img src="https://github.com/nogginbox.png" width="60px" alt="Richard Garside" /></a><a href="https://github.com/aws"><img src="https://github.com/aws.png" width="60px" alt="Amazon Web Services" /></a><!-- sponsors -->


22 changes: 22 additions & 0 deletions demos/PuppeteerSharpPdfDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.IO;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Threading.Tasks;
using PuppeteerSharp;

Expand All @@ -10,6 +12,10 @@ class MainClass
{
public static async Task Main(string[] args)
{
#if NET8_0_OR_GREATER
Puppeteer.ExtraJsonSerializerContext = DemoJsonSerializationContext.Default;
#endif

var options = new LaunchOptions { Headless = true };

Console.WriteLine("Downloading chromium");
Expand All @@ -28,10 +34,26 @@ public static async Task Main(string[] args)

Console.WriteLine("Export completed");

#if NET8_0_OR_GREATER
// AOT Test
var result = await page.EvaluateFunctionAsync<TestClass>("test => test", new TestClass { Name = "Dario"});
Console.WriteLine($"Name evaluated to {result.Name}");
#endif
if (!args.Any(arg => arg == "auto-exit"))
{
Console.ReadLine();
}
}
}

#if NET8_0_OR_GREATER
public class TestClass
{
public string Name { get; set; }
}

[JsonSerializable(typeof(TestClass))]
public partial class DemoJsonSerializationContext : JsonSerializerContext
{}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<TargetFrameworks>net8.0;net471</TargetFrameworks>
<LangVersion>12</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PublishAot>true</PublishAot>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\lib\PuppeteerSharp\PuppeteerSharp.csproj" />
Expand Down
70 changes: 70 additions & 0 deletions docfx_project/examples/AOT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# How to test a Chrome Extension
_Contributors: [Dario Kondratiuk](https://github.com/kblok)_

## Problem

You need to use Puppeteer Sharp in an application set up for AOT compilation.

## Solution

You shouldn't need to do anything special to use Puppeteer Sharp in an AOT environment. The library is already prepared for it.\
The only challenge you might face is if you use any custom class to pass into or get from an Evaluate function. In that case you will need to provide a serialization context to PuppeteerSharp.\
Let's say you have a class like this:

```csharp
public class TestClass
{
public string Name { get; set; }
}
```

You need to create a serialization context like this:

```csharp
[JsonSerializable(typeof(TestClass))]
public partial class DemoJsonSerializationContext : JsonSerializerContext
{}

```

_For more information about `JsonSerializerContext` see [How to use source generation in System.Text.Json](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation?WT.mc_id=DT-MVP-5003814)._

Once you have your own context you have to pass it to PuppeteerSharp before launching the browser:

```csharp
Puppeteer.ExtraJsonSerializerContext = DemoJsonSerializationContext.Default;
```

`ExtraJsonSerializerContext` will be used the first time PuppeteerSharp serializes or deserializes any object. So it's important to set it before launching the browser. Once set, you can't change it.

## Example

```csharp
class MainClass
{
public static async Task Main(string[] args)
{
Puppeteer.ExtraJsonSerializerContext = DemoJsonSerializationContext.Default;
var options = new LaunchOptions { Headless = true };

var browserFetcher = new BrowserFetcher();
await browserFetcher.DownloadAsync();

await using var browser = await Puppeteer.LaunchAsync(options);
await using var page = await browser.NewPageAsync();

await page.GoToAsync("https://www.google.com");

var result = await page.EvaluateFunctionAsync<TestClass>("test => test", new TestClass { Name = "Dario"});
}
}

public class TestClass
{
public string Name { get; set; }
}

[JsonSerializable(typeof(TestClass))]
public partial class DemoJsonSerializationContext : JsonSerializerContext
{}
```
2 changes: 2 additions & 0 deletions docfx_project/examples/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
href: Page.Request.md
- name: How to download an specific browser
href: DownloadFetcher.Download.md
- name: Using PuppeteerSharp with AOT compilation
href: AOT.md
- name: Advanced
items:
- name: How to log CDP communication
Expand Down
16 changes: 15 additions & 1 deletion lib/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ dotnet_diagnostic.CA1054.severity = none
dotnet_diagnostic.CA1056.severity = none
# Avoid empty interfaces
dotnet_diagnostic.CA1040.severity = none
# Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance
# This is not netstandard compatible
dotnet_diagnostic.CA1510.severity = none
# Use 'string.StartsWith(char)' instead of 'string.StartsWith(string)' when you have a string with a single char
# This is not .NET 8 compatible
dotnet_diagnostic.CA1865.severity = none
# Use 'ObjectDisposedException.ThrowIf' instead of explicitly throwing a new exception instance
# This is not netstandard compatible
dotnet_diagnostic.CA1513.severity = none
# Use Change the 'WriteAsync' method call to use the 'Stream.WriteAsync(ReadOnlyMemory<byte>, CancellationToken)' overload
# This is not netstandard compatible
dotnet_diagnostic.CA1835.severity = none
# Use ConfigureAwait
dotnet_diagnostic.CA2007.severity = error
# CA1711: Identifiers should not have incorrect suffix
Expand All @@ -264,6 +276,8 @@ dotnet_diagnostic.CA1815.severity = suggestion
# CA1848: Use the LoggerMessage delegates
# TODO: REMOVE
dotnet_diagnostic.CA1848.severity = none
# internal sealed
dotnet_diagnostic.CA1852.severity = error
# CA1859: Use concrete types when possible for improved performance
# It collides with https://www.jetbrains.com/help/rider/ReturnTypeCanBeEnumerable.Local.html
dotnet_diagnostic.CA1859.severity = none
Expand Down Expand Up @@ -303,7 +317,7 @@ dotnet_diagnostic.CA1001.severity = error
dotnet_diagnostic.CA1304.severity = error
# CA1305: String.Format with culture
dotnet_diagnostic.CA1305.severity = error
# CA1304: CA1308: Normalize strings to uppercase
# CA1308: Normalize strings to uppercase
dotnet_diagnostic.CA1308.severity = none
# CA1721: The property name 'DefaultArgs' is confusing given the existence of method 'GetDefaultArgs'. Rename or remove one of these members.
dotnet_diagnostic.CA1721.severity = error
Expand Down
2 changes: 1 addition & 1 deletion lib/Common/CommonProps.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
</None>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="dotnet format $(ProjectPath)" />
<Exec Command="dotnet format $(ProjectPath) --exclude-diagnostics CA1865" />
</Target>
</Project>
8 changes: 4 additions & 4 deletions lib/PuppeteerSharp.DevicesFetcher/Device.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace PuppeteerSharp.DevicesFetcher
{
public class Device
{
[JsonProperty("userAgent")]
[JsonPropertyName("userAgent")]
public string UserAgent { get; set; }
[JsonProperty("name")]
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonProperty("viewport")]
[JsonPropertyName("viewport")]
public ViewPort Viewport { get; set; }
}
}
13 changes: 3 additions & 10 deletions lib/PuppeteerSharp.DevicesFetcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace PuppeteerSharp.DevicesFetcher
{
static class Program
Expand Down Expand Up @@ -34,7 +35,7 @@ static async Task Main(string[] args)
Device[] devices;
try
{
devices = JsonConvert.DeserializeObject<Device[]>(text);
devices = JsonSerializer.Deserialize<Device[]>(text);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -70,14 +71,6 @@ public static class DeviceDescriptors
new Lazy<IReadOnlyDictionary<DeviceDescriptorName, DeviceDescriptor>>(() => new ReadOnlyDictionary<DeviceDescriptorName, DeviceDescriptor>(Devices));
internal static IReadOnlyDictionary<DeviceDescriptorName, DeviceDescriptor> ToReadOnly() => _readOnlyDevices.Value;
/// <summary>
/// Get the specified device description.
/// </summary>
/// <returns>The device descriptor.</returns>
/// <param name=""name"">Device Name.</param>
[Obsolete(""Use Puppeteer.Devices instead"")]
public static DeviceDescriptor Get(DeviceDescriptorName name) => Devices[name];
}
}";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PuppeteerSharp\PuppeteerSharp.csproj" />
</ItemGroup>
Expand Down
14 changes: 7 additions & 7 deletions lib/PuppeteerSharp.DevicesFetcher/ViewPort.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace PuppeteerSharp.DevicesFetcher
{
public class ViewPort
{
[JsonProperty("width")]
[JsonPropertyName("width")]
public int Width { get; set; }
[JsonProperty("height")]
[JsonPropertyName("height")]
public int Height { get; set; }

[JsonProperty("deviceScaleFactor")]
[JsonPropertyName("deviceScaleFactor")]
public double DeviceScaleFactor { get; set; }

[JsonProperty("isMobile")]
[JsonPropertyName("isMobile")]
public bool IsMobile { get; set; }
[JsonProperty("hasTouch")]
[JsonPropertyName("hasTouch")]
public bool HasTouch { get; set; }
[JsonProperty("isLandscape")]
[JsonPropertyName("isLandscape")]
public bool IsLandscape { get; set; }
}
}
Loading

0 comments on commit d451054

Please sign in to comment.