forked from microsoft/playwright-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageAddInitScriptTests.cs
More file actions
164 lines (140 loc) · 7.25 KB
/
PageAddInitScriptTests.cs
File metadata and controls
164 lines (140 loc) · 7.25 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* MIT License
*
* Copyright (c) Microsoft Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Microsoft.Playwright.Tests;
public class PageAddInitScriptTests : PageTestEx
{
[PlaywrightTest("page-add-init-script.spec.ts", "should evaluate before anything else on the page")]
public async Task ShouldEvaluateBeforeAnythingElseOnThePage()
{
await Page.AddInitScriptAsync("window.injected = 123;");
await Page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.AreEqual(123, await Page.EvaluateAsync<int>("() => window.result"));
}
[PlaywrightTest("page-add-init-script.spec.ts", "should work with a path")]
public async Task ShouldWorkWithAPath()
{
await Page.AddInitScriptAsync(scriptPath: TestUtils.GetAsset("injectedfile.js"));
await Page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.AreEqual(123, await Page.EvaluateAsync<int>("() => window.result"));
}
[PlaywrightTest("page-add-init-script.spec.ts", "should work with a path")]
public async Task ShouldWorkWithContents()
{
await Page.AddInitScriptAsync("window.injected = 123;");
await Page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.AreEqual(123, await Page.EvaluateAsync<int>("() => window.result"));
}
[PlaywrightTest("page-add-init-script.spec.ts", "should throw without path and content")]
public Task ShouldThrowWithoutPathAndContent()
{
return PlaywrightAssert.ThrowsAsync<ArgumentException>(() => Page.AddInitScriptAsync());
}
[PlaywrightTest("page-add-init-script.spec.ts", "should work with browser context scripts")]
public async Task ShouldWorkWithBrowserContextScripts()
{
await using var context = await Browser.NewContextAsync();
await context.AddInitScriptAsync("window.temp = 123;");
var page = await context.NewPageAsync();
await page.AddInitScriptAsync("window.injected = window.temp;");
await page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.AreEqual(123, await page.EvaluateAsync<int>("() => window.result"));
}
[PlaywrightTest("page-add-init-script.spec.ts", "should work with browser context scripts with path")]
public async Task ShouldWorkWithBrowserContextScriptsWithPath()
{
await using var context = await Browser.NewContextAsync();
await context.AddInitScriptAsync(scriptPath: TestUtils.GetAsset("injectedfile.js"));
var page = await context.NewPageAsync();
await page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.AreEqual(123, await page.EvaluateAsync<int>("() => window.result"));
}
[PlaywrightTest("page-add-init-script.spec.ts", "should work with browser context scripts for already created pages")]
public async Task ShouldWorkWithBrowserContextScriptsForAlreadyCreatedPages()
{
await using var context = await Browser.NewContextAsync();
var page = await context.NewPageAsync();
await context.AddInitScriptAsync("window.temp = 123;");
await page.AddInitScriptAsync(script: "window.injected = window.temp;");
await page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.AreEqual(123, await page.EvaluateAsync<int>("() => window.result"));
}
[PlaywrightTest("page-add-init-script.spec.ts", "should support multiple scripts")]
public async Task ShouldSupportMultipleScripts()
{
await Page.AddInitScriptAsync("window.script1 = 1;");
await Page.AddInitScriptAsync("window.script2 = 2;");
await Page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.AreEqual(1, await Page.EvaluateAsync<int>("() => window.script1"));
Assert.AreEqual(2, await Page.EvaluateAsync<int>("() => window.script2"));
}
[PlaywrightTest("page-add-init-script.spec.ts", "should work with CSP")]
public async Task ShouldWorkWithCSP()
{
Server.SetCSP("/empty.html", "script-src " + Server.Prefix);
await Page.AddInitScriptAsync("window.injected = 123;");
await Page.GotoAsync(Server.EmptyPage);
Assert.AreEqual(123, await Page.EvaluateAsync<int>("() => window.injected"));
// Make sure CSP works.
try
{
await Page.AddScriptTagAsync(new() { Content = "window.e = 10;" });
}
catch
{
//Silent exception
}
Assert.Null(await Page.EvaluateAsync("() => window.e"));
}
[PlaywrightTest("page-add-init-script.spec.ts", "should work after a cross origin navigation")]
public async Task ShouldWorkAfterACrossOriginNavigation()
{
await Page.GotoAsync(Server.CrossProcessPrefix);
await Page.AddInitScriptAsync("window.injected = 123;");
await Page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.AreEqual(123, await Page.EvaluateAsync<int>("() => window.result"));
}
[PlaywrightTest("page-add-init-script.spec.ts", "should remove init script after dispose")]
public async Task ShouldRemoveInitScriptAfterDispose()
{
var disposable = await Page.AddInitScriptAsync("window.injected = 123;");
await Page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.AreEqual(123, await Page.EvaluateAsync<int>("() => window.result"));
await disposable.DisposeAsync();
await Page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.IsNull(await Page.EvaluateAsync("() => window.injected"));
}
[PlaywrightTest("page-add-init-script.spec.ts", "should remove one of multiple init scripts after dispose")]
public async Task ShouldRemoveOneOfMultipleInitScriptsAfterDispose()
{
var disposable1 = await Page.AddInitScriptAsync("window.script1 = 1;");
await Page.AddInitScriptAsync("window.script2 = 2;");
await Page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.AreEqual(1, await Page.EvaluateAsync<int>("() => window.script1"));
Assert.AreEqual(2, await Page.EvaluateAsync<int>("() => window.script2"));
await disposable1.DisposeAsync();
await Page.GotoAsync(Server.Prefix + "/tamperable.html");
Assert.IsNull(await Page.EvaluateAsync("() => window.script1"));
Assert.AreEqual(2, await Page.EvaluateAsync<int>("() => window.script2"));
}
}