forked from hardkoded/puppeteer-sharp
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ExposeFunctionTests.cs
143 lines (129 loc) · 5.87 KB
/
ExposeFunctionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;
namespace PuppeteerSharp.Tests.DevToolsContextTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class ExposeFunctionTests : DevToolsContextBaseTest
{
public ExposeFunctionTests(ITestOutputHelper output) : base(output)
{
}
[PuppeteerTest("page.spec.ts", "Page.exposeFunction", "should work")]
[PuppeteerFact]
public async Task ShouldWork()
{
await DevToolsContext.ExposeFunctionAsync("compute", (int a, int b) => a * b);
var result = await DevToolsContext.EvaluateExpressionAsync<int>("compute(9, 4)");
Assert.Equal(36, result);
}
[PuppeteerTest("page.spec.ts", "Page.exposeFunction", "should throw exception in page context")]
[PuppeteerFact]
public async Task ShouldThrowExceptionInPageContext()
{
await DevToolsContext.ExposeFunctionAsync("woof", () => throw new Exception("WOOF WOOF"));
var result = await DevToolsContext.EvaluateFunctionAsync<JToken>(@" async () =>{
try
{
await woof();
}
catch (e)
{
return { message: e.message, stack: e.stack};
}
}");
Assert.Equal("WOOF WOOF", result.SelectToken("message").ToObject<string>());
Assert.Contains("ExposeFunctionTests", result.SelectToken("stack").ToObject<string>());
}
[PuppeteerTest("page.spec.ts", "Page.exposeFunction", "should survive navigation")]
[PuppeteerFact]
public async Task ShouldBeCallableFromInsideEvaluateOnNewDocument()
{
var called = false;
await DevToolsContext.ExposeFunctionAsync("woof", () => called = true);
await DevToolsContext.EvaluateFunctionOnNewDocumentAsync("() => woof()");
await DevToolsContext.ReloadAsync();
Assert.True(called);
}
[PuppeteerTest("page.spec.ts", "Page.exposeFunction", "should work")]
[PuppeteerFact]
public async Task ShouldSurviveNavigation()
{
await DevToolsContext.ExposeFunctionAsync("compute", (int a, int b) => a * b);
await DevToolsContext.GoToAsync(TestConstants.EmptyPage);
var result = await DevToolsContext.EvaluateExpressionAsync<int>("compute(9, 4)");
Assert.Equal(36, result);
}
[PuppeteerTest("page.spec.ts", "Page.exposeFunction", "should await returned promise")]
[PuppeteerFact]
public async Task ShouldAwaitReturnedValueTask()
{
await DevToolsContext.ExposeFunctionAsync("compute", (int a, int b) => Task.FromResult(a * b));
var result = await DevToolsContext.EvaluateExpressionAsync<int>("compute(3, 5)");
Assert.Equal(15, result);
}
[PuppeteerTest("page.spec.ts", "Page.exposeFunction", "should work on frames")]
[PuppeteerFact]
public async Task ShouldWorkOnFrames()
{
await DevToolsContext.ExposeFunctionAsync("compute", (int a, int b) => Task.FromResult(a * b));
await DevToolsContext.GoToAsync(TestConstants.ServerUrl + "/frames/nested-frames.html");
var frame = DevToolsContext.FirstChildFrame();
var result = await frame.EvaluateExpressionAsync<int>("compute(3, 5)");
Assert.Equal(15, result);
}
[PuppeteerTest("page.spec.ts", "Page.exposeFunction", "should work on frames before navigation")]
[PuppeteerFact]
public async Task ShouldWorkOnFramesBeforeNavigation()
{
await DevToolsContext.GoToAsync(TestConstants.ServerUrl + "/frames/nested-frames.html");
await DevToolsContext.ExposeFunctionAsync("compute", (int a, int b) => Task.FromResult(a * b));
var frame = DevToolsContext.FirstChildFrame();
var result = await frame.EvaluateExpressionAsync<int>("compute(3, 5)");
Assert.Equal(15, result);
}
[PuppeteerTest("page.spec.ts", "Page.exposeFunction", "should work with complex objects")]
[PuppeteerFact]
public async Task ShouldWorkWithComplexObjects()
{
await DevToolsContext.GoToAsync(TestConstants.ServerUrl + "/frames/nested-frames.html");
await DevToolsContext.ExposeFunctionAsync("complexObject", (dynamic a, dynamic b) => Task.FromResult(new { X = a.x + b.x }));
var result = await DevToolsContext.EvaluateExpressionAsync<JToken>("complexObject({x: 5}, {x: 2})");
Assert.Equal(7, result.SelectToken("x").ToObject<int>());
}
[PuppeteerFact]
public async Task ShouldAwaitReturnedTask()
{
var called = false;
await DevToolsContext.ExposeFunctionAsync("changeFlag", () =>
{
called = true;
return Task.CompletedTask;
});
await DevToolsContext.EvaluateExpressionAsync("changeFlag()");
Assert.True(called);
}
[PuppeteerFact]
public async Task ShouldWorkWithAction()
{
var called = false;
await DevToolsContext.ExposeFunctionAsync("changeFlag", () =>
{
called = true;
});
await DevToolsContext.EvaluateExpressionAsync("changeFlag()");
Assert.True(called);
}
[PuppeteerFact]
public async Task ShouldKeepTheCallbackClean()
{
await DevToolsContext.ExposeFunctionAsync("compute", (int a, int b) => a * b);
await DevToolsContext.EvaluateExpressionAsync<int>("compute(9, 4)");
Assert.False(DevToolsContext.Client.HasPendingCallbacks());
}
}
}