Skip to content

Commit 9d24cdb

Browse files
authored
Warn on nested js handle (#915)
* Warn on nested js handle * Test ElementHandle
1 parent e1571f6 commit 9d24cdb

26 files changed

+71
-31
lines changed

lib/PuppeteerSharp.Tests/PageTests/EvaluateTests.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Newtonsoft.Json.Linq;
55
using Xunit;
66
using Xunit.Abstractions;
7-
using PuppeteerSharp.Helpers;
7+
using PuppeteerSharp.Helpers.Json;
88

99
namespace PuppeteerSharp.Tests.PageTests
1010
{
@@ -177,6 +177,28 @@ public async Task ShouldAcceptObjectHandleToPrimitiveTypes()
177177
Assert.True(isFive);
178178
}
179179

180+
[Fact]
181+
public async Task ShouldWarnOnNestedObjectHandles()
182+
{
183+
var handle = await Page.EvaluateFunctionHandleAsync("() => document.body");
184+
var elementHandle = handle as ElementHandle;
185+
186+
var exception = await Assert.ThrowsAsync<EvaluationFailedException>(()
187+
=> Page.EvaluateFunctionHandleAsync(
188+
"opts => opts.elem.querySelector('p')",
189+
new { elem = handle }));
190+
191+
Assert.Contains("Are you passing a nested JSHandle?", exception.Message);
192+
193+
//Check with ElementHandle
194+
exception = await Assert.ThrowsAsync<EvaluationFailedException>(()
195+
=> Page.EvaluateFunctionHandleAsync(
196+
"opts => opts.elem.querySelector('p')",
197+
new { elem = elementHandle }));
198+
199+
Assert.Contains("Are you passing a nested JSHandle?", exception.Message);
200+
}
201+
180202
[Fact]
181203
public async Task ShouldWorkFromInsideAnExposedFunction()
182204
{

lib/PuppeteerSharp/Browser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Microsoft.Extensions.Logging;
88
using PuppeteerSharp.Helpers;
9+
using PuppeteerSharp.Helpers.Json;
910
using PuppeteerSharp.Messaging;
1011

1112
namespace PuppeteerSharp

lib/PuppeteerSharp/CDPSession.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
using System;
22
using System.Collections.Concurrent;
3-
using System.Collections.Generic;
43
using System.Linq;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using Microsoft.Extensions.Logging;
87
using Newtonsoft.Json;
98
using Newtonsoft.Json.Linq;
10-
using Newtonsoft.Json.Serialization;
11-
using PuppeteerSharp.Helpers;
9+
using PuppeteerSharp.Helpers.Json;
1210
using PuppeteerSharp.Messaging;
1311

1412
namespace PuppeteerSharp

lib/PuppeteerSharp/Connection.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using System;
22
using System.Collections.Concurrent;
3-
using System.Collections.Generic;
43
using System.Linq;
54
using System.Net.WebSockets;
65
using System.Threading;
76
using System.Threading.Tasks;
87
using Microsoft.Extensions.Logging;
98
using Newtonsoft.Json;
109
using Newtonsoft.Json.Linq;
11-
using PuppeteerSharp.Helpers;
10+
using PuppeteerSharp.Helpers.Json;
1211
using PuppeteerSharp.Messaging;
1312
using PuppeteerSharp.Transport;
1413

lib/PuppeteerSharp/ElementHandle.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using Microsoft.Extensions.Logging;
2-
using Newtonsoft.Json;
32
using Newtonsoft.Json.Linq;
43
using PuppeteerSharp.Helpers;
4+
using PuppeteerSharp.Helpers.Json;
55
using PuppeteerSharp.Input;
66
using PuppeteerSharp.Messaging;
77
using System;
8-
using System.Collections.Generic;
98
using System.IO;
109
using System.Linq;
1110
using System.Threading.Tasks;

lib/PuppeteerSharp/FrameManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Microsoft.Extensions.Logging;
88
using PuppeteerSharp.Helpers;
9+
using PuppeteerSharp.Helpers.Json;
910
using PuppeteerSharp.Messaging;
1011

1112
namespace PuppeteerSharp

lib/PuppeteerSharp/Helpers/FlexibleStringEnumConverter.cs renamed to lib/PuppeteerSharp/Helpers/Json/FlexibleStringEnumConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Newtonsoft.Json;
33
using Newtonsoft.Json.Converters;
44

5-
namespace PuppeteerSharp.Helpers
5+
namespace PuppeteerSharp.Helpers.Json
66
{
77
internal class FlexibleStringEnumConverter : StringEnumConverter
88
{

lib/PuppeteerSharp/Helpers/HttpMethodConverter.cs renamed to lib/PuppeteerSharp/Helpers/Json/HttpMethodConverter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
using System.Net.Http;
33
using Newtonsoft.Json;
44

5-
namespace PuppeteerSharp.Helpers
5+
namespace PuppeteerSharp.Helpers.Json
66
{
77
internal class HttpMethodConverter : JsonConverter
88
{
99
public override bool CanConvert(Type objectType) => objectType == typeof(string);
1010

1111
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
12-
{
13-
return new HttpMethod((string)reader.Value);
14-
}
12+
=> new HttpMethod((string)reader.Value);
1513

1614
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
1715
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace PuppeteerSharp.Helpers.Json
5+
{
6+
/// <summary>
7+
/// JSHandleMethodConverter will throw an exception if a JSHandle object is trying to be serialized
8+
/// </summary>
9+
internal class JSHandleMethodConverter : JsonConverter
10+
{
11+
public override bool CanConvert(Type objectType) => false;
12+
13+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
14+
=> null;
15+
16+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
17+
=> throw new EvaluationFailedException("Unable to make function call. Are you passing a nested JSHandle?");
18+
}
19+
}

lib/PuppeteerSharp/Helpers/JTokenExtensions.cs renamed to lib/PuppeteerSharp/Helpers/Json/JTokenExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Newtonsoft.Json.Linq;
33
using Newtonsoft.Json.Serialization;
44

5-
namespace PuppeteerSharp.Helpers
5+
namespace PuppeteerSharp.Helpers.Json
66
{
77
/// <summary>
88
/// A set of extension methods for JToken

0 commit comments

Comments
 (0)