Skip to content

Commit 8818347

Browse files
authored
[dotnet] [bidi] Simplify context aware command options (#16954)
1 parent 421cf91 commit 8818347

16 files changed

Lines changed: 139 additions & 136 deletions

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContext.cs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,9 @@ public Task<HandleUserPromptResult> HandleUserPromptAsync(HandleUserPromptOption
121121
return BiDi.BrowsingContext.HandleUserPromptAsync(this, options);
122122
}
123123

124-
public Task<GetTreeResult> GetTreeAsync(BrowsingContextGetTreeOptions? options = null)
124+
public Task<GetTreeResult> GetTreeAsync(ContextGetTreeOptions? options = null)
125125
{
126-
GetTreeOptions getTreeOptions = new(options)
127-
{
128-
Root = this
129-
};
130-
131-
return BiDi.BrowsingContext.GetTreeAsync(getTreeOptions);
126+
return BiDi.BrowsingContext.GetTreeAsync(ContextGetTreeOptions.WithContext(options, this));
132127
}
133128

134129
public Task<Subscription> OnNavigationStartedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
@@ -139,7 +134,7 @@ public Task<Subscription> OnNavigationStartedAsync(Func<NavigationInfo, Task> ha
139134
{
140135
await handler(e).ConfigureAwait(false);
141136
}
142-
}, options.WithContext(this));
137+
}, ContextSubscriptionOptions.WithContext(options, this));
143138
}
144139

145140
public Task<Subscription> OnNavigationStartedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
@@ -150,7 +145,7 @@ public Task<Subscription> OnNavigationStartedAsync(Action<NavigationInfo> handle
150145
{
151146
handler(e);
152147
}
153-
}, options.WithContext(this));
148+
}, ContextSubscriptionOptions.WithContext(options, this));
154149
}
155150

156151
public Task<Subscription> OnFragmentNavigatedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
@@ -161,7 +156,7 @@ public Task<Subscription> OnFragmentNavigatedAsync(Func<NavigationInfo, Task> ha
161156
{
162157
await handler(e).ConfigureAwait(false);
163158
}
164-
}, options.WithContext(this));
159+
}, ContextSubscriptionOptions.WithContext(options, this));
165160
}
166161

167162
public Task<Subscription> OnFragmentNavigatedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
@@ -172,7 +167,7 @@ public Task<Subscription> OnFragmentNavigatedAsync(Action<NavigationInfo> handle
172167
{
173168
handler(e);
174169
}
175-
}, options.WithContext(this));
170+
}, ContextSubscriptionOptions.WithContext(options, this));
176171
}
177172

178173
public Task<Subscription> OnHistoryUpdatedAsync(Func<HistoryUpdatedEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
@@ -183,7 +178,7 @@ public Task<Subscription> OnHistoryUpdatedAsync(Func<HistoryUpdatedEventArgs, Ta
183178
{
184179
await handler(e).ConfigureAwait(false);
185180
}
186-
}, options.WithContext(this));
181+
}, ContextSubscriptionOptions.WithContext(options, this));
187182
}
188183

189184
public Task<Subscription> OnHistoryUpdatedAsync(Action<HistoryUpdatedEventArgs> handler, ContextSubscriptionOptions? options = null)
@@ -194,7 +189,7 @@ public Task<Subscription> OnHistoryUpdatedAsync(Action<HistoryUpdatedEventArgs>
194189
{
195190
handler(e);
196191
}
197-
}, options.WithContext(this));
192+
}, ContextSubscriptionOptions.WithContext(options, this));
198193
}
199194

200195
public Task<Subscription> OnDomContentLoadedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
@@ -205,7 +200,7 @@ public Task<Subscription> OnDomContentLoadedAsync(Func<NavigationInfo, Task> han
205200
{
206201
await handler(e).ConfigureAwait(false);
207202
}
208-
}, options.WithContext(this));
203+
}, ContextSubscriptionOptions.WithContext(options, this));
209204
}
210205

211206
public Task<Subscription> OnDomContentLoadedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
@@ -216,7 +211,7 @@ public Task<Subscription> OnDomContentLoadedAsync(Action<NavigationInfo> handler
216211
{
217212
handler(e);
218213
}
219-
}, options.WithContext(this));
214+
}, ContextSubscriptionOptions.WithContext(options, this));
220215
}
221216

222217
public Task<Subscription> OnLoadAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
@@ -227,7 +222,7 @@ public Task<Subscription> OnLoadAsync(Action<NavigationInfo> handler, ContextSub
227222
{
228223
handler(e);
229224
}
230-
}, options.WithContext(this));
225+
}, ContextSubscriptionOptions.WithContext(options, this));
231226
}
232227

233228
public Task<Subscription> OnLoadAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
@@ -238,7 +233,7 @@ public Task<Subscription> OnLoadAsync(Func<NavigationInfo, Task> handler, Contex
238233
{
239234
await handler(e).ConfigureAwait(false);
240235
}
241-
}, options.WithContext(this));
236+
}, ContextSubscriptionOptions.WithContext(options, this));
242237
}
243238

244239
public Task<Subscription> OnDownloadWillBeginAsync(Action<DownloadWillBeginEventArgs> handler, ContextSubscriptionOptions? options = null)
@@ -249,7 +244,7 @@ public Task<Subscription> OnDownloadWillBeginAsync(Action<DownloadWillBeginEvent
249244
{
250245
handler(e);
251246
}
252-
}, options.WithContext(this));
247+
}, ContextSubscriptionOptions.WithContext(options, this));
253248
}
254249

255250
public Task<Subscription> OnDownloadWillBeginAsync(Func<DownloadWillBeginEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
@@ -260,7 +255,7 @@ public Task<Subscription> OnDownloadWillBeginAsync(Func<DownloadWillBeginEventAr
260255
{
261256
await handler(e).ConfigureAwait(false);
262257
}
263-
}, options.WithContext(this));
258+
}, ContextSubscriptionOptions.WithContext(options, this));
264259
}
265260

266261
public Task<Subscription> OnDownloadEndAsync(Action<DownloadEndEventArgs> handler, ContextSubscriptionOptions? options = null)
@@ -271,7 +266,7 @@ public Task<Subscription> OnDownloadEndAsync(Action<DownloadEndEventArgs> handle
271266
{
272267
handler(e);
273268
}
274-
}, options.WithContext(this));
269+
}, ContextSubscriptionOptions.WithContext(options, this));
275270
}
276271

277272
public Task<Subscription> OnDownloadEndAsync(Func<DownloadEndEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
@@ -282,7 +277,7 @@ public Task<Subscription> OnDownloadEndAsync(Func<DownloadEndEventArgs, Task> ha
282277
{
283278
await handler(e).ConfigureAwait(false);
284279
}
285-
}, options.WithContext(this));
280+
}, ContextSubscriptionOptions.WithContext(options, this));
286281
}
287282

288283
public Task<Subscription> OnNavigationAbortedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
@@ -293,7 +288,7 @@ public Task<Subscription> OnNavigationAbortedAsync(Action<NavigationInfo> handle
293288
{
294289
handler(e);
295290
}
296-
}, options.WithContext(this));
291+
}, ContextSubscriptionOptions.WithContext(options, this));
297292
}
298293

299294
public Task<Subscription> OnNavigationAbortedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
@@ -304,7 +299,7 @@ public Task<Subscription> OnNavigationAbortedAsync(Func<NavigationInfo, Task> ha
304299
{
305300
await handler(e).ConfigureAwait(false);
306301
}
307-
}, options.WithContext(this));
302+
}, ContextSubscriptionOptions.WithContext(options, this));
308303
}
309304

310305
public Task<Subscription> OnNavigationFailedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
@@ -315,7 +310,7 @@ public Task<Subscription> OnNavigationFailedAsync(Action<NavigationInfo> handler
315310
{
316311
handler(e);
317312
}
318-
}, options.WithContext(this));
313+
}, ContextSubscriptionOptions.WithContext(options, this));
319314
}
320315

321316
public Task<Subscription> OnNavigationFailedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
@@ -326,7 +321,7 @@ public Task<Subscription> OnNavigationFailedAsync(Func<NavigationInfo, Task> han
326321
{
327322
await handler(e).ConfigureAwait(false);
328323
}
329-
}, options.WithContext(this));
324+
}, ContextSubscriptionOptions.WithContext(options, this));
330325
}
331326

332327
public Task<Subscription> OnNavigationCommittedAsync(Action<NavigationInfo> handler, ContextSubscriptionOptions? options = null)
@@ -337,7 +332,7 @@ public Task<Subscription> OnNavigationCommittedAsync(Action<NavigationInfo> hand
337332
{
338333
handler(e);
339334
}
340-
}, options.WithContext(this));
335+
}, ContextSubscriptionOptions.WithContext(options, this));
341336
}
342337

343338
public Task<Subscription> OnNavigationCommittedAsync(Func<NavigationInfo, Task> handler, ContextSubscriptionOptions? options = null)
@@ -348,7 +343,7 @@ public Task<Subscription> OnNavigationCommittedAsync(Func<NavigationInfo, Task>
348343
{
349344
await handler(e).ConfigureAwait(false);
350345
}
351-
}, options.WithContext(this));
346+
}, ContextSubscriptionOptions.WithContext(options, this));
352347
}
353348

354349
public bool Equals(BrowsingContext? other)

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextInputModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public Task<Subscription> OnFileDialogOpenedAsync(Func<FileDialogInfo, Task> han
4949
{
5050
await handler(e).ConfigureAwait(false);
5151
}
52-
}, options.WithContext(context));
52+
}, ContextSubscriptionOptions.WithContext(options, context));
5353
}
5454

5555
public Task<Subscription> OnFileDialogOpenedAsync(Action<FileDialogInfo> handler, ContextSubscriptionOptions? options = null)
@@ -60,6 +60,6 @@ public Task<Subscription> OnFileDialogOpenedAsync(Action<FileDialogInfo> handler
6060
{
6161
handler(e);
6262
}
63-
}, options.WithContext(context));
63+
}, ContextSubscriptionOptions.WithContext(options, context));
6464
}
6565
}

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public Task<Subscription> OnEntryAddedAsync(Func<Log.LogEntry, Task> handler, Co
2929
{
3030
return logModule.OnEntryAddedAsync(async args =>
3131
{
32-
if (args.Source.Context?.Equals(context) is true)
32+
if (context.Equals(args.Source.Context))
3333
{
3434
await handler(args).ConfigureAwait(false);
3535
}
@@ -40,7 +40,7 @@ public Task<Subscription> OnEntryAddedAsync(Action<Log.LogEntry> handler, Contex
4040
{
4141
return logModule.OnEntryAddedAsync(args =>
4242
{
43-
if (args.Source.Context?.Equals(context) is true)
43+
if (context.Equals(args.Source.Context))
4444
{
4545
handler(args);
4646
}

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,14 @@ await interception.OnAuthRequiredAsync(
8080
return interception;
8181
}
8282

83-
public Task<AddDataCollectorResult> AddDataCollectorAsync(IEnumerable<DataType> dataTypes, int maxEncodedDataSize, BrowsingContextAddDataCollectorOptions? options = null)
83+
public Task<AddDataCollectorResult> AddDataCollectorAsync(IEnumerable<DataType> dataTypes, int maxEncodedDataSize, ContextAddDataCollectorOptions? options = null)
8484
{
85-
AddDataCollectorOptions addDataCollectorOptions = new(options)
86-
{
87-
Contexts = [context]
88-
};
89-
90-
return networkModule.AddDataCollectorAsync(dataTypes, maxEncodedDataSize, addDataCollectorOptions);
85+
return networkModule.AddDataCollectorAsync(dataTypes, maxEncodedDataSize, ContextAddDataCollectorOptions.WithContext(options, context));
9186
}
9287

93-
public Task<SetCacheBehaviorResult> SetCacheBehaviorAsync(CacheBehavior behavior, BrowsingContextSetCacheBehaviorOptions? options = null)
88+
public Task<SetCacheBehaviorResult> SetCacheBehaviorAsync(CacheBehavior behavior, ContextSetCacheBehaviorOptions? options = null)
9489
{
95-
SetCacheBehaviorOptions setCacheBehaviorOptions = new(options)
96-
{
97-
Contexts = [context]
98-
};
99-
100-
return networkModule.SetCacheBehaviorAsync(behavior, setCacheBehaviorOptions);
90+
return networkModule.SetCacheBehaviorAsync(behavior, ContextSetCacheBehaviorOptions.WithContext(options, context));
10191
}
10292

10393
public Task<Subscription> OnBeforeRequestSentAsync(Func<BeforeRequestSentEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
@@ -108,7 +98,7 @@ public Task<Subscription> OnBeforeRequestSentAsync(Func<BeforeRequestSentEventAr
10898
{
10999
await handler(e).ConfigureAwait(false);
110100
}
111-
}, options.WithContext(context));
101+
}, ContextSubscriptionOptions.WithContext(options, context));
112102
}
113103

114104
public Task<Subscription> OnBeforeRequestSentAsync(Action<BeforeRequestSentEventArgs> handler, ContextSubscriptionOptions? options = null)
@@ -119,7 +109,7 @@ public Task<Subscription> OnBeforeRequestSentAsync(Action<BeforeRequestSentEvent
119109
{
120110
handler(e);
121111
}
122-
}, options.WithContext(context));
112+
}, ContextSubscriptionOptions.WithContext(options, context));
123113
}
124114

125115
public Task<Subscription> OnResponseStartedAsync(Func<ResponseStartedEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
@@ -130,7 +120,7 @@ public Task<Subscription> OnResponseStartedAsync(Func<ResponseStartedEventArgs,
130120
{
131121
await handler(e).ConfigureAwait(false);
132122
}
133-
}, options.WithContext(context));
123+
}, ContextSubscriptionOptions.WithContext(options, context));
134124
}
135125

136126
public Task<Subscription> OnResponseStartedAsync(Action<ResponseStartedEventArgs> handler, ContextSubscriptionOptions? options = null)
@@ -141,7 +131,7 @@ public Task<Subscription> OnResponseStartedAsync(Action<ResponseStartedEventArgs
141131
{
142132
handler(e);
143133
}
144-
}, options.WithContext(context));
134+
}, ContextSubscriptionOptions.WithContext(options, context));
145135
}
146136

147137
public Task<Subscription> OnResponseCompletedAsync(Func<ResponseCompletedEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
@@ -152,7 +142,7 @@ public Task<Subscription> OnResponseCompletedAsync(Func<ResponseCompletedEventAr
152142
{
153143
await handler(e).ConfigureAwait(false);
154144
}
155-
}, options.WithContext(context));
145+
}, ContextSubscriptionOptions.WithContext(options, context));
156146
}
157147

158148
public Task<Subscription> OnResponseCompletedAsync(Action<ResponseCompletedEventArgs> handler, ContextSubscriptionOptions? options = null)
@@ -163,7 +153,7 @@ public Task<Subscription> OnResponseCompletedAsync(Action<ResponseCompletedEvent
163153
{
164154
handler(e);
165155
}
166-
}, options.WithContext(context));
156+
}, ContextSubscriptionOptions.WithContext(options, context));
167157
}
168158

169159
public Task<Subscription> OnFetchErrorAsync(Func<FetchErrorEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
@@ -174,7 +164,7 @@ public Task<Subscription> OnFetchErrorAsync(Func<FetchErrorEventArgs, Task> hand
174164
{
175165
await handler(e).ConfigureAwait(false);
176166
}
177-
}, options.WithContext(context));
167+
}, ContextSubscriptionOptions.WithContext(options, context));
178168
}
179169

180170
public Task<Subscription> OnFetchErrorAsync(Action<FetchErrorEventArgs> handler, ContextSubscriptionOptions? options = null)
@@ -185,7 +175,7 @@ public Task<Subscription> OnFetchErrorAsync(Action<FetchErrorEventArgs> handler,
185175
{
186176
handler(e);
187177
}
188-
}, options.WithContext(context));
178+
}, ContextSubscriptionOptions.WithContext(options, context));
189179
}
190180

191181
public Task<Subscription> OnAuthRequiredAsync(Func<AuthRequiredEventArgs, Task> handler, ContextSubscriptionOptions? options = null)
@@ -196,7 +186,7 @@ public Task<Subscription> OnAuthRequiredAsync(Func<AuthRequiredEventArgs, Task>
196186
{
197187
await handler(e).ConfigureAwait(false);
198188
}
199-
}, options.WithContext(context));
189+
}, ContextSubscriptionOptions.WithContext(options, context));
200190
}
201191

202192
public Task<Subscription> OnAuthRequiredAsync(Action<AuthRequiredEventArgs> handler, ContextSubscriptionOptions? options = null)
@@ -207,12 +197,12 @@ public Task<Subscription> OnAuthRequiredAsync(Action<AuthRequiredEventArgs> hand
207197
{
208198
handler(e);
209199
}
210-
}, options.WithContext(context));
200+
}, ContextSubscriptionOptions.WithContext(options, context));
211201
}
212202
}
213203

214-
public sealed record InterceptRequestOptions : BrowsingContextAddInterceptOptions;
204+
public sealed class InterceptRequestOptions : ContextAddInterceptOptions;
215205

216-
public sealed record InterceptResponseOptions : BrowsingContextAddInterceptOptions;
206+
public sealed class InterceptResponseOptions : ContextAddInterceptOptions;
217207

218-
public sealed record InterceptAuthOptions : BrowsingContextAddInterceptOptions;
208+
public sealed class InterceptAuthOptions : ContextAddInterceptOptions;

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextScriptModule.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,14 @@ namespace OpenQA.Selenium.BiDi.BrowsingContext;
2525

2626
public sealed class BrowsingContextScriptModule(BrowsingContext context, ScriptModule scriptModule)
2727
{
28-
public async Task<AddPreloadScriptResult> AddPreloadScriptAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string functionDeclaration, BrowsingContextAddPreloadScriptOptions? options = null)
28+
public Task<AddPreloadScriptResult> AddPreloadScriptAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string functionDeclaration, ContextAddPreloadScriptOptions? options = null)
2929
{
30-
AddPreloadScriptOptions addPreloadScriptOptions = new(options)
31-
{
32-
Contexts = [context]
33-
};
34-
35-
return await scriptModule.AddPreloadScriptAsync(functionDeclaration, addPreloadScriptOptions).ConfigureAwait(false);
30+
return scriptModule.AddPreloadScriptAsync(functionDeclaration, ContextAddPreloadScriptOptions.WithContext(options, context));
3631
}
3732

38-
public async Task<GetRealmsResult> GetRealmsAsync(GetRealmsOptions? options = null)
33+
public Task<GetRealmsResult> GetRealmsAsync(ContextGetRealmsOptions? options = null)
3934
{
40-
options ??= new();
41-
42-
options.Context = context;
43-
44-
return await scriptModule.GetRealmsAsync(options).ConfigureAwait(false);
35+
return scriptModule.GetRealmsAsync(ContextGetRealmsOptions.WithContext(options, context));
4536
}
4637

4738
public Task<EvaluateResult> EvaluateAsync([StringSyntax(StringSyntaxConstants.JavaScript)] string expression, bool awaitPromise, EvaluateOptions? options = null, ContextTargetOptions? targetOptions = null)

0 commit comments

Comments
 (0)