Skip to content

Commit 43fcf45

Browse files
authored
[dotnet] [bidi] Decouple EvaluateResult in Script module (#15493)
1 parent 64dbbfc commit 43fcf45

File tree

6 files changed

+22
-23
lines changed

6 files changed

+22
-23
lines changed

dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
2727
[JsonSerializable(typeof(MessageError))]
2828
[JsonSerializable(typeof(MessageEvent))]
2929

30-
[JsonSerializable(typeof(Modules.Script.EvaluateResult.Success))]
31-
[JsonSerializable(typeof(Modules.Script.EvaluateResult.Exception))]
30+
[JsonSerializable(typeof(Modules.Script.EvaluateResultSuccess))]
31+
[JsonSerializable(typeof(Modules.Script.EvaluateResultException))]
3232

3333
[JsonSerializable(typeof(Modules.Script.NumberRemoteValue))]
3434
[JsonSerializable(typeof(Modules.Script.BooleanRemoteValue))]

dotnet/src/webdriver/BiDi/Communication/Json/Converters/Polymorphic/EvaluateResultConverter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ internal class EvaluateResultConverter : JsonConverter<EvaluateResult>
3333

3434
return jsonDocument.RootElement.GetProperty("type").ToString() switch
3535
{
36-
"success" => jsonDocument.Deserialize<EvaluateResult.Success>(options),
37-
"exception" => jsonDocument.Deserialize<EvaluateResult.Exception>(options),
36+
"success" => jsonDocument.Deserialize<EvaluateResultSuccess>(options),
37+
"exception" => jsonDocument.Deserialize<EvaluateResultException>(options),
3838
_ => null,
3939
};
4040
}

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

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

@@ -63,7 +63,7 @@ public async Task<IReadOnlyList<RealmInfo>> GetRealmsAsync(GetRealmsOptions? opt
6363
return result.Result.ConvertTo<TResult>();
6464
}
6565

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

dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs

+8-9
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@ public record EvaluateOptions : CommandOptions
3737

3838
// https://github.com/dotnet/runtime/issues/72604
3939
//[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
40-
//[JsonDerivedType(typeof(Success), "success")]
41-
//[JsonDerivedType(typeof(Exception), "exception")]
42-
public abstract record EvaluateResult
43-
{
44-
public record Success(RemoteValue Result, Realm Realm) : EvaluateResult
45-
{
46-
public static implicit operator RemoteValue(Success success) => success.Result;
47-
}
40+
//[JsonDerivedType(typeof(EvaluateResultSuccess), "success")]
41+
//[JsonDerivedType(typeof(EvaluateResultException), "exception")]
42+
public abstract record EvaluateResult;
4843

49-
public record Exception(ExceptionDetails ExceptionDetails, Realm Realm) : EvaluateResult;
44+
public record EvaluateResultSuccess(RemoteValue Result, Realm Realm) : EvaluateResult
45+
{
46+
public static implicit operator RemoteValue(EvaluateResultSuccess success) => success.Result;
5047
}
5148

49+
public record EvaluateResultException(ExceptionDetails ExceptionDetails, Realm Realm) : EvaluateResult;
50+
5251
public record ExceptionDetails(long ColumnNumber, long LineNumber, StackTrace StackTrace, string Text);

dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

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

24-
public class ScriptEvaluateException(EvaluateResult.Exception evaluateResultException) : Exception
24+
public class ScriptEvaluateException(EvaluateResultException evaluateResultException) : Exception
2525
{
26-
private readonly EvaluateResult.Exception _evaluateResultException = evaluateResultException;
26+
private readonly EvaluateResultException _evaluateResultException = evaluateResultException;
2727

2828
public string Text => _evaluateResultException.ExceptionDetails.Text;
2929

dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs

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

2626
public sealed class ScriptModule(Broker broker) : Module(broker)
2727
{
28-
public async Task<EvaluateResult.Success> EvaluateAsync(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
28+
public async Task<EvaluateResultSuccess> 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

3232
var result = await Broker.ExecuteCommandAsync<EvaluateCommand, EvaluateResult>(new EvaluateCommand(@params), options).ConfigureAwait(false);
3333

34-
if (result is EvaluateResult.Exception exp)
34+
if (result is EvaluateResultException exp)
3535
{
3636
throw new ScriptEvaluateException(exp);
3737
}
3838

39-
return (EvaluateResult.Success)result;
39+
return (EvaluateResultSuccess)result;
4040
}
4141

4242
public async Task<TResult?> EvaluateAsync<TResult>(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
@@ -46,18 +46,18 @@ public sealed class ScriptModule(Broker broker) : Module(broker)
4646
return result.Result.ConvertTo<TResult>();
4747
}
4848

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

5353
var result = await Broker.ExecuteCommandAsync<CallFunctionCommand, EvaluateResult>(new CallFunctionCommand(@params), options).ConfigureAwait(false);
5454

55-
if (result is EvaluateResult.Exception exp)
55+
if (result is EvaluateResultException exp)
5656
{
5757
throw new ScriptEvaluateException(exp);
5858
}
5959

60-
return (EvaluateResult.Success)result;
60+
return (EvaluateResultSuccess)result;
6161
}
6262

6363
public async Task<TResult?> CallFunctionAsync<TResult>(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null)

0 commit comments

Comments
 (0)