Skip to content

Commit

Permalink
Merge pull request #2745 from hardkoded/v19-docs
Browse files Browse the repository at this point in the history
Prepare docs for v19
  • Loading branch information
kblok authored Aug 20, 2024
2 parents 9aa23f3 + ec36c08 commit 4a007ae
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
4 changes: 2 additions & 2 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
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

0 comments on commit 4a007ae

Please sign in to comment.