Skip to content

Commit f779b49

Browse files
kblokMeir017
authored andcommitted
Throw error when page reloads during page.evaluate (#370)
1 parent 77fe222 commit f779b49

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

lib/PuppeteerSharp.Tests/PageTests/EvaluateTests.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ public async Task BasicEvaluationTest(string script, object expected)
8181
[Fact]
8282
public async Task ShouldAcceptNullAsOneOfMultipleParameters()
8383
{
84-
bool result = await Page.EvaluateFunctionAsync<bool>("(a, b) => Object.is(a, null) && Object.is(b, 'foo')", null, "foo");
84+
var result = await Page.EvaluateFunctionAsync<bool>(
85+
"(a, b) => Object.is(a, null) && Object.is(b, 'foo')",
86+
null,
87+
"foo");
8588
Assert.True(result);
8689
}
8790

@@ -113,7 +116,7 @@ public async Task ShouldFailForWindowObjectUsingEvaluateFunction()
113116
var window = await Page.EvaluateFunctionAsync("() => window");
114117
Assert.Null(window);
115118
}
116-
119+
117120
[Fact]
118121
public async Task ShouldAcceptElementHandleAsAnArgument()
119122
{
@@ -173,5 +176,34 @@ await Page.ExposeFunctionAsync("callController", async (int a, int b) =>
173176
}");
174177
Assert.Equal(27, result);
175178
}
179+
180+
[Fact]
181+
public async Task ShouldThrowWhenEvaluationTriggersReload()
182+
{
183+
var exception = await Assert.ThrowsAsync<MessageException>(() =>
184+
{
185+
return Page.EvaluateFunctionAsync<object>(@"() => {
186+
location.reload();
187+
return new Promise(resolve => {
188+
setTimeout(() => resolve(1), 0);
189+
});
190+
}");
191+
});
192+
193+
Assert.Contains("Protocol error", exception.Message);
194+
}
195+
196+
[Fact]
197+
public async Task ShouldFailForCircularObject()
198+
{
199+
var result = await Page.EvaluateFunctionAsync<object>(@"() => {
200+
const a = {};
201+
const b = {a};
202+
a.b = b;
203+
return a;
204+
}");
205+
206+
Assert.Null(result);
207+
}
176208
}
177209
}

lib/PuppeteerSharp/ExecutionContext.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public async Task<dynamic> QueryObjectsAsync(JSHandle prototypeHandle)
125125
throw new PuppeteerException("Prototype JSHandle must not be referencing primitive value");
126126
}
127127

128-
dynamic response = await _client.SendAsync("Runtime.queryObjects", new Dictionary<string, object>()
128+
dynamic response = await _client.SendAsync("Runtime.queryObjects", new Dictionary<string, object>
129129
{
130130
{"prototypeObjectId", objectId.ToString()}
131131
});
@@ -140,7 +140,7 @@ internal async Task<JSHandle> EvaluateExpressionHandleAsync(string script)
140140
return null;
141141
}
142142

143-
return await EvaluateHandleAsync("Runtime.evaluate", new Dictionary<string, object>()
143+
return await EvaluateHandleAsync("Runtime.evaluate", new Dictionary<string, object>
144144
{
145145
{"contextId", _contextId},
146146
{"expression", script},
@@ -156,7 +156,7 @@ internal async Task<JSHandle> EvaluateFunctionHandleAsync(string script, params
156156
return null;
157157
}
158158

159-
return await EvaluateHandleAsync("Runtime.callFunctionOn", new Dictionary<string, object>()
159+
return await EvaluateHandleAsync("Runtime.callFunctionOn", new Dictionary<string, object>
160160
{
161161
{"functionDeclaration", script },
162162
{"executionContextId", _contextId},
@@ -169,9 +169,20 @@ internal async Task<JSHandle> EvaluateFunctionHandleAsync(string script, params
169169
private async Task<T> EvaluateAsync<T>(Task<JSHandle> handleEvaluator)
170170
{
171171
var handle = await handleEvaluator;
172-
var result = await handle.JsonValueAsync<T>()
173-
.ContinueWith(jsonTask => jsonTask.Exception != null ? default(T) : jsonTask.Result);
172+
var result = default(T);
174173

174+
try
175+
{
176+
result = await handle.JsonValueAsync<T>()
177+
.ContinueWith(jsonTask => jsonTask.Exception != null ? default : jsonTask.Result);
178+
}
179+
catch (Exception ex)
180+
{
181+
if (!ex.Message.Contains("Object reference chain is too long"))
182+
{
183+
throw new EvaluationFailedException(ex.Message, ex);
184+
}
185+
}
175186
await handle.DisposeAsync();
176187
return result;
177188
}

0 commit comments

Comments
 (0)