Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] [bidi] Decouple EvaluateResult in Script module #15493

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
[JsonSerializable(typeof(MessageError))]
[JsonSerializable(typeof(MessageEvent))]

[JsonSerializable(typeof(Modules.Script.EvaluateResult.Success))]
[JsonSerializable(typeof(Modules.Script.EvaluateResult.Exception))]
[JsonSerializable(typeof(Modules.Script.EvaluateResultSuccess))]
[JsonSerializable(typeof(Modules.Script.EvaluateResultException))]

[JsonSerializable(typeof(Modules.Script.NumberRemoteValue))]
[JsonSerializable(typeof(Modules.Script.BooleanRemoteValue))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ internal class EvaluateResultConverter : JsonConverter<EvaluateResult>

return jsonDocument.RootElement.GetProperty("type").ToString() switch
{
"success" => jsonDocument.Deserialize<EvaluateResult.Success>(options),
"exception" => jsonDocument.Deserialize<EvaluateResult.Exception>(options),
"success" => jsonDocument.Deserialize<EvaluateResultSuccess>(options),
"exception" => jsonDocument.Deserialize<EvaluateResultException>(options),
_ => null,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task<IReadOnlyList<RealmInfo>> GetRealmsAsync(GetRealmsOptions? opt
return await scriptModule.GetRealmsAsync(options).ConfigureAwait(false);
}

public Task<EvaluateResult.Success> EvaluateAsync(string expression, bool awaitPromise, EvaluateOptions? options = null, ContextTargetOptions? targetOptions = null)
public Task<EvaluateResultSuccess> EvaluateAsync(string expression, bool awaitPromise, EvaluateOptions? options = null, ContextTargetOptions? targetOptions = null)
{
var contextTarget = new ContextTarget(context);

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

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

Expand Down
17 changes: 8 additions & 9 deletions dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ public record EvaluateOptions : CommandOptions

// https://github.com/dotnet/runtime/issues/72604
//[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
//[JsonDerivedType(typeof(Success), "success")]
//[JsonDerivedType(typeof(Exception), "exception")]
public abstract record EvaluateResult
{
public record Success(RemoteValue Result, Realm Realm) : EvaluateResult
{
public static implicit operator RemoteValue(Success success) => success.Result;
}
//[JsonDerivedType(typeof(EvaluateResultSuccess), "success")]
//[JsonDerivedType(typeof(EvaluateResultException), "exception")]
public abstract record EvaluateResult;

public record Exception(ExceptionDetails ExceptionDetails, Realm Realm) : EvaluateResult;
public record EvaluateResultSuccess(RemoteValue Result, Realm Realm) : EvaluateResult
{
public static implicit operator RemoteValue(EvaluateResultSuccess success) => success.Result;
}

public record EvaluateResultException(ExceptionDetails ExceptionDetails, Realm Realm) : EvaluateResult;

public record ExceptionDetails(long ColumnNumber, long LineNumber, StackTrace StackTrace, string Text);
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

namespace OpenQA.Selenium.BiDi.Modules.Script;

public class ScriptEvaluateException(EvaluateResult.Exception evaluateResultException) : Exception
public class ScriptEvaluateException(EvaluateResultException evaluateResultException) : Exception
{
private readonly EvaluateResult.Exception _evaluateResultException = evaluateResultException;
private readonly EvaluateResultException _evaluateResultException = evaluateResultException;

public string Text => _evaluateResultException.ExceptionDetails.Text;

Expand Down
12 changes: 6 additions & 6 deletions dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ namespace OpenQA.Selenium.BiDi.Modules.Script;

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

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

if (result is EvaluateResult.Exception exp)
if (result is EvaluateResultException exp)
{
throw new ScriptEvaluateException(exp);
}

return (EvaluateResult.Success)result;
return (EvaluateResultSuccess)result;
}

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

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

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

if (result is EvaluateResult.Exception exp)
if (result is EvaluateResultException exp)
{
throw new ScriptEvaluateException(exp);
}

return (EvaluateResult.Success)result;
return (EvaluateResultSuccess)result;
}

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