Skip to content

[dotnet] [bidi] Do not throw when CallFunction or Evaluate return exceptional result (breaking change) #15521

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

Merged
merged 21 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b29c80f
[dotnet] [bidi] Do not throw when CallFunction or Evaluate return exc…
RenderMichael Mar 27, 2025
63e744e
Remote errant import
RenderMichael Mar 27, 2025
7b7338c
merge trunk
RenderMichael Mar 28, 2025
f88cc83
fix whitespace
RenderMichael Mar 28, 2025
dace752
fix whitespace further
RenderMichael Mar 28, 2025
9bc66bf
Merge branch 'trunk' into evaluate-throw
RenderMichael Apr 2, 2025
c2b8a5a
Merge branch 'trunk' into evaluate-throw
RenderMichael Apr 3, 2025
1d2323e
Rename`EvaluateResult.ThrowOnError` to `EvaluateResult.AsSuccess`
RenderMichael Apr 3, 2025
1684694
Rename `AsSuccess()` to `AsSuccessResult()`
RenderMichael Apr 3, 2025
11d4c7d
Change `AsSuccessResult()` to return the `RemoteValue` directly.
RenderMichael Apr 3, 2025
23e7e8e
Rename ScriptEvaluateException.ExceptionDetails
RenderMichael Apr 3, 2025
68e11ca
Remove unnecessary attribute
RenderMichael Apr 8, 2025
3845f05
Remove string evaluation exception, use BiDiException with string for…
RenderMichael Apr 8, 2025
daed241
whitespace
RenderMichael Apr 8, 2025
c718a39
Merge remote-tracking branch 'upstream/trunk' into pr/15521
nvborisenko Apr 13, 2025
a8b7410
Fix merge
nvborisenko Apr 13, 2025
ed99506
Remove FormatException
nvborisenko Apr 13, 2025
7902e1d
Reveal original non-success error
nvborisenko Apr 13, 2025
254a377
Remove formatting, the same will be done in another way
nvborisenko Apr 13, 2025
81c629d
Merge branch 'trunk' into evaluate-throw
nvborisenko Apr 13, 2025
5759846
Update ScriptModule.cs
nvborisenko Apr 13, 2025
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 @@ -44,7 +44,7 @@ public async Task<IReadOnlyList<RealmInfo>> GetRealmsAsync(GetRealmsOptions? opt
return await scriptModule.GetRealmsAsync(options).ConfigureAwait(false);
}

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

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

return result.Result.ConvertTo<TResult>();
return result.AsSuccessResult().ConvertTo<TResult>();
}

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

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

return result.Result.ConvertTo<TResult>();
return result.AsSuccessResult().ConvertTo<TResult>();
}
}
14 changes: 13 additions & 1 deletion dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// </copyright>

using OpenQA.Selenium.BiDi.Communication;
using System;

namespace OpenQA.Selenium.BiDi.Modules.Script;

Expand All @@ -39,7 +40,18 @@ public record EvaluateOptions : CommandOptions
//[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
//[JsonDerivedType(typeof(EvaluateResultSuccess), "success")]
//[JsonDerivedType(typeof(EvaluateResultException), "exception")]
public abstract record EvaluateResult : EmptyResult;
public abstract record EvaluateResult : EmptyResult
{
public RemoteValue AsSuccessResult()
{
if (this is EvaluateResultSuccess success)
{
return success.Result;
}

throw new InvalidCastException($"Expected the result to be {nameof(EvaluateResultSuccess)}, but received {this}");
}
}

public record EvaluateResultSuccess(RemoteValue Result, Realm Realm) : EvaluateResult
{
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ public record BigIntLocalValue(string Value) : PrimitiveProtocolLocalValue;

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

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

This file was deleted.

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

public sealed class ScriptModule(Broker broker) : Module(broker)
{
public async Task<EvaluateResultSuccess> EvaluateAsync(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
public async Task<EvaluateResult> 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 EvaluateResultException exp)
{
throw new ScriptEvaluateException(exp);
}

return (EvaluateResultSuccess)result;
return await Broker.ExecuteCommandAsync<EvaluateCommand, EvaluateResult>(new EvaluateCommand(@params), options).ConfigureAwait(false);
}

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

return result.Result.ConvertTo<TResult>();
return result.AsSuccessResult().ConvertTo<TResult>();
}

public async Task<EvaluateResultSuccess> CallFunctionAsync(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null)
public async Task<EvaluateResult> 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 EvaluateResultException exp)
{
throw new ScriptEvaluateException(exp);
}

return (EvaluateResultSuccess)result;
return await Broker.ExecuteCommandAsync<CallFunctionCommand, EvaluateResult>(new CallFunctionCommand(@params), options).ConfigureAwait(false);
}

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

return result.Result.ConvertTo<TResult>();
return result.AsSuccessResult().ConvertTo<TResult>();
}

public async Task<GetRealmsResult> GetRealmsAsync(GetRealmsOptions? options = null)
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

namespace OpenQA.Selenium.BiDi.Modules.Script;

public record StackTrace(IReadOnlyCollection<StackFrame> CallFrames);
public record StackTrace(IReadOnlyList<StackFrame> CallFrames);
Loading
Loading