forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrowsingContextTest.cs
319 lines (249 loc) · 9.07 KB
/
BrowsingContextTest.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
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
316
317
318
319
// <copyright file="BrowsingContextTest.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>
using NUnit.Framework;
using OpenQA.Selenium.BiDi.Modules.BrowsingContext;
using System.Linq;
using System.Threading.Tasks;
namespace OpenQA.Selenium.BiDi.BrowsingContext;
class BrowsingContextTest : BiDiTestFixture
{
[Test]
public async Task CanCreateNewTab()
{
var tab = await bidi.BrowsingContext.CreateAsync(ContextType.Tab);
Assert.That(tab, Is.Not.Null);
}
[Test]
public async Task CanCreateNewTabWithReferencedContext()
{
var tab = await bidi.BrowsingContext.CreateAsync(ContextType.Tab, new()
{
ReferenceContext = context
});
Assert.That(tab, Is.Not.Null);
}
[Test]
public async Task CanCreateNewWindow()
{
var window = await bidi.BrowsingContext.CreateAsync(ContextType.Window);
Assert.That(window, Is.Not.Null);
}
[Test]
public async Task CanCreateNewWindowWithReferencedContext()
{
var window = await bidi.BrowsingContext.CreateAsync(ContextType.Window, new()
{
ReferenceContext = context
});
Assert.That(window, Is.Not.Null);
}
[Test]
public async Task CanCreateContextWithAllParameters()
{
var userContext = await bidi.Browser.CreateUserContextAsync();
var window = await bidi.BrowsingContext.CreateAsync(ContextType.Window, new()
{
ReferenceContext = context,
UserContext = userContext.UserContext,
Background = true
});
Assert.That(window, Is.Not.Null);
}
[Test]
public async Task CanNavigateToUrl()
{
var info = await context.NavigateAsync(UrlBuilder.WhereIs("/bidi/logEntryAdded.html"));
Assert.That(info.Url, Does.Contain("/bidi/logEntryAdded.html"));
}
[Test]
public async Task CanNavigateToUrlWithReadinessState()
{
var info = await context.NavigateAsync(UrlBuilder.WhereIs("/bidi/logEntryAdded.html"), new()
{
Wait = ReadinessState.Complete
});
Assert.That(info, Is.Not.Null);
Assert.That(info.Url, Does.Contain("/bidi/logEntryAdded.html"));
}
[Test]
public async Task CanGetTreeWithChild()
{
await context.NavigateAsync(UrlBuilder.WhereIs("iframes.html"), new() { Wait = ReadinessState.Complete });
var tree = await context.GetTreeAsync();
Assert.That(tree, Has.Count.EqualTo(1));
Assert.That(tree[0].Context, Is.EqualTo(context));
Assert.That(tree[0].Children, Has.Count.EqualTo(1));
Assert.That(tree[0].Children[0].Url, Does.Contain("formPage.html"));
}
[Test]
public async Task CanGetTreeWithDepth()
{
await context.NavigateAsync(UrlBuilder.WhereIs("iframes.html"), new() { Wait = ReadinessState.Complete });
var tree = await context.GetTreeAsync(new() { MaxDepth = 0 });
Assert.That(tree, Has.Count.EqualTo(1));
Assert.That(tree[0].Context, Is.EqualTo(context));
Assert.That(tree[0].Children, Is.Null);
}
[Test]
public async Task CanGetTreeTopLevel()
{
var window1 = await bidi.BrowsingContext.CreateAsync(ContextType.Window);
var window2 = await bidi.BrowsingContext.CreateAsync(ContextType.Window);
var tree = await bidi.BrowsingContext.GetTreeAsync();
Assert.That(tree, Has.Count.GreaterThanOrEqualTo(2));
}
[Test]
public async Task CanCloseWindow()
{
var window = await bidi.BrowsingContext.CreateAsync(ContextType.Window);
await window.CloseAsync();
var tree = await bidi.BrowsingContext.GetTreeAsync();
Assert.That(tree.Select(i => i.Context), Does.Not.Contain(window));
}
[Test]
public async Task CanCloseTab()
{
var tab = await bidi.BrowsingContext.CreateAsync(ContextType.Tab);
await tab.CloseAsync();
var tree = await bidi.BrowsingContext.GetTreeAsync();
Assert.That(tree.Select(i => i.Context), Does.Not.Contain(tab));
}
[Test]
public async Task CanActivate()
{
await context.ActivateAsync();
// TODO: Add assertion when https://w3c.github.io/webdriver-bidi/#type-browser-ClientWindowInfo is implemented
}
[Test]
public async Task CanReload()
{
string url = UrlBuilder.WhereIs("/bidi/logEntryAdded.html");
await context.NavigateAsync(url, new() { Wait = ReadinessState.Complete });
var info = await context.ReloadAsync();
Assert.That(info, Is.Not.Null);
Assert.That(info.Url, Does.Contain("/bidi/logEntryAdded.html"));
}
[Test]
public async Task CanReloadWithReadinessState()
{
string url = UrlBuilder.WhereIs("/bidi/logEntryAdded.html");
await context.NavigateAsync(url, new() { Wait = ReadinessState.Complete });
var info = await context.ReloadAsync(new()
{
Wait = ReadinessState.Complete
});
Assert.That(info, Is.Not.Null);
Assert.That(info.Url, Does.Contain("/bidi/logEntryAdded.html"));
}
[Test]
public async Task CanHandleUserPrompt()
{
await context.NavigateAsync(UrlBuilder.WhereIs("alerts.html"), new() { Wait = ReadinessState.Complete });
driver.FindElement(By.Id("alert")).Click();
await context.HandleUserPromptAsync();
}
[Test]
public async Task CanAcceptUserPrompt()
{
await context.NavigateAsync(UrlBuilder.WhereIs("alerts.html"), new() { Wait = ReadinessState.Complete });
driver.FindElement(By.Id("alert")).Click();
await context.HandleUserPromptAsync(new()
{
Accept = true
});
}
[Test]
public async Task CanDismissUserPrompt()
{
await context.NavigateAsync(UrlBuilder.WhereIs("alerts.html"), new() { Wait = ReadinessState.Complete });
driver.FindElement(By.Id("alert")).Click();
await context.HandleUserPromptAsync(new()
{
Accept = false
});
}
[Test]
public async Task CanPassUserTextToPrompt()
{
await context.NavigateAsync(UrlBuilder.WhereIs("alerts.html"), new() { Wait = ReadinessState.Complete });
driver.FindElement(By.Id("alert")).Click();
await context.HandleUserPromptAsync(new()
{
UserText = "Selenium automates browsers"
});
}
[Test]
public async Task CanCaptureScreenshot()
{
var screenshot = await context.CaptureScreenshotAsync();
Assert.That(screenshot, Is.Not.Null);
Assert.That(screenshot.Data, Is.Not.Empty);
}
[Test]
public async Task CanCaptureScreenshotWithParameters()
{
var screenshot = await context.CaptureScreenshotAsync(new()
{
Origin = Origin.Document,
Clip = new ClipRectangle.Box(5, 5, 10, 10)
});
Assert.That(screenshot, Is.Not.Null);
Assert.That(screenshot.Data, Is.Not.Empty);
}
[Test]
public async Task CanCaptureScreenshotOfViewport()
{
var screenshot = await context.CaptureScreenshotAsync(new()
{
Origin = Origin.Viewport,
Clip = new ClipRectangle.Box(5, 5, 10, 10)
});
Assert.That(screenshot, Is.Not.Null);
Assert.That(screenshot.Data, Is.Not.Empty);
}
[Test]
public async Task CanCaptureScreenshotOfElement()
{
await context.NavigateAsync(UrlBuilder.WhereIs("formPage.html"), new() { Wait = ReadinessState.Complete });
var nodes = await context.LocateNodesAsync(new CssLocator("#checky"));
var screenshot = await context.CaptureScreenshotAsync(new()
{
Clip = new ClipRectangle.Element(nodes[0])
});
Assert.That(screenshot, Is.Not.Null);
Assert.That(screenshot.Data, Is.Not.Empty);
}
[Test]
public async Task CanSetViewport()
{
await context.SetViewportAsync(new() { Viewport = new(250, 300) });
}
[Test]
public async Task CanSetViewportWithDevicePixelRatio()
{
await context.SetViewportAsync(new() { Viewport = new(250, 300), DevicePixelRatio = 5 });
}
[Test]
public async Task CanPrintPage()
{
var pdf = await context.PrintAsync();
Assert.That(pdf, Is.Not.Null);
Assert.That(pdf.Data, Is.Not.Empty);
}
}