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] Do not throw when CallFunction or Evaluate return exceptional result (breaking change) #15521

Open
wants to merge 14 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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.ThrowOnError().Result.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.ThrowOnError().Result.ConvertTo<TResult>();
}
}
13 changes: 12 additions & 1 deletion dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,18 @@ public record EvaluateOptions : CommandOptions
//[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
//[JsonDerivedType(typeof(EvaluateResultSuccess), "success")]
//[JsonDerivedType(typeof(EvaluateResultException), "exception")]
public abstract record EvaluateResult;
public abstract record EvaluateResult
{
public EvaluateResultSuccess ThrowOnError()
{
if (this is EvaluateResultSuccess success)
{
return success;
}

throw new ScriptEvaluateException((EvaluateResultException)this);
}
}

public record EvaluateResultSuccess(RemoteValue Result, Realm Realm) : EvaluateResult
{
Expand Down
5 changes: 3 additions & 2 deletions dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ 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";
[JsonPropertyName("type")]
internal string Type => "channel";
}

public record ArrayLocalValue(IEnumerable<LocalValue> Value) : LocalValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ public class ScriptEvaluateException(EvaluateResultException evaluateResultExcep

public long ColumNumber => _evaluateResultException.ExceptionDetails.ColumnNumber;

public override string Message => $"{Text}{Environment.NewLine}{_evaluateResultException.ExceptionDetails.StackTrace}";
public override string Message => $"{Text}{Environment.NewLine}{string.Join(Environment.NewLine, _evaluateResultException.ExceptionDetails.StackTrace.CallFrames)}";
}
23 changes: 7 additions & 16 deletions dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,44 @@
// </copyright>

using OpenQA.Selenium.BiDi.Communication;
using OpenQA.Selenium.DevTools.V132.Page;
using System;
using System.Threading.Tasks;

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 result;
}

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.ThrowOnError().Result.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 result;
}

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.ThrowOnError().Result.ConvertTo<TResult>();
}

public async Task<GetRealmsResult> GetRealmsAsync(GetRealmsOptions? options = null)
Expand Down
Loading
Loading