Skip to content

Commit e79e75e

Browse files
authored
Bidi: Unlock dialog tests (#2970)
* Bidi: Unlock Dialog tests * cr * cr * prettier
1 parent 9aa7051 commit e79e75e

12 files changed

Lines changed: 298 additions & 38 deletions

File tree

lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@
7676
"parameters": ["firefox", "headful"],
7777
"expectations": ["FAIL"]
7878
},
79-
{
80-
"comment": "This is flaky on CI/CD",
81-
"testIdPattern": "[oopif.spec] OOPIF should provide access to elements",
82-
"platforms": ["win32"],
83-
"parameters": ["chrome"],
84-
"expectations": ["FAIL"]
85-
},
8679
{
8780
"comment": "This is flaky on CI/CD",
8881
"testIdPattern": "[worker.spec] Workers Page.workers",
@@ -397,21 +390,6 @@
397390
"FAIL"
398391
]
399392
},
400-
{
401-
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
402-
"testIdPattern": "[dialog.spec] *",
403-
"platforms": [
404-
"darwin",
405-
"linux",
406-
"win32"
407-
],
408-
"parameters": [
409-
"webDriverBiDi"
410-
],
411-
"expectations": [
412-
"FAIL"
413-
]
414-
},
415393
{
416394
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
417395
"testIdPattern": "[diffstyle] *",

lib/PuppeteerSharp.Tests/DialogTests/DialogTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ namespace PuppeteerSharp.Tests.DialogTests
66
{
77
public class DialogTests : PuppeteerPageBaseTest
88
{
9-
public DialogTests() : base()
10-
{
11-
}
12-
139
[Test, PuppeteerTest("dialog.spec", "Page.Events.Dialog", "should fire")]
1410
public async Task ShouldFire()
1511
{

lib/PuppeteerSharp/Bidi/BidiBrowser.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class BidiBrowser : Browser
4141
private readonly ConcurrentSet<BidiBrowserContext> _browserContexts = [];
4242
private readonly ILogger<BidiBrowser> _logger;
4343
private readonly BidiBrowserTarget _target;
44+
private bool _isClosed;
4445

4546
private BidiBrowser(Core.Browser browserCore, LaunchOptions options, ILoggerFactory loggerFactory)
4647
{
@@ -52,7 +53,7 @@ private BidiBrowser(Core.Browser browserCore, LaunchOptions options, ILoggerFact
5253
}
5354

5455
/// <inheritdoc />
55-
public override bool IsClosed { get; }
56+
public override bool IsClosed => _isClosed;
5657

5758
/// <inheritdoc />
5859
public override ITarget Target => _target;
@@ -103,6 +104,7 @@ private BidiBrowser(Core.Browser browserCore, LaunchOptions options, ILoggerFact
103104
/// <inheritdoc />
104105
public override async Task CloseAsync()
105106
{
107+
_isClosed = true;
106108
try
107109
{
108110
await BrowserCore.CloseAsync().ConfigureAwait(false);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// * MIT License
2+
// *
3+
// * Copyright (c) Darío Kondratiuk
4+
// *
5+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// * of this software and associated documentation files (the "Software"), to deal
7+
// * in the Software without restriction, including without limitation the rights
8+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// * copies of the Software, and to permit persons to whom the Software is
10+
// * furnished to do so, subject to the following conditions:
11+
// *
12+
// * The above copyright notice and this permission notice shall be included in all
13+
// * copies or substantial portions of the Software.
14+
// *
15+
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// * SOFTWARE.
22+
23+
using System.Threading.Tasks;
24+
using PuppeteerSharp.Bidi.Core;
25+
26+
namespace PuppeteerSharp.Bidi;
27+
28+
/// <summary>
29+
/// BiDi implementation of <see cref="Dialog"/>.
30+
/// </summary>
31+
public class BidiDialog : Dialog
32+
{
33+
private readonly UserPrompt _prompt;
34+
35+
private BidiDialog(UserPrompt prompt)
36+
: base(ConvertDialogType(prompt.Info.PromptType), prompt.Info.Message, prompt.Info.DefaultValue ?? string.Empty)
37+
{
38+
_prompt = prompt;
39+
}
40+
41+
internal static BidiDialog From(UserPrompt prompt)
42+
{
43+
return new BidiDialog(prompt);
44+
}
45+
46+
internal override Task HandleAsync(bool accept, string text)
47+
{
48+
return _prompt.HandleAsync(accept, text);
49+
}
50+
51+
private static DialogType ConvertDialogType(WebDriverBiDi.BrowsingContext.UserPromptType type)
52+
{
53+
return type switch
54+
{
55+
WebDriverBiDi.BrowsingContext.UserPromptType.Alert => DialogType.Alert,
56+
WebDriverBiDi.BrowsingContext.UserPromptType.Confirm => DialogType.Confirm,
57+
WebDriverBiDi.BrowsingContext.UserPromptType.Prompt => DialogType.Prompt,
58+
WebDriverBiDi.BrowsingContext.UserPromptType.BeforeUnload => DialogType.BeforeUnload,
59+
_ => DialogType.Alert,
60+
};
61+
}
62+
}
63+

lib/PuppeteerSharp/Bidi/BidiFrame.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,12 @@ private void Initialize()
570570
((Page)Page).OnDOMContentLoaded();
571571
((Page)Page).OnFrameNavigated(new FrameNavigatedEventArgs(this, NavigationType.Navigation));
572572
};
573+
574+
BrowsingContext.UserPrompt += (sender, args) =>
575+
{
576+
var dialog = BidiDialog.From(args.UserPrompt);
577+
((Page)Page).OnDialog(new DialogEventArgs(dialog));
578+
};
573579
}
574580

575581
private void CreateFrameTarget(BrowsingContext browsingContext)

lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
using System.Collections.Concurrent;
2525
using System.Collections.Generic;
2626
using System.Threading.Tasks;
27-
using Microsoft.Extensions.Options;
2827
using WebDriverBiDi.BrowsingContext;
2928
using WebDriverBiDi.Input;
3029

@@ -64,6 +63,8 @@ private BrowsingContext(UserContext userContext, BrowsingContext parent, string
6463

6564
public event EventHandler HistoryUpdated;
6665

66+
public event EventHandler<UserPromptEventArgs> UserPrompt;
67+
6768
public UserContext UserContext { get; }
6869

6970
public string Id { get; }
@@ -174,7 +175,7 @@ internal WindowRealm CreateWindowRealm(string sandbox = null)
174175
{
175176
var realm = WindowRealm.From(this, sandbox);
176177

177-
realm.Worker += (sender, args) =>
178+
realm.Worker += (_, args) =>
178179
{
179180
OnWorker(args.Realm);
180181
};
@@ -192,7 +193,7 @@ internal async Task ReloadAsync()
192193

193194
private void Initialize()
194195
{
195-
UserContext.Closed += (sender, args) => Dispose("User context was closed");
196+
UserContext.Closed += (_, _) => Dispose("User context was closed");
196197

197198
Session.BrowsingContextContextCreated += (sender, args) =>
198199
{
@@ -205,15 +206,15 @@ private void Initialize()
205206

206207
_children.TryAdd(args.UserContextId, browsingContext);
207208

208-
browsingContext.Closed += (sender, args) =>
209+
browsingContext.Closed += (_, _) =>
209210
{
210211
_children.TryRemove(browsingContext.Id, out _);
211212
};
212213

213214
OnBrowsingContextCreated(new BidiBrowsingContextEventArgs(browsingContext));
214215
};
215216

216-
Session.BrowsingContextContextDestroyed += (sender, args) =>
217+
Session.BrowsingContextContextDestroyed += (_, args) =>
217218
{
218219
if (args.UserContextId != Id)
219220
{
@@ -223,7 +224,7 @@ private void Initialize()
223224
Dispose("Browsing context already closed.");
224225
};
225226

226-
Session.BrowsingContextDomContentLoaded += (sender, args) =>
227+
Session.BrowsingContextDomContentLoaded += (_, args) =>
227228
{
228229
if (args.BrowsingContextId != Id)
229230
{
@@ -234,7 +235,7 @@ private void Initialize()
234235
OnDomContentLoaded();
235236
};
236237

237-
Session.BrowsingContextLoad += (sender, args) =>
238+
Session.BrowsingContextLoad += (_, args) =>
238239
{
239240
if (args.BrowsingContextId != Id)
240241
{
@@ -274,7 +275,7 @@ private void Initialize()
274275
OnNavigation(new BrowserContextNavigationEventArgs(_navigation));
275276
};
276277

277-
Session.BrowsingContextHistoryUpdated += (sender, args) =>
278+
Session.BrowsingContextHistoryUpdated += (_, args) =>
278279
{
279280
if (args.BrowsingContextId != Id)
280281
{
@@ -284,7 +285,7 @@ private void Initialize()
284285
OnHistoryUpdated();
285286
};
286287

287-
Session.NetworkBeforeRequestSent += (sender, args) =>
288+
Session.NetworkBeforeRequestSent += (_, args) =>
288289
{
289290
if (args.BrowsingContextId != Id)
290291
{
@@ -300,6 +301,17 @@ private void Initialize()
300301
_requests.TryAdd(args.Request.RequestId, request);
301302
Request?.Invoke(this, new RequestEventArgs(request));
302303
};
304+
305+
Session.BrowsingContextUserPromptOpened += (_, args) =>
306+
{
307+
if (args.BrowsingContextId != Id)
308+
{
309+
return;
310+
}
311+
312+
var userPrompt = Core.UserPrompt.From(this, args);
313+
OnUserPromptOpened(new UserPromptEventArgs(userPrompt));
314+
};
303315
}
304316

305317
private void OnNavigation(BrowserContextNavigationEventArgs args) => Navigation?.Invoke(this, args);
@@ -324,4 +336,6 @@ private void Dispose(string reason)
324336
private void OnClosed(string reason) => Closed?.Invoke(this, new ClosedEventArgs(reason));
325337

326338
private void OnWorker(DedicatedWorkerRealm args) => Worker?.Invoke(this, new WorkerRealmEventArgs(args));
339+
340+
private void OnUserPromptOpened(UserPromptEventArgs args) => UserPrompt?.Invoke(this, args);
327341
}

lib/PuppeteerSharp/Bidi/Core/Session.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ internal class Session(BiDiDriver driver, NewCommandResult info) : IDisposable
6464

6565
public event EventHandler<ResponseCompletedEventArgs> NetworkResponseComplete;
6666

67+
public event EventHandler<WebDriverBiDi.BrowsingContext.UserPromptOpenedEventArgs> BrowsingContextUserPromptOpened;
68+
69+
public event EventHandler<WebDriverBiDi.BrowsingContext.UserPromptClosedEventArgs> BrowsingContextUserPromptClosed;
70+
6771
public BiDiDriver Driver { get; } = driver;
6872

6973
public NewCommandResult Info { get; } = info;
@@ -112,6 +116,8 @@ private async Task InitializeAsync()
112116
Driver.Network.OnAuthRequired.AddObserver(OnNetworkAuthRequired);
113117
Driver.Network.OnFetchError.AddObserver(OnNetworkFetchError);
114118
Driver.Network.OnResponseCompleted.AddObserver(OnNetworkResponseCompleted);
119+
Driver.BrowsingContext.OnUserPromptOpened.AddObserver(OnBrowsingContextUserPromptOpened);
120+
Driver.BrowsingContext.OnUserPromptClosed.AddObserver(OnBrowsingContextUserPromptClosed);
115121
}
116122

117123
private void OnFragmentNavigated(WebDriverBiDi.BrowsingContext.NavigationEventArgs info)
@@ -149,4 +155,8 @@ private void OnFragmentNavigated(WebDriverBiDi.BrowsingContext.NavigationEventAr
149155
private void OnBrowsingContextDomContentLoaded(WebDriverBiDi.BrowsingContext.NavigationEventArgs obj) => BrowsingContextDomContentLoaded?.Invoke(this, obj);
150156

151157
private void OnBrowsingContextContextDestroyed(BrowsingContextEventArgs obj) => BrowsingContextContextDestroyed?.Invoke(this, obj);
158+
159+
private void OnBrowsingContextUserPromptOpened(WebDriverBiDi.BrowsingContext.UserPromptOpenedEventArgs obj) => BrowsingContextUserPromptOpened?.Invoke(this, obj);
160+
161+
private void OnBrowsingContextUserPromptClosed(WebDriverBiDi.BrowsingContext.UserPromptClosedEventArgs obj) => BrowsingContextUserPromptClosed?.Invoke(this, obj);
152162
}

0 commit comments

Comments
 (0)