Skip to content

Commit f457f15

Browse files
[dotnet] [bidi] Do not throw when CallFunction or Evaluate return exceptional result (breaking change) (#15521)
Co-authored-by: Nikolay Borisenko <[email protected]>
1 parent 35c4323 commit f457f15

11 files changed

+193
-238
lines changed

Diff for: dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task<IReadOnlyList<RealmInfo>> GetRealmsAsync(GetRealmsOptions? opt
4444
return await scriptModule.GetRealmsAsync(options).ConfigureAwait(false);
4545
}
4646

47-
public Task<EvaluateResultSuccess> EvaluateAsync(string expression, bool awaitPromise, EvaluateOptions? options = null, ContextTargetOptions? targetOptions = null)
47+
public Task<EvaluateResult> EvaluateAsync(string expression, bool awaitPromise, EvaluateOptions? options = null, ContextTargetOptions? targetOptions = null)
4848
{
4949
var contextTarget = new ContextTarget(context);
5050

@@ -60,10 +60,10 @@ public Task<EvaluateResultSuccess> EvaluateAsync(string expression, bool awaitPr
6060
{
6161
var result = await EvaluateAsync(expression, awaitPromise, options, targetOptions).ConfigureAwait(false);
6262

63-
return result.Result.ConvertTo<TResult>();
63+
return result.AsSuccessResult().ConvertTo<TResult>();
6464
}
6565

66-
public Task<EvaluateResultSuccess> CallFunctionAsync(string functionDeclaration, bool awaitPromise, CallFunctionOptions? options = null, ContextTargetOptions? targetOptions = null)
66+
public Task<EvaluateResult> CallFunctionAsync(string functionDeclaration, bool awaitPromise, CallFunctionOptions? options = null, ContextTargetOptions? targetOptions = null)
6767
{
6868
var contextTarget = new ContextTarget(context);
6969

@@ -79,6 +79,6 @@ public Task<EvaluateResultSuccess> CallFunctionAsync(string functionDeclaration,
7979
{
8080
var result = await CallFunctionAsync(functionDeclaration, awaitPromise, options, targetOptions).ConfigureAwait(false);
8181

82-
return result.Result.ConvertTo<TResult>();
82+
return result.AsSuccessResult().ConvertTo<TResult>();
8383
}
8484
}

Diff for: dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.BiDi.Communication;
21+
using System;
2122

2223
namespace OpenQA.Selenium.BiDi.Modules.Script;
2324

@@ -39,7 +40,18 @@ public record EvaluateOptions : CommandOptions
3940
//[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
4041
//[JsonDerivedType(typeof(EvaluateResultSuccess), "success")]
4142
//[JsonDerivedType(typeof(EvaluateResultException), "exception")]
42-
public abstract record EvaluateResult : EmptyResult;
43+
public abstract record EvaluateResult : EmptyResult
44+
{
45+
public RemoteValue AsSuccessResult()
46+
{
47+
if (this is EvaluateResultSuccess success)
48+
{
49+
return success.Result;
50+
}
51+
52+
throw new InvalidCastException($"Expected the result to be {nameof(EvaluateResultSuccess)}, but received {this}");
53+
}
54+
}
4355

4456
public record EvaluateResultSuccess(RemoteValue Result, Realm Realm) : EvaluateResult
4557
{

Diff for: dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,9 @@ public record BigIntLocalValue(string Value) : PrimitiveProtocolLocalValue;
299299

300300
public record ChannelLocalValue(ChannelProperties Value) : LocalValue
301301
{
302-
// TODO: Revise why we need it
302+
// AddPreloadScript takes arguments typed as ChannelLocalValue but still requires "type":"channel"
303303
[JsonInclude]
304-
internal string type = "channel";
304+
internal string Type => "channel";
305305
}
306306

307307
public record ArrayLocalValue(IEnumerable<LocalValue> Value) : LocalValue;

Diff for: dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs

-33
This file was deleted.

Diff for: dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs

+6-20
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,32 @@ namespace OpenQA.Selenium.BiDi.Modules.Script;
2525

2626
public sealed class ScriptModule(Broker broker) : Module(broker)
2727
{
28-
public async Task<EvaluateResultSuccess> EvaluateAsync(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
28+
public async Task<EvaluateResult> EvaluateAsync(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
2929
{
3030
var @params = new EvaluateCommandParameters(expression, target, awaitPromise, options?.ResultOwnership, options?.SerializationOptions, options?.UserActivation);
3131

32-
var result = await Broker.ExecuteCommandAsync<EvaluateCommand, EvaluateResult>(new EvaluateCommand(@params), options).ConfigureAwait(false);
33-
34-
if (result is EvaluateResultException exp)
35-
{
36-
throw new ScriptEvaluateException(exp);
37-
}
38-
39-
return (EvaluateResultSuccess)result;
32+
return await Broker.ExecuteCommandAsync<EvaluateCommand, EvaluateResult>(new EvaluateCommand(@params), options).ConfigureAwait(false);
4033
}
4134

4235
public async Task<TResult?> EvaluateAsync<TResult>(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
4336
{
4437
var result = await EvaluateAsync(expression, awaitPromise, target, options).ConfigureAwait(false);
4538

46-
return result.Result.ConvertTo<TResult>();
39+
return result.AsSuccessResult().ConvertTo<TResult>();
4740
}
4841

49-
public async Task<EvaluateResultSuccess> CallFunctionAsync(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null)
42+
public async Task<EvaluateResult> CallFunctionAsync(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null)
5043
{
5144
var @params = new CallFunctionCommandParameters(functionDeclaration, awaitPromise, target, options?.Arguments, options?.ResultOwnership, options?.SerializationOptions, options?.This, options?.UserActivation);
5245

53-
var result = await Broker.ExecuteCommandAsync<CallFunctionCommand, EvaluateResult>(new CallFunctionCommand(@params), options).ConfigureAwait(false);
54-
55-
if (result is EvaluateResultException exp)
56-
{
57-
throw new ScriptEvaluateException(exp);
58-
}
59-
60-
return (EvaluateResultSuccess)result;
46+
return await Broker.ExecuteCommandAsync<CallFunctionCommand, EvaluateResult>(new CallFunctionCommand(@params), options).ConfigureAwait(false);
6147
}
6248

6349
public async Task<TResult?> CallFunctionAsync<TResult>(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null)
6450
{
6551
var result = await CallFunctionAsync(functionDeclaration, awaitPromise, target, options).ConfigureAwait(false);
6652

67-
return result.Result.ConvertTo<TResult>();
53+
return result.AsSuccessResult().ConvertTo<TResult>();
6854
}
6955

7056
public async Task<GetRealmsResult> GetRealmsAsync(GetRealmsOptions? options = null)

Diff for: dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121

2222
namespace OpenQA.Selenium.BiDi.Modules.Script;
2323

24-
public record StackTrace(IReadOnlyCollection<StackFrame> CallFrames);
24+
public record StackTrace(IReadOnlyList<StackFrame> CallFrames);

0 commit comments

Comments
 (0)