Skip to content

Commit

Permalink
Migrate to system.text.json (#2713)
Browse files Browse the repository at this point in the history
* Some progress

* More progress

* more progress

* More progress

* Docs changes

* Library builds

* More progress

* Solution builds

* Docs changes

* format

* It seems that the demo runs

* Fix expectation loading

* Fix accessibility tests

* Fix tests using json

* Docs changes

* Fix aria tests and RemoteObject type checks

* Fix more accessibility steps and chrome version checker

* more json fixes

* Fix enum parser

* Fix page event test

* fix bounding box

* Fix CSS Coverage Test

* Fix console tests

* Docs changes

* Add enum member to MouseButton

* Improve enums

* Fix tracing tests

* Fix JS coverage tests

* Fix json tests

* Fix SetUserAgentTests

* Fix QueryAllTests

* Fix security details

* Fix keyboard tests

* Fix ResponseJsonTests

* Fix ElementHandleScreenshotTests

* Some early review

* Internal sealed converters

* fix condition

* Make visible to the Nunit project

* Fix touch tests

* Fix JS Coverage tests

* Fix screenshot rounding

* Fix evaluate with nulls

* Fix evaluation exceptions

* Fix complex objects test

* Fix JS Handle validation

* Fix complex object tests

* Remove Mock verbosity

* Fix wait for frame

* Fix console logs

* Fix PageWorkerTests

* Convert context id in Firefox

* Flag beta version

* Restore some json config

* undo a change

* We don't need this

* AOT Support!

* Remove extra types

* Small .NET framework test

* Test AOT

* AOT only on net8.0

* .NET Framework does not like generic attributes

* remove remaining newtonsoft

* remove unused imports

* Docs changes

* some styles

* internal sealed

* Include System.Text.Json only for net standard

* remove extra constructor

* Update lib/PuppeteerSharp/Helpers/Json/AnyTypeToStringConverter.cs

Co-authored-by: Jonas Nyrup <[email protected]>

* cr

* cr

* Way too generic serializing attribute

* Update lib/PuppeteerSharp.Nunit/PuppeteerTestAttribute.cs

Co-authored-by: campersau <[email protected]>

* Improve serialization

* Add more types

* rollback AOT by default

* AOT tests

* Docs changes

* beta2

* Revert "AOT tests"

This reverts commit 0d94b1b.

* You can't AOT a test

* Docs changes

* Add AOT demo

---------

Co-authored-by: GitHub Action <[email protected]>
Co-authored-by: Jonas Nyrup <[email protected]>
Co-authored-by: campersau <[email protected]>
  • Loading branch information
4 people authored Aug 8, 2024
1 parent c4c67db commit e8eac9a
Show file tree
Hide file tree
Showing 264 changed files with 1,433 additions and 1,119 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ 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 == 'macos-latest'
working-directory: ./demos/PuppeteerSharpPdfDemo
run: |
dotnet run --project PuppeteerSharpPdfDemo-Local.csproj -r osx-arm64 -c Release -f net8.0 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
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ 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' }}
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
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
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
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
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; }
}
}
5 changes: 3 additions & 2 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
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; }
}
}
1 change: 0 additions & 1 deletion lib/PuppeteerSharp.Nunit/PuppeteerSharp.Nunit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<LangVersion>12</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NUnit" Version="3.13.3" />
</ItemGroup>
<ItemGroup>
Expand Down
15 changes: 13 additions & 2 deletions lib/PuppeteerSharp.Nunit/PuppeteerTestAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using System.Text.Json;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using PuppeteerSharp.Helpers.Json;
using PuppeteerSharp.Nunit.TestExpectations;

namespace PuppeteerSharp.Nunit
Expand Down Expand Up @@ -170,14 +171,24 @@ private static TestExpectation[] GetLocalExpectations() =>
private static TestExpectation[] GetUpstreamExpectations() =>
_upstreamExpectations ??= LoadExpectationsFromResource("PuppeteerSharp.Nunit.TestExpectations.TestExpectations.upstream.json");

private static readonly JsonSerializerOptions DefaultJsonSerializerOptions =
new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumMemberConverter(),
},
};

private static TestExpectation[] LoadExpectationsFromResource(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();

using var stream = assembly.GetManifestResourceStream(resourceName);
using var reader = new StreamReader(stream);
var fileContent = reader.ReadToEnd();
return JsonConvert.DeserializeObject<TestExpectation[]>(fileContent);
return JsonSerializer.Deserialize<TestExpectation[]>(fileContent, DefaultJsonSerializerOptions);
}
}
}
14 changes: 9 additions & 5 deletions lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace PuppeteerSharp.Nunit.TestExpectations;

Expand Down Expand Up @@ -56,7 +54,6 @@ public Regex TestIdRegex

public TestExpectationResult[] Expectations { get; set; }

[JsonConverter(typeof(StringEnumConverter))]
public enum TestExpectationResult
{
[EnumMember(Value = "FAIL")] Fail,
Expand All @@ -65,24 +62,31 @@ public enum TestExpectationResult
[EnumMember(Value = "TIMEOUT")] Timeout,
}

[JsonConverter(typeof(StringEnumConverter))]
public enum TestExpectationsParameter
{
[EnumMember(Value = "firefox")]
Firefox,
[EnumMember(Value = "chrome")]
Chrome,
[EnumMember(Value = "webDriverBiDi")]
WebDriverBiDi,
[EnumMember(Value = "cdp")]
Cdp,
[EnumMember(Value = "chrome-headless-shell")]
ChromeHeadlessShell,
[EnumMember(Value = "headless")]
Headless,
[EnumMember(Value = "headful")]
Headful,
}

[JsonConverter(typeof(StringEnumConverter))]
public enum TestExpectationPlatform
{
[EnumMember(Value = "darwin")]
Darwin,
[EnumMember(Value = "linux")]
Linux,
[EnumMember(Value = "win32")]
Win32,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,42 @@
"comment": "Remove when https://github.com/hardkoded/puppeteer-sharp/issues/2450 is implemented",
"testIdPattern": "[elementhandle.spec] ElementHandle specs ElementHandle.boundingBox *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["FIREFOX"],
"parameters": ["firefox"],
"expectations": ["FAIL"]
},
{
"comment": "Remove when https://github.com/hardkoded/puppeteer-sharp/issues/2450 is implemented",
"testIdPattern": "[elementhandle.spec] ElementHandle specs ElementHandle.boxModel *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["FIREFOX"],
"parameters": ["firefox"],
"expectations": ["FAIL"]
},
{
"comment": "TODO It's failing on CI/CD",
"testIdPattern": "[fixtures.spec] Fixtures should dump browser process stderr",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["FIREFOX"],
"parameters": ["firefox"],
"expectations": ["FAIL"]
},
{
"comment": "TODO Review why is failing on Ci/CD",
"testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.connect should be able to reconnect",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["FIREFOX"],
"parameters": ["firefox"],
"expectations": ["FAIL"]
},
{
"comment": "TODO Review why is failing on Ci/CD",
"testIdPattern": "[headful.spec] headful tests HEADFUL headless should be able to read cookies written by headful",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["FIREFOX"],
"parameters": ["firefox"],
"expectations": ["FAIL"]
},
{
"comment": "TODO Review why is failing on Ci/CD",
"testIdPattern": "[headful.spec] headful tests HEADFUL headless should be able to read cookies written by headful",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["CHROME"],
"parameters": ["chrome"],
"expectations": ["FAIL"]
},
{
Expand Down
3 changes: 1 addition & 2 deletions lib/PuppeteerSharp.TestServer/SimpleCompressionMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace PuppeteerSharp.TestServer
{
internal class SimpleCompressionMiddleware
internal sealed class SimpleCompressionMiddleware
{
private readonly RequestDelegate _next;
private readonly SimpleServer _server;
Expand Down
Loading

0 comments on commit e8eac9a

Please sign in to comment.