forked from microsoft/playwright-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserContextBasicTests.cs
More file actions
315 lines (272 loc) · 12.9 KB
/
BrowserContextBasicTests.cs
File metadata and controls
315 lines (272 loc) · 12.9 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* MIT License
*
* Copyright (c) 2020 Darío Kondratiuk
* Modifications 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.
*/
using System.Net;
namespace Microsoft.Playwright.Tests;
public class BrowserContextBasicTests : BrowserTestEx
{
[PlaywrightTest("browsercontext-basic.spec.ts", "should create new context")]
public async Task ShouldCreateNewContext()
{
await using var browser = await BrowserType.LaunchAsync();
Assert.IsEmpty(browser.Contexts);
await using var context = await browser.NewContextAsync();
Assert.That(browser.Contexts, Has.Length.EqualTo(1));
CollectionAssert.Contains(browser.Contexts, context);
Assert.AreEqual(browser, context.Browser);
await context.CloseAsync();
Assert.IsEmpty(browser.Contexts);
Assert.AreEqual(browser, context.Browser);
}
[PlaywrightTest("browsercontext-basic.spec.ts", "window.open should use parent tab context")]
public async Task WindowOpenShouldUseParentTabContext()
{
await using var context = await Browser.NewContextAsync();
var page = await context.NewPageAsync();
await page.GotoAsync(Server.EmptyPage);
var popupTargetCompletion = new TaskCompletionSource<IPage>();
page.Popup += (_, e) => popupTargetCompletion.SetResult(e);
var (popupTarget, _) = await TaskUtils.WhenAll(
popupTargetCompletion.Task,
page.EvaluateAsync("url => window.open(url)", Server.EmptyPage)
);
Assert.AreEqual(context, popupTarget.Context);
await context.CloseAsync();
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should isolate localStorage and cookies")]
public async Task ShouldIsolateLocalStorageAndCookies()
{
// Create two incognito contexts.
await using var browser = await BrowserType.LaunchAsync();
var context1 = await browser.NewContextAsync();
var context2 = await browser.NewContextAsync();
Assert.IsEmpty(context1.Pages);
Assert.IsEmpty(context2.Pages);
// Create a page in first incognito context.
var page1 = await context1.NewPageAsync();
await page1.GotoAsync(Server.EmptyPage);
await page1.EvaluateAsync(@"() => {
localStorage.setItem('name', 'page1');
document.cookie = 'name=page1';
}");
Assert.That(context1.Pages, Has.Count.EqualTo(1));
Assert.IsEmpty(context2.Pages);
// Create a page in second incognito context.
var page2 = await context2.NewPageAsync();
await page2.GotoAsync(Server.EmptyPage);
await page2.EvaluateAsync(@"() => {
localStorage.setItem('name', 'page2');
document.cookie = 'name=page2';
}");
Assert.That(context1.Pages, Has.Count.EqualTo(1));
Assert.AreEqual(page1, context1.Pages.FirstOrDefault());
Assert.That(context2.Pages, Has.Count.EqualTo(1));
Assert.AreEqual(page2, context2.Pages.FirstOrDefault());
// Make sure pages don't share localstorage or cookies.
Assert.AreEqual("page1", await page1.EvaluateAsync<string>("() => localStorage.getItem('name')"));
Assert.AreEqual("name=page1", await page1.EvaluateAsync<string>("() => document.cookie"));
Assert.AreEqual("page2", await page2.EvaluateAsync<string>("() => localStorage.getItem('name')"));
Assert.AreEqual("name=page2", await page2.EvaluateAsync<string>("() => document.cookie"));
// Cleanup contexts.
await TaskUtils.WhenAll(context1.CloseAsync(), context2.CloseAsync());
Assert.IsEmpty(browser.Contexts);
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should propagate default viewport to the page")]
public async Task ShouldPropagateDefaultViewportToThePage()
{
await using var context = await Browser.NewContextAsync(new()
{
ViewportSize = new()
{
Width = 456,
Height = 789
}
});
var page = await context.NewPageAsync();
await TestUtils.VerifyViewportAsync(page, 456, 789);
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should make a copy of default viewport")]
public async Task ShouldMakeACopyOfDefaultViewport()
{
var viewport = new ViewportSize
{
Width = 456,
Height = 789
};
await using var context = await Browser.NewContextAsync(new() { ViewportSize = viewport });
viewport.Width = 567;
var page = await context.NewPageAsync();
await TestUtils.VerifyViewportAsync(page, 456, 789);
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should respect deviceScaleFactor")]
public async Task ShouldRespectDeviceScaleFactor()
{
await using var context = await Browser.NewContextAsync(new()
{
DeviceScaleFactor = 3.5F
});
var page = await context.NewPageAsync();
Assert.AreEqual(3.5F, await page.EvaluateAsync<float>("window.devicePixelRatio"));
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should not allow deviceScaleFactor with null viewport")]
public async Task ShouldNotAllowDeviceScaleFactorWithViewportDisabled()
{
var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Browser.NewContextAsync(new()
{
ViewportSize = ViewportSize.NoViewport,
DeviceScaleFactor = 3,
}));
Assert.AreEqual("\"deviceScaleFactor\" option is not supported with null \"viewport\"", exception.Message);
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should not allow isMobile with null viewport")]
public async Task ShouldNotAllowIsMobileWithViewportDisabled()
{
var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Browser.NewContextAsync(new()
{
ViewportSize = ViewportSize.NoViewport,
IsMobile = true,
}));
Assert.AreEqual("\"isMobile\" option is not supported with null \"viewport\"", exception.Message);
}
[PlaywrightTest("browsercontext-basic.spec.ts", "close() should work for empty context")]
public async Task CloseShouldWorkForEmptyContext()
{
var context = await Browser.NewContextAsync();
await context.CloseAsync();
}
[PlaywrightTest("browsercontext-basic.spec.ts", "close() should abort waitForEvent")]
public async Task CloseShouldAbortWaitForEvent()
{
var context = await Browser.NewContextAsync();
var waitTask = context.WaitForPageAsync();
await context.CloseAsync();
var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => waitTask);
Assert.AreEqual(TestConstants.TargetClosedErrorMessage, exception.Message);
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should not report frameless pages on error")]
public async Task ShouldNotReportFramelessPagesOnError()
{
var context = await Browser.NewContextAsync();
var page = await context.NewPageAsync();
Server.SetRoute("/empty.html", context =>
{
context.Response.ContentType = "text/html";
return context.Response.WriteAsync($"<a href=\"{Server.EmptyPage}\" target=\"_blank\">Click me</a>");
});
IPage popup = null;
context.Page += (_, e) => popup = e;
await page.GotoAsync(Server.EmptyPage);
await page.ClickAsync("'Click me'");
await context.CloseAsync();
if (popup != null)
{
Assert.True(popup.IsClosed);
Assert.NotNull(popup.MainFrame);
}
}
[PlaywrightTest("browsercontext-basic.spec.ts", "close() should be callable twice")]
public async Task CloseShouldBeCallableTwice()
{
var context = await Browser.NewContextAsync();
await TaskUtils.WhenAll(context.CloseAsync(), context.CloseAsync());
await context.CloseAsync();
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should return all of the pages")]
public async Task ShouldReturnAllOfThePages()
{
await using var context = await Browser.NewContextAsync();
var page = await context.NewPageAsync();
var second = await context.NewPageAsync();
Assert.AreEqual(2, context.Pages.Count);
CollectionAssert.Contains(context.Pages, page);
CollectionAssert.Contains(context.Pages, second);
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should close all belonging pages once closing context")]
public async Task ShouldCloseAllBelongingPagesOnceClosingContext()
{
await using var context = await Browser.NewContextAsync();
await context.NewPageAsync();
Assert.That(context.Pages, Has.Count.EqualTo(1));
await context.CloseAsync();
Assert.IsEmpty(context.Pages);
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should disable javascript")]
public async Task ShouldDisableJavascript()
{
await using (var context = await Browser.NewContextAsync(new() { JavaScriptEnabled = false }))
{
var page = await context.NewPageAsync();
await page.GotoAsync("data:text/html, <script>var something = 'forbidden'</script>");
var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => page.EvaluateAsync("something"));
StringAssert.Contains(
TestConstants.IsWebKit ? "Can't find variable: something" : "something is not defined",
exception.Message);
}
await using (var context = await Browser.NewContextAsync())
{
var page = await context.NewPageAsync();
await page.GotoAsync("data:text/html, <script>var something = 'forbidden'</script>");
Assert.AreEqual("forbidden", await page.EvaluateAsync<string>("something"));
}
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should be able to navigate after disabling javascript")]
public async Task ShouldBeAbleToNavigateAfterDisablingJavascript()
{
await using var context = await Browser.NewContextAsync(new() { JavaScriptEnabled = false });
var page = await context.NewPageAsync();
await page.GotoAsync(Server.EmptyPage);
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should work with offline option")]
public async Task ShouldWorkWithOfflineOption()
{
await using var context = await Browser.NewContextAsync(new() { Offline = true });
var page = await context.NewPageAsync();
if (BrowserName == "firefox")
{
var frameNavigatedEvent = new TaskCompletionSource<bool>();
page.FrameNavigated += (_, _) => frameNavigatedEvent.TrySetResult(true);
await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => page.GotoAsync(Server.EmptyPage));
await frameNavigatedEvent.Task;
}
else
{
await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => page.GotoAsync(Server.EmptyPage));
}
await context.SetOfflineAsync(false);
var response = await page.GotoAsync(Server.EmptyPage);
Assert.AreEqual((int)HttpStatusCode.OK, response.Status);
}
[PlaywrightTest("browsercontext-basic.spec.ts", "should emulate navigator.onLine")]
[Skip(SkipAttribute.Targets.Firefox)]
public async Task ShouldEmulateNavigatorOnLine()
{
await using var context = await Browser.NewContextAsync();
var page = await context.NewPageAsync();
Assert.True(await page.EvaluateAsync<bool>("() => window.navigator.onLine"));
await context.SetOfflineAsync(true);
Assert.False(await page.EvaluateAsync<bool>("() => window.navigator.onLine"));
await context.SetOfflineAsync(false);
Assert.True(await page.EvaluateAsync<bool>("() => window.navigator.onLine"));
}
}