From b29c80f4959152b3785898e432edb8ce9d65692b Mon Sep 17 00:00:00 2001 From: Michael Render Date: Thu, 27 Mar 2025 01:19:38 -0400 Subject: [PATCH 01/16] [dotnet] [bidi] Do not throw when CallFunction or Evaluate return exceptional results --- .../BrowsingContextScriptModule.cs | 8 +- .../BiDi/Modules/Script/EvaluateCommand.cs | 13 +- .../BiDi/Modules/Script/LocalValue.cs | 5 +- .../Modules/Script/ScriptEvaluateException.cs | 2 +- .../BiDi/Modules/Script/ScriptModule.cs | 23 +-- .../BiDi/Script/CallFunctionLocalValueTest.cs | 188 ++++++++---------- .../BiDi/Script/CallFunctionParameterTest.cs | 45 +++-- .../Script/CallFunctionRemoteValueTest.cs | 76 +++---- .../BiDi/Script/EvaluateParametersTest.cs | 23 ++- .../common/BiDi/Script/ScriptCommandsTest.cs | 2 +- 10 files changed, 189 insertions(+), 196 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs index 82ae97d28bd72..b8accdbfe18eb 100644 --- a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs @@ -44,7 +44,7 @@ public async Task> GetRealmsAsync(GetRealmsOptions? opt return await scriptModule.GetRealmsAsync(options).ConfigureAwait(false); } - public Task EvaluateAsync(string expression, bool awaitPromise, EvaluateOptions? options = null, ContextTargetOptions? targetOptions = null) + public Task EvaluateAsync(string expression, bool awaitPromise, EvaluateOptions? options = null, ContextTargetOptions? targetOptions = null) { var contextTarget = new ContextTarget(context); @@ -60,10 +60,10 @@ public Task EvaluateAsync(string expression, bool awaitPr { var result = await EvaluateAsync(expression, awaitPromise, options, targetOptions).ConfigureAwait(false); - return result.Result.ConvertTo(); + return result.ThrowOnError().Result.ConvertTo(); } - public Task CallFunctionAsync(string functionDeclaration, bool awaitPromise, CallFunctionOptions? options = null, ContextTargetOptions? targetOptions = null) + public Task CallFunctionAsync(string functionDeclaration, bool awaitPromise, CallFunctionOptions? options = null, ContextTargetOptions? targetOptions = null) { var contextTarget = new ContextTarget(context); @@ -79,6 +79,6 @@ public Task CallFunctionAsync(string functionDeclaration, { var result = await CallFunctionAsync(functionDeclaration, awaitPromise, options, targetOptions).ConfigureAwait(false); - return result.Result.ConvertTo(); + return result.ThrowOnError().Result.ConvertTo(); } } diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs index d125b59fd706b..7a603549ff0fb 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs @@ -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 { diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs b/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs index cbb70c50a6b20..c861d20b7ce00 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs @@ -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 Value) : LocalValue; diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs index 6b8f92deedcbc..493cf20619e53 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs @@ -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)}"; } diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs index fde575506f88e..5eb81cbe0573e 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs @@ -18,6 +18,7 @@ // using OpenQA.Selenium.BiDi.Communication; +using OpenQA.Selenium.DevTools.V132.Page; using System; using System.Threading.Tasks; @@ -25,46 +26,36 @@ namespace OpenQA.Selenium.BiDi.Modules.Script; public sealed class ScriptModule(Broker broker) : Module(broker) { - public async Task EvaluateAsync(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null) + public async Task 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(new EvaluateCommand(@params), options).ConfigureAwait(false); - if (result is EvaluateResultException exp) - { - throw new ScriptEvaluateException(exp); - } - - return (EvaluateResultSuccess)result; + return result; } public async Task EvaluateAsync(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null) { var result = await EvaluateAsync(expression, awaitPromise, target, options).ConfigureAwait(false); - return result.Result.ConvertTo(); + return result.ThrowOnError().Result.ConvertTo(); } - public async Task CallFunctionAsync(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null) + public async Task 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(new CallFunctionCommand(@params), options).ConfigureAwait(false); - if (result is EvaluateResultException exp) - { - throw new ScriptEvaluateException(exp); - } - - return (EvaluateResultSuccess)result; + return result; } public async Task CallFunctionAsync(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null) { var result = await CallFunctionAsync(functionDeclaration, awaitPromise, target, options).ConfigureAwait(false); - return result.Result.ConvertTo(); + return result.ThrowOnError().Result.ConvertTo(); } public async Task GetRealmsAsync(GetRealmsOptions? options = null) diff --git a/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs index 5def3928912e6..07f3bd679fd21 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs @@ -19,347 +19,335 @@ using NUnit.Framework; using OpenQA.Selenium.BiDi.Modules.Script; +using System.Threading.Tasks; namespace OpenQA.Selenium.BiDi.Script; class CallFunctionLocalValueTest : BiDiTestFixture { [Test] - public void CanCallFunctionWithArgumentUndefined() + public async Task CanCallFunctionWithArgumentUndefined() { var arg = new UndefinedLocalValue(); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (typeof arg !== 'undefined') { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentNull() + public async Task CanCallFunctionWithArgumentNull() { var arg = new NullLocalValue(); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg !== null) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentBoolean() + public async Task CanCallFunctionWithArgumentBoolean() { var arg = new BooleanLocalValue(true); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg !== true) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentBigInt() + public async Task CanCallFunctionWithArgumentBigInt() { var arg = new BigIntLocalValue("12345"); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg !== 12345n) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentEmptyString() + public async Task CanCallFunctionWithArgumentEmptyString() { var arg = new StringLocalValue(string.Empty); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg !== '') { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentNonEmptyString() + public async Task CanCallFunctionWithArgumentNonEmptyString() { var arg = new StringLocalValue("whoa"); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg !== 'whoa') { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentRecentDate() + public async Task CanCallFunctionWithArgumentRecentDate() { const string PinnedDateTimeString = "2025-03-09T00:30:33.083Z"; var arg = new DateLocalValue(PinnedDateTimeString); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg.toISOString() !== '{{PinnedDateTimeString}}') { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentEpochDate() + public async Task CanCallFunctionWithArgumentEpochDate() { const string EpochString = "1970-01-01T00:00:00.000Z"; var arg = new DateLocalValue(EpochString); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg.toISOString() !== '{{EpochString}}') { throw new Error("Assert failed: " + arg.toISOString()); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentNumberFive() + public async Task CanCallFunctionWithArgumentNumberFive() { var arg = new NumberLocalValue(5); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg !== 5) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentNumberNegativeFive() + public async Task CanCallFunctionWithArgumentNumberNegativeFive() { var arg = new NumberLocalValue(-5); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg !== -5) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentNumberZero() + public async Task CanCallFunctionWithArgumentNumberZero() { var arg = new NumberLocalValue(0); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg !== 0) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] [IgnoreBrowser(Selenium.Browser.Edge, "Chromium can't handle -0 argument as a number: https://github.com/w3c/webdriver-bidi/issues/887")] [IgnoreBrowser(Selenium.Browser.Chrome, "Chromium can't handle -0 argument as a number: https://github.com/w3c/webdriver-bidi/issues/887")] - public void CanCallFunctionWithArgumentNumberNegativeZero() + public async Task CanCallFunctionWithArgumentNumberNegativeZero() { var arg = new NumberLocalValue(double.NegativeZero); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (!Object.is(arg, -0)) { throw new Error("Assert failed: " + arg.toLocaleString()); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentNumberPositiveInfinity() + public async Task CanCallFunctionWithArgumentNumberPositiveInfinity() { var arg = new NumberLocalValue(double.PositiveInfinity); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg !== Number.POSITIVE_INFINITY) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentNumberNegativeInfinity() + public async Task CanCallFunctionWithArgumentNumberNegativeInfinity() { var arg = new NumberLocalValue(double.NegativeInfinity); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg !== Number.NEGATIVE_INFINITY) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentNumberNaN() + public async Task CanCallFunctionWithArgumentNumberNaN() { var arg = new NumberLocalValue(double.NaN); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (!isNaN(arg)) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentRegExp() + public async Task CanCallFunctionWithArgumentRegExp() { var arg = new RegExpLocalValue(new RegExpValue("foo*") { Flags = "g" }); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (!arg.test('foo') || arg.source !== 'foo*' || !arg.global) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentArray() + public async Task CanCallFunctionWithArgumentArray() { var arg = new ArrayLocalValue([new StringLocalValue("hi")]); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg.length !== 1 || arg[0] !== 'hi') { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentObject() + public async Task CanCallFunctionWithArgumentObject() { var arg = new ObjectLocalValue([[new StringLocalValue("objKey"), new StringLocalValue("objValue")]]); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg.objKey !== 'objValue' || Object.keys(arg).length !== 1) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentMap() + public async Task CanCallFunctionWithArgumentMap() { var arg = new MapLocalValue([[new StringLocalValue("mapKey"), new StringLocalValue("mapValue")]]); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg.get('mapKey') !== 'mapValue' || arg.size !== 1) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } [Test] - public void CanCallFunctionWithArgumentSet() + public async Task CanCallFunctionWithArgumentSet() { var arg = new SetLocalValue([new StringLocalValue("setKey")]); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (!arg.has('setKey') || arg.size !== 1) { throw new Error("Assert failed: " + arg); } } """, false, new() { Arguments = [arg] }); - }, Throws.Nothing); + + Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } } diff --git a/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs index 957b75eece58f..3f753223d6fc6 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs @@ -31,8 +31,8 @@ public async Task CanCallFunctionWithDeclaration() var res = await context.Script.CallFunctionAsync("() => { return 1 + 2; }", false); Assert.That(res, Is.Not.Null); - Assert.That(res.Realm, Is.Not.Null); - Assert.That((res.Result as NumberRemoteValue).Value, Is.EqualTo(3)); + Assert.That(res.ThrowOnError().Realm, Is.Not.Null); + Assert.That((res.ThrowOnError().Result as NumberRemoteValue).Value, Is.EqualTo(3)); } [Test] @@ -75,9 +75,9 @@ public async Task CanCallFunctionWithArguments() Arguments = ["abc", 42] }); - Assert.That(res.Result, Is.AssignableFrom()); - Assert.That((string)(res.Result as ArrayRemoteValue).Value[0], Is.EqualTo("abc")); - Assert.That((int)(res.Result as ArrayRemoteValue).Value[1], Is.EqualTo(42)); + Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); + Assert.That((string)(res.ThrowOnError().Result as ArrayRemoteValue).Value[0], Is.EqualTo("abc")); + Assert.That((int)(res.ThrowOnError().Result as ArrayRemoteValue).Value[1], Is.EqualTo(42)); } [Test] @@ -90,8 +90,8 @@ public async Task CanCallFunctionToGetIFrameBrowsingContext() """, false); Assert.That(res, Is.Not.Null); - Assert.That(res.Result, Is.AssignableFrom()); - Assert.That((res.Result as WindowProxyRemoteValue).Value, Is.Not.Null); + Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); + Assert.That((res.ThrowOnError().Result as WindowProxyRemoteValue).Value, Is.Not.Null); } [Test] @@ -104,8 +104,8 @@ public async Task CanCallFunctionToGetElement() """, false); Assert.That(res, Is.Not.Null); - Assert.That(res.Result, Is.AssignableFrom()); - Assert.That((res.Result as NodeRemoteValue).Value, Is.Not.Null); + Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); + Assert.That((res.ThrowOnError().Result as NodeRemoteValue).Value, Is.Not.Null); } [Test] @@ -132,7 +132,7 @@ async function() { """, awaitPromise: false); Assert.That(res, Is.Not.Null); - Assert.That(res.Result, Is.AssignableFrom()); + Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); } [Test] @@ -156,9 +156,9 @@ public async Task CanCallFunctionWithOwnershipRoot() }); Assert.That(res, Is.Not.Null); - Assert.That((res.Result as ObjectRemoteValue).Handle, Is.Not.Null); - Assert.That((string)(res.Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.ThrowOnError().Result as ObjectRemoteValue).Handle, Is.Not.Null); + Assert.That((string)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] @@ -170,17 +170,18 @@ public async Task CanCallFunctionWithOwnershipNone() }); Assert.That(res, Is.Not.Null); - Assert.That((res.Result as ObjectRemoteValue).Handle, Is.Null); - Assert.That((string)(res.Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.ThrowOnError().Result as ObjectRemoteValue).Handle, Is.Null); + Assert.That((string)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] - public void CanCallFunctionThatThrowsException() + public async Task CanCallFunctionThatThrowsExceptionAsync() { - var action = () => context.Script.CallFunctionAsync("))) !!@@## some invalid JS script (((", false); + var action = await context.Script.CallFunctionAsync("))) !!@@## some invalid JS script (((", false); - Assert.That(action, Throws.InstanceOf().And.Message.Contain("SyntaxError:")); + Assert.That(action, Is.TypeOf()); + Assert.That(((EvaluateResultException)action).ExceptionDetails.Text, Does.Contain("SyntaxError:")); } [Test] @@ -191,7 +192,7 @@ public async Task CanCallFunctionInASandBox() var res = await context.Script.CallFunctionAsync("() => window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.Result, Is.AssignableFrom()); + Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); // Make changes in the sandbox await context.Script.CallFunctionAsync("() => { window.foo = 2; }", true, targetOptions: new() { Sandbox = "sandbox" }); @@ -199,8 +200,8 @@ public async Task CanCallFunctionInASandBox() // Check if the changes are present in the sandbox res = await context.Script.CallFunctionAsync("() => window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.Result, Is.AssignableFrom()); - Assert.That((res.Result as NumberRemoteValue).Value, Is.EqualTo(2)); + Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); + Assert.That((res.ThrowOnError().Result as NumberRemoteValue).Value, Is.EqualTo(2)); } [Test] diff --git a/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs index 9b9fde3ecfb6b..225048d74f7d2 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs @@ -30,7 +30,7 @@ public async Task CanCallFunctionAndReturnUndefined() { var response = await context.Script.CallFunctionAsync("() => { return undefined; }", false); - Assert.That(response.Result, Is.AssignableTo()); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); } [Test] @@ -38,7 +38,7 @@ public async Task CanCallFunctionAndReturnNull() { var response = await context.Script.CallFunctionAsync("() => { return null; }", false); - Assert.That(response.Result, Is.AssignableTo()); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); } [Test] @@ -46,8 +46,8 @@ public async Task CanCallFunctionAndReturnTrue() { var response = await context.Script.CallFunctionAsync("() => { return true; }", false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(((BooleanRemoteValue)response.Result).Value, Is.True); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(((BooleanRemoteValue)response.ThrowOnError().Result).Value, Is.True); } [Test] @@ -55,8 +55,8 @@ public async Task CanCallFunctionAndReturnFalse() { var response = await context.Script.CallFunctionAsync("() => { return false; }", false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(((BooleanRemoteValue)response.Result).Value, Is.False); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(((BooleanRemoteValue)response.ThrowOnError().Result).Value, Is.False); } @@ -65,8 +65,8 @@ public async Task CanCallFunctionAndReturnEmptyString() { var response = await context.Script.CallFunctionAsync("() => { return ''; }", false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(((StringRemoteValue)response.Result).Value, Is.Empty); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(((StringRemoteValue)response.ThrowOnError().Result).Value, Is.Empty); } [Test] @@ -74,8 +74,8 @@ public async Task CanCallFunctionAndReturnNonEmptyString() { var response = await context.Script.CallFunctionAsync("() => { return 'whoa'; }", false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(((StringRemoteValue)response.Result).Value, Is.EqualTo("whoa")); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(((StringRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo("whoa")); } [Test] @@ -85,8 +85,8 @@ public async Task CanCallFunctionAndReturnRecentDate() var response = await context.Script.CallFunctionAsync($$"""() => { return new Date('{{PinnedDateTimeString}}'); }""", false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(response.Result, Is.EqualTo(new DateRemoteValue(PinnedDateTimeString))); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(response.ThrowOnError().Result, Is.EqualTo(new DateRemoteValue(PinnedDateTimeString))); } [Test] @@ -96,8 +96,8 @@ public async Task CanCallFunctionAndReturnUnixEpoch() var response = await context.Script.CallFunctionAsync($$"""() => { return new Date('{{EpochString}}'); }""", false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(response.Result, Is.EqualTo(new DateRemoteValue(EpochString))); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(response.ThrowOnError().Result, Is.EqualTo(new DateRemoteValue(EpochString))); } [Test] @@ -105,8 +105,8 @@ public async Task CanCallFunctionAndReturnNumberFive() { var response = await context.Script.CallFunctionAsync("() => { return 5; }", false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.Result).Value, Is.EqualTo(5)); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(5)); } [Test] @@ -114,8 +114,8 @@ public async Task CanCallFunctionAndReturnNumberNegativeFive() { var response = await context.Script.CallFunctionAsync("() => { return -5; }", false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.Result).Value, Is.EqualTo(-5)); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(-5)); } [Test] @@ -123,8 +123,8 @@ public async Task CanCallFunctionAndReturnNumberZero() { var response = await context.Script.CallFunctionAsync("() => { return 0; }", false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.Result).Value, Is.Zero); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.ThrowOnError().Result).Value, Is.Zero); } [Test] @@ -132,9 +132,9 @@ public async Task CanCallFunctionAndReturnNumberNegativeZero() { var response = await context.Script.CallFunctionAsync("() => { return -0; }", false); - Assert.That(response.Result, Is.AssignableTo()); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - var actualNumberValue = ((NumberRemoteValue)response.Result).Value; + var actualNumberValue = ((NumberRemoteValue)response.ThrowOnError().Result).Value; Assert.That(actualNumberValue, Is.Zero); Assert.That(double.IsNegative(actualNumberValue), Is.True); } @@ -144,9 +144,9 @@ public async Task CanCallFunctionAndReturnNumberPositiveInfinity() { var response = await context.Script.CallFunctionAsync("() => { return Number.POSITIVE_INFINITY; }", false); - Assert.That(response.Result, Is.AssignableTo()); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.Result).Value; + var expectedInfinity = ((NumberRemoteValue)response.ThrowOnError().Result).Value; Assert.That(double.IsPositiveInfinity(expectedInfinity)); } @@ -155,9 +155,9 @@ public async Task CanCallFunctionAndReturnNumberNegativeInfinity() { var response = await context.Script.CallFunctionAsync("() => { return Number.NEGATIVE_INFINITY; }", false); - Assert.That(response.Result, Is.AssignableTo()); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.Result).Value; + var expectedInfinity = ((NumberRemoteValue)response.ThrowOnError().Result).Value; Assert.That(double.IsNegativeInfinity(expectedInfinity)); } @@ -166,8 +166,8 @@ public async Task CanCallFunctionAndReturnNumberNaN() { var response = await context.Script.CallFunctionAsync("() => { return NaN; }", false); - Assert.That(response.Result, Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.Result).Value; + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + var expectedInfinity = ((NumberRemoteValue)response.ThrowOnError().Result).Value; Assert.That(double.IsNaN(expectedInfinity)); } @@ -176,8 +176,8 @@ public async Task CanCallFunctionAndReturnRegExp() { var response = await context.Script.CallFunctionAsync("() => { return /foo*/g; }", false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(response.Result, Is.EqualTo(new RegExpRemoteValue(new RegExpValue("foo*") { Flags = "g" }))); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(response.ThrowOnError().Result, Is.EqualTo(new RegExpRemoteValue(new RegExpValue("foo*") { Flags = "g" }))); } [Test] @@ -186,8 +186,8 @@ public async Task CanCallFunctionAndReturnArray() var response = await context.Script.CallFunctionAsync("() => { return ['hi']; }", false); var expectedArray = new ArrayRemoteValue { Value = [new StringRemoteValue("hi")] }; - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(((ArrayRemoteValue)response.Result).Value, Is.EqualTo(expectedArray.Value)); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(((ArrayRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(expectedArray.Value)); } [Test] @@ -195,13 +195,13 @@ public async Task CanCallFunctionAndReturnObject() { var response = await context.Script.CallFunctionAsync("() => { return { objKey: 'objValue' }; }", false); - Assert.That(response.Result, Is.AssignableTo()); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); var expected = new ObjectRemoteValue { Value = [[new StringRemoteValue("objKey"), new StringRemoteValue("objValue")]] }; - Assert.That(((ObjectRemoteValue)response.Result).Value, Is.EqualTo(expected.Value)); + Assert.That(((ObjectRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(expected.Value)); } [Test] @@ -220,8 +220,8 @@ public async Task CanCallFunctionAndReturnMap() } """, false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(((MapRemoteValue)response.Result).Value, Is.EqualTo(expected.Value)); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(((MapRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(expected.Value)); } [Test] @@ -236,7 +236,7 @@ public async Task CanCallFunctionAndReturnSet() } """, false); - Assert.That(response.Result, Is.AssignableTo()); - Assert.That(((SetRemoteValue)response.Result).Value, Is.EqualTo(expected.Value)); + Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(((SetRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(expected.Value)); } } diff --git a/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs b/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs index 4d389643e808d..dd7de6c480cf8 100644 --- a/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs +++ b/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs @@ -31,8 +31,8 @@ public async Task CanEvaluateScript() var res = await context.Script.EvaluateAsync("1 + 2", false); Assert.That(res, Is.Not.Null); - Assert.That(res.Realm, Is.Not.Null); - Assert.That((res.Result as NumberRemoteValue).Value, Is.EqualTo(3)); + Assert.That(res.ThrowOnError().Realm, Is.Not.Null); + Assert.That((res.ThrowOnError().Result as NumberRemoteValue).Value, Is.EqualTo(3)); } [Test] @@ -68,11 +68,12 @@ public async Task СanEvaluateScriptWithUserActivationFalse() } [Test] - public void CanCallFunctionThatThrowsException() + public async Task CanCallFunctionThatThrowsException() { - var action = () => context.Script.EvaluateAsync("))) !!@@## some invalid JS script (((", false); + var errorResult = await context.Script.EvaluateAsync("))) !!@@## some invalid JS script (((", false); - Assert.That(action, Throws.InstanceOf().And.Message.Contain("SyntaxError:")); + Assert.That(errorResult, Is.TypeOf()); + Assert.That(((EvaluateResultException)errorResult).ExceptionDetails.Text, Does.Contain("SyntaxError:")); } [Test] @@ -84,9 +85,9 @@ public async Task CanEvaluateScriptWithResulWithOwnership() }); Assert.That(res, Is.Not.Null); - Assert.That((res.Result as ObjectRemoteValue).Handle, Is.Not.Null); - Assert.That((string)(res.Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.ThrowOnError().Result as ObjectRemoteValue).Handle, Is.Not.Null); + Assert.That((string)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] @@ -97,7 +98,7 @@ public async Task CanEvaluateInASandBox() var res = await context.Script.EvaluateAsync("window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.Result, Is.AssignableFrom()); + Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); // Make changes in the sandbox await context.Script.EvaluateAsync("window.foo = 2", true, targetOptions: new() { Sandbox = "sandbox" }); @@ -105,8 +106,8 @@ public async Task CanEvaluateInASandBox() // Check if the changes are present in the sandbox res = await context.Script.EvaluateAsync("window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.Result, Is.AssignableFrom()); - Assert.That((res.Result as NumberRemoteValue).Value, Is.EqualTo(2)); + Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); + Assert.That((res.ThrowOnError().Result as NumberRemoteValue).Value, Is.EqualTo(2)); } [Test] diff --git a/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs b/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs index 7bb43889b4182..c90a62650759f 100644 --- a/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs +++ b/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs @@ -164,6 +164,6 @@ public async Task CanRemovePreloadedScript() var resultAfterRemoval = await context.Script.EvaluateAsync("window.bar", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(resultAfterRemoval.Result, Is.AssignableFrom()); + Assert.That(resultAfterRemoval.ThrowOnError().Result, Is.AssignableFrom()); } } From 63e744e1452ce6e516bee827054da805a2ff5a07 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Thu, 27 Mar 2025 01:23:19 -0400 Subject: [PATCH 02/16] Remote errant import --- dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs index 5eb81cbe0573e..6d437768c00b2 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs @@ -18,7 +18,6 @@ // using OpenQA.Selenium.BiDi.Communication; -using OpenQA.Selenium.DevTools.V132.Page; using System; using System.Threading.Tasks; From f88cc836c3119aad5478582acc5cb0e3acb9fe1b Mon Sep 17 00:00:00 2001 From: Michael Render Date: Thu, 27 Mar 2025 23:48:14 -0400 Subject: [PATCH 03/16] fix whitespace --- .../common/BiDi/Script/CallFunctionLocalValueTest.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs index af8b856a3fd84..e0cc0fc3accf8 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs @@ -79,12 +79,12 @@ public async Task CanCallFunctionWithArgumentFalse() var arg = new BooleanLocalValue(false); var result = await context.Script.CallFunctionAsync($$""" - (arg) => { - if (arg !== false) { - throw new Error("Assert failed: " + arg); + (arg) => { + if (arg !== false) { + throw new Error("Assert failed: " + arg); + } } - } - """, false, new() { Arguments = [arg] }); + """, false, new() { Arguments = [arg] }); Assert.That(result, Is.TypeOf(), $"Call was not successful: {result}"); } From dace75232366337737cb6d6f417287f579c2880e Mon Sep 17 00:00:00 2001 From: Michael Render Date: Thu, 27 Mar 2025 23:48:54 -0400 Subject: [PATCH 04/16] fix whitespace further --- .../test/common/BiDi/Script/CallFunctionLocalValueTest.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs index e0cc0fc3accf8..ae2650426320c 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs @@ -80,9 +80,9 @@ public async Task CanCallFunctionWithArgumentFalse() var result = await context.Script.CallFunctionAsync($$""" (arg) => { - if (arg !== false) { - throw new Error("Assert failed: " + arg); - } + if (arg !== false) { + throw new Error("Assert failed: " + arg); + } } """, false, new() { Arguments = [arg] }); From 1d2323e10941cdb94e179089112892c9d28b811a Mon Sep 17 00:00:00 2001 From: Michael Render Date: Wed, 2 Apr 2025 23:42:16 -0400 Subject: [PATCH 05/16] Rename`EvaluateResult.ThrowOnError` to `EvaluateResult.AsSuccess` --- .../BrowsingContextScriptModule.cs | 4 +- .../BiDi/Modules/Script/EvaluateCommand.cs | 6 +- .../Modules/Script/ScriptEvaluateException.cs | 10 +-- .../BiDi/Modules/Script/ScriptModule.cs | 4 +- .../BiDi/Script/CallFunctionParameterTest.cs | 38 +++++----- .../Script/CallFunctionRemoteValueTest.cs | 76 +++++++++---------- .../BiDi/Script/EvaluateParametersTest.cs | 16 ++-- .../common/BiDi/Script/ScriptCommandsTest.cs | 2 +- 8 files changed, 77 insertions(+), 79 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs index b8accdbfe18eb..5283b6a389248 100644 --- a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs @@ -60,7 +60,7 @@ public Task EvaluateAsync(string expression, bool awaitPromise, { var result = await EvaluateAsync(expression, awaitPromise, options, targetOptions).ConfigureAwait(false); - return result.ThrowOnError().Result.ConvertTo(); + return result.AsSuccess().Result.ConvertTo(); } public Task CallFunctionAsync(string functionDeclaration, bool awaitPromise, CallFunctionOptions? options = null, ContextTargetOptions? targetOptions = null) @@ -79,6 +79,6 @@ public Task CallFunctionAsync(string functionDeclaration, bool a { var result = await CallFunctionAsync(functionDeclaration, awaitPromise, options, targetOptions).ConfigureAwait(false); - return result.ThrowOnError().Result.ConvertTo(); + return result.AsSuccess().Result.ConvertTo(); } } diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs index 7a603549ff0fb..d89cae9aacd92 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs @@ -41,14 +41,16 @@ public record EvaluateOptions : CommandOptions //[JsonDerivedType(typeof(EvaluateResultException), "exception")] public abstract record EvaluateResult { - public EvaluateResultSuccess ThrowOnError() + public EvaluateResultSuccess AsSuccess() { if (this is EvaluateResultSuccess success) { return success; } - throw new ScriptEvaluateException((EvaluateResultException)this); + var exceptionResult = (EvaluateResultException)this; + + throw new ScriptEvaluateException(exceptionResult.ExceptionDetails); } } diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs index 493cf20619e53..701a60290089b 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs @@ -21,13 +21,9 @@ namespace OpenQA.Selenium.BiDi.Modules.Script; -public class ScriptEvaluateException(EvaluateResultException evaluateResultException) : Exception +public class ScriptEvaluateException(ExceptionDetails evaluateResultException) : Exception { - private readonly EvaluateResultException _evaluateResultException = evaluateResultException; + public ExceptionDetails EvaluateResultException { get; } = evaluateResultException; - public string Text => _evaluateResultException.ExceptionDetails.Text; - - public long ColumNumber => _evaluateResultException.ExceptionDetails.ColumnNumber; - - public override string Message => $"{Text}{Environment.NewLine}{string.Join(Environment.NewLine, _evaluateResultException.ExceptionDetails.StackTrace.CallFrames)}"; + public override string Message => $"{EvaluateResultException.Text}{Environment.NewLine}{string.Join(Environment.NewLine, EvaluateResultException.StackTrace.CallFrames)}"; } diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs index 6d437768c00b2..818f2ba8138bf 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs @@ -38,7 +38,7 @@ public async Task EvaluateAsync(string expression, bool awaitPro { var result = await EvaluateAsync(expression, awaitPromise, target, options).ConfigureAwait(false); - return result.ThrowOnError().Result.ConvertTo(); + return result.AsSuccess().Result.ConvertTo(); } public async Task CallFunctionAsync(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null) @@ -54,7 +54,7 @@ public async Task CallFunctionAsync(string functionDeclaration, { var result = await CallFunctionAsync(functionDeclaration, awaitPromise, target, options).ConfigureAwait(false); - return result.ThrowOnError().Result.ConvertTo(); + return result.AsSuccess().Result.ConvertTo(); } public async Task GetRealmsAsync(GetRealmsOptions? options = null) diff --git a/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs index 3f753223d6fc6..bdfb0b2d6e66f 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs @@ -31,8 +31,8 @@ public async Task CanCallFunctionWithDeclaration() var res = await context.Script.CallFunctionAsync("() => { return 1 + 2; }", false); Assert.That(res, Is.Not.Null); - Assert.That(res.ThrowOnError().Realm, Is.Not.Null); - Assert.That((res.ThrowOnError().Result as NumberRemoteValue).Value, Is.EqualTo(3)); + Assert.That(res.AsSuccess().Realm, Is.Not.Null); + Assert.That((res.AsSuccess().Result as NumberRemoteValue).Value, Is.EqualTo(3)); } [Test] @@ -75,9 +75,9 @@ public async Task CanCallFunctionWithArguments() Arguments = ["abc", 42] }); - Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); - Assert.That((string)(res.ThrowOnError().Result as ArrayRemoteValue).Value[0], Is.EqualTo("abc")); - Assert.That((int)(res.ThrowOnError().Result as ArrayRemoteValue).Value[1], Is.EqualTo(42)); + Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); + Assert.That((string)(res.AsSuccess().Result as ArrayRemoteValue).Value[0], Is.EqualTo("abc")); + Assert.That((int)(res.AsSuccess().Result as ArrayRemoteValue).Value[1], Is.EqualTo(42)); } [Test] @@ -90,8 +90,8 @@ public async Task CanCallFunctionToGetIFrameBrowsingContext() """, false); Assert.That(res, Is.Not.Null); - Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); - Assert.That((res.ThrowOnError().Result as WindowProxyRemoteValue).Value, Is.Not.Null); + Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); + Assert.That((res.AsSuccess().Result as WindowProxyRemoteValue).Value, Is.Not.Null); } [Test] @@ -104,8 +104,8 @@ public async Task CanCallFunctionToGetElement() """, false); Assert.That(res, Is.Not.Null); - Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); - Assert.That((res.ThrowOnError().Result as NodeRemoteValue).Value, Is.Not.Null); + Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); + Assert.That((res.AsSuccess().Result as NodeRemoteValue).Value, Is.Not.Null); } [Test] @@ -132,7 +132,7 @@ async function() { """, awaitPromise: false); Assert.That(res, Is.Not.Null); - Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); + Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); } [Test] @@ -156,9 +156,9 @@ public async Task CanCallFunctionWithOwnershipRoot() }); Assert.That(res, Is.Not.Null); - Assert.That((res.ThrowOnError().Result as ObjectRemoteValue).Handle, Is.Not.Null); - Assert.That((string)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.AsSuccess().Result as ObjectRemoteValue).Handle, Is.Not.Null); + Assert.That((string)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] @@ -170,9 +170,9 @@ public async Task CanCallFunctionWithOwnershipNone() }); Assert.That(res, Is.Not.Null); - Assert.That((res.ThrowOnError().Result as ObjectRemoteValue).Handle, Is.Null); - Assert.That((string)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.AsSuccess().Result as ObjectRemoteValue).Handle, Is.Null); + Assert.That((string)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] @@ -192,7 +192,7 @@ public async Task CanCallFunctionInASandBox() var res = await context.Script.CallFunctionAsync("() => window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); + Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); // Make changes in the sandbox await context.Script.CallFunctionAsync("() => { window.foo = 2; }", true, targetOptions: new() { Sandbox = "sandbox" }); @@ -200,8 +200,8 @@ public async Task CanCallFunctionInASandBox() // Check if the changes are present in the sandbox res = await context.Script.CallFunctionAsync("() => window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); - Assert.That((res.ThrowOnError().Result as NumberRemoteValue).Value, Is.EqualTo(2)); + Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); + Assert.That((res.AsSuccess().Result as NumberRemoteValue).Value, Is.EqualTo(2)); } [Test] diff --git a/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs index 225048d74f7d2..2a4dbd94c20e0 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs @@ -30,7 +30,7 @@ public async Task CanCallFunctionAndReturnUndefined() { var response = await context.Script.CallFunctionAsync("() => { return undefined; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); } [Test] @@ -38,7 +38,7 @@ public async Task CanCallFunctionAndReturnNull() { var response = await context.Script.CallFunctionAsync("() => { return null; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); } [Test] @@ -46,8 +46,8 @@ public async Task CanCallFunctionAndReturnTrue() { var response = await context.Script.CallFunctionAsync("() => { return true; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(((BooleanRemoteValue)response.ThrowOnError().Result).Value, Is.True); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(((BooleanRemoteValue)response.AsSuccess().Result).Value, Is.True); } [Test] @@ -55,8 +55,8 @@ public async Task CanCallFunctionAndReturnFalse() { var response = await context.Script.CallFunctionAsync("() => { return false; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(((BooleanRemoteValue)response.ThrowOnError().Result).Value, Is.False); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(((BooleanRemoteValue)response.AsSuccess().Result).Value, Is.False); } @@ -65,8 +65,8 @@ public async Task CanCallFunctionAndReturnEmptyString() { var response = await context.Script.CallFunctionAsync("() => { return ''; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(((StringRemoteValue)response.ThrowOnError().Result).Value, Is.Empty); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(((StringRemoteValue)response.AsSuccess().Result).Value, Is.Empty); } [Test] @@ -74,8 +74,8 @@ public async Task CanCallFunctionAndReturnNonEmptyString() { var response = await context.Script.CallFunctionAsync("() => { return 'whoa'; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(((StringRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo("whoa")); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(((StringRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo("whoa")); } [Test] @@ -85,8 +85,8 @@ public async Task CanCallFunctionAndReturnRecentDate() var response = await context.Script.CallFunctionAsync($$"""() => { return new Date('{{PinnedDateTimeString}}'); }""", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(response.ThrowOnError().Result, Is.EqualTo(new DateRemoteValue(PinnedDateTimeString))); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(response.AsSuccess().Result, Is.EqualTo(new DateRemoteValue(PinnedDateTimeString))); } [Test] @@ -96,8 +96,8 @@ public async Task CanCallFunctionAndReturnUnixEpoch() var response = await context.Script.CallFunctionAsync($$"""() => { return new Date('{{EpochString}}'); }""", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(response.ThrowOnError().Result, Is.EqualTo(new DateRemoteValue(EpochString))); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(response.AsSuccess().Result, Is.EqualTo(new DateRemoteValue(EpochString))); } [Test] @@ -105,8 +105,8 @@ public async Task CanCallFunctionAndReturnNumberFive() { var response = await context.Script.CallFunctionAsync("() => { return 5; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(5)); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(5)); } [Test] @@ -114,8 +114,8 @@ public async Task CanCallFunctionAndReturnNumberNegativeFive() { var response = await context.Script.CallFunctionAsync("() => { return -5; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(-5)); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(-5)); } [Test] @@ -123,8 +123,8 @@ public async Task CanCallFunctionAndReturnNumberZero() { var response = await context.Script.CallFunctionAsync("() => { return 0; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.ThrowOnError().Result).Value, Is.Zero); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.AsSuccess().Result).Value, Is.Zero); } [Test] @@ -132,9 +132,9 @@ public async Task CanCallFunctionAndReturnNumberNegativeZero() { var response = await context.Script.CallFunctionAsync("() => { return -0; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - var actualNumberValue = ((NumberRemoteValue)response.ThrowOnError().Result).Value; + var actualNumberValue = ((NumberRemoteValue)response.AsSuccess().Result).Value; Assert.That(actualNumberValue, Is.Zero); Assert.That(double.IsNegative(actualNumberValue), Is.True); } @@ -144,9 +144,9 @@ public async Task CanCallFunctionAndReturnNumberPositiveInfinity() { var response = await context.Script.CallFunctionAsync("() => { return Number.POSITIVE_INFINITY; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.ThrowOnError().Result).Value; + var expectedInfinity = ((NumberRemoteValue)response.AsSuccess().Result).Value; Assert.That(double.IsPositiveInfinity(expectedInfinity)); } @@ -155,9 +155,9 @@ public async Task CanCallFunctionAndReturnNumberNegativeInfinity() { var response = await context.Script.CallFunctionAsync("() => { return Number.NEGATIVE_INFINITY; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.ThrowOnError().Result).Value; + var expectedInfinity = ((NumberRemoteValue)response.AsSuccess().Result).Value; Assert.That(double.IsNegativeInfinity(expectedInfinity)); } @@ -166,8 +166,8 @@ public async Task CanCallFunctionAndReturnNumberNaN() { var response = await context.Script.CallFunctionAsync("() => { return NaN; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.ThrowOnError().Result).Value; + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + var expectedInfinity = ((NumberRemoteValue)response.AsSuccess().Result).Value; Assert.That(double.IsNaN(expectedInfinity)); } @@ -176,8 +176,8 @@ public async Task CanCallFunctionAndReturnRegExp() { var response = await context.Script.CallFunctionAsync("() => { return /foo*/g; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(response.ThrowOnError().Result, Is.EqualTo(new RegExpRemoteValue(new RegExpValue("foo*") { Flags = "g" }))); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(response.AsSuccess().Result, Is.EqualTo(new RegExpRemoteValue(new RegExpValue("foo*") { Flags = "g" }))); } [Test] @@ -186,8 +186,8 @@ public async Task CanCallFunctionAndReturnArray() var response = await context.Script.CallFunctionAsync("() => { return ['hi']; }", false); var expectedArray = new ArrayRemoteValue { Value = [new StringRemoteValue("hi")] }; - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(((ArrayRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(expectedArray.Value)); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(((ArrayRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(expectedArray.Value)); } [Test] @@ -195,13 +195,13 @@ public async Task CanCallFunctionAndReturnObject() { var response = await context.Script.CallFunctionAsync("() => { return { objKey: 'objValue' }; }", false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); var expected = new ObjectRemoteValue { Value = [[new StringRemoteValue("objKey"), new StringRemoteValue("objValue")]] }; - Assert.That(((ObjectRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(expected.Value)); + Assert.That(((ObjectRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(expected.Value)); } [Test] @@ -220,8 +220,8 @@ public async Task CanCallFunctionAndReturnMap() } """, false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(((MapRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(expected.Value)); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(((MapRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(expected.Value)); } [Test] @@ -236,7 +236,7 @@ public async Task CanCallFunctionAndReturnSet() } """, false); - Assert.That(response.ThrowOnError().Result, Is.AssignableTo()); - Assert.That(((SetRemoteValue)response.ThrowOnError().Result).Value, Is.EqualTo(expected.Value)); + Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(((SetRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(expected.Value)); } } diff --git a/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs b/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs index dd7de6c480cf8..5ea05516cc5c0 100644 --- a/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs +++ b/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs @@ -31,8 +31,8 @@ public async Task CanEvaluateScript() var res = await context.Script.EvaluateAsync("1 + 2", false); Assert.That(res, Is.Not.Null); - Assert.That(res.ThrowOnError().Realm, Is.Not.Null); - Assert.That((res.ThrowOnError().Result as NumberRemoteValue).Value, Is.EqualTo(3)); + Assert.That(res.AsSuccess().Realm, Is.Not.Null); + Assert.That((res.AsSuccess().Result as NumberRemoteValue).Value, Is.EqualTo(3)); } [Test] @@ -85,9 +85,9 @@ public async Task CanEvaluateScriptWithResulWithOwnership() }); Assert.That(res, Is.Not.Null); - Assert.That((res.ThrowOnError().Result as ObjectRemoteValue).Handle, Is.Not.Null); - Assert.That((string)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.ThrowOnError().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.AsSuccess().Result as ObjectRemoteValue).Handle, Is.Not.Null); + Assert.That((string)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] @@ -98,7 +98,7 @@ public async Task CanEvaluateInASandBox() var res = await context.Script.EvaluateAsync("window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); + Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); // Make changes in the sandbox await context.Script.EvaluateAsync("window.foo = 2", true, targetOptions: new() { Sandbox = "sandbox" }); @@ -106,8 +106,8 @@ public async Task CanEvaluateInASandBox() // Check if the changes are present in the sandbox res = await context.Script.EvaluateAsync("window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.ThrowOnError().Result, Is.AssignableFrom()); - Assert.That((res.ThrowOnError().Result as NumberRemoteValue).Value, Is.EqualTo(2)); + Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); + Assert.That((res.AsSuccess().Result as NumberRemoteValue).Value, Is.EqualTo(2)); } [Test] diff --git a/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs b/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs index c90a62650759f..d9d2c2cc79e73 100644 --- a/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs +++ b/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs @@ -164,6 +164,6 @@ public async Task CanRemovePreloadedScript() var resultAfterRemoval = await context.Script.EvaluateAsync("window.bar", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(resultAfterRemoval.ThrowOnError().Result, Is.AssignableFrom()); + Assert.That(resultAfterRemoval.AsSuccess().Result, Is.AssignableFrom()); } } From 1684694327ad98548be19a07cb6efb9a061fbb80 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Wed, 2 Apr 2025 23:45:30 -0400 Subject: [PATCH 06/16] Rename `AsSuccess()` to `AsSuccessResult()` --- .../BrowsingContextScriptModule.cs | 4 +- .../BiDi/Modules/Script/EvaluateCommand.cs | 2 +- .../BiDi/Modules/Script/ScriptModule.cs | 4 +- .../BiDi/Script/CallFunctionParameterTest.cs | 38 +++++----- .../Script/CallFunctionRemoteValueTest.cs | 76 +++++++++---------- .../BiDi/Script/EvaluateParametersTest.cs | 16 ++-- .../common/BiDi/Script/ScriptCommandsTest.cs | 2 +- 7 files changed, 71 insertions(+), 71 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs index 5283b6a389248..34d18e17ee297 100644 --- a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs @@ -60,7 +60,7 @@ public Task EvaluateAsync(string expression, bool awaitPromise, { var result = await EvaluateAsync(expression, awaitPromise, options, targetOptions).ConfigureAwait(false); - return result.AsSuccess().Result.ConvertTo(); + return result.AsSuccessResult().Result.ConvertTo(); } public Task CallFunctionAsync(string functionDeclaration, bool awaitPromise, CallFunctionOptions? options = null, ContextTargetOptions? targetOptions = null) @@ -79,6 +79,6 @@ public Task CallFunctionAsync(string functionDeclaration, bool a { var result = await CallFunctionAsync(functionDeclaration, awaitPromise, options, targetOptions).ConfigureAwait(false); - return result.AsSuccess().Result.ConvertTo(); + return result.AsSuccessResult().Result.ConvertTo(); } } diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs index d89cae9aacd92..0e21096b5e8f0 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs @@ -41,7 +41,7 @@ public record EvaluateOptions : CommandOptions //[JsonDerivedType(typeof(EvaluateResultException), "exception")] public abstract record EvaluateResult { - public EvaluateResultSuccess AsSuccess() + public EvaluateResultSuccess AsSuccessResult() { if (this is EvaluateResultSuccess success) { diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs index 818f2ba8138bf..51037d14b2ccb 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs @@ -38,7 +38,7 @@ public async Task EvaluateAsync(string expression, bool awaitPro { var result = await EvaluateAsync(expression, awaitPromise, target, options).ConfigureAwait(false); - return result.AsSuccess().Result.ConvertTo(); + return result.AsSuccessResult().Result.ConvertTo(); } public async Task CallFunctionAsync(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null) @@ -54,7 +54,7 @@ public async Task CallFunctionAsync(string functionDeclaration, { var result = await CallFunctionAsync(functionDeclaration, awaitPromise, target, options).ConfigureAwait(false); - return result.AsSuccess().Result.ConvertTo(); + return result.AsSuccessResult().Result.ConvertTo(); } public async Task GetRealmsAsync(GetRealmsOptions? options = null) diff --git a/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs index bdfb0b2d6e66f..03b750c5bcdb5 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs @@ -31,8 +31,8 @@ public async Task CanCallFunctionWithDeclaration() var res = await context.Script.CallFunctionAsync("() => { return 1 + 2; }", false); Assert.That(res, Is.Not.Null); - Assert.That(res.AsSuccess().Realm, Is.Not.Null); - Assert.That((res.AsSuccess().Result as NumberRemoteValue).Value, Is.EqualTo(3)); + Assert.That(res.AsSuccessResult().Realm, Is.Not.Null); + Assert.That((res.AsSuccessResult().Result as NumberRemoteValue).Value, Is.EqualTo(3)); } [Test] @@ -75,9 +75,9 @@ public async Task CanCallFunctionWithArguments() Arguments = ["abc", 42] }); - Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); - Assert.That((string)(res.AsSuccess().Result as ArrayRemoteValue).Value[0], Is.EqualTo("abc")); - Assert.That((int)(res.AsSuccess().Result as ArrayRemoteValue).Value[1], Is.EqualTo(42)); + Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); + Assert.That((string)(res.AsSuccessResult().Result as ArrayRemoteValue).Value[0], Is.EqualTo("abc")); + Assert.That((int)(res.AsSuccessResult().Result as ArrayRemoteValue).Value[1], Is.EqualTo(42)); } [Test] @@ -90,8 +90,8 @@ public async Task CanCallFunctionToGetIFrameBrowsingContext() """, false); Assert.That(res, Is.Not.Null); - Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); - Assert.That((res.AsSuccess().Result as WindowProxyRemoteValue).Value, Is.Not.Null); + Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); + Assert.That((res.AsSuccessResult().Result as WindowProxyRemoteValue).Value, Is.Not.Null); } [Test] @@ -104,8 +104,8 @@ public async Task CanCallFunctionToGetElement() """, false); Assert.That(res, Is.Not.Null); - Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); - Assert.That((res.AsSuccess().Result as NodeRemoteValue).Value, Is.Not.Null); + Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); + Assert.That((res.AsSuccessResult().Result as NodeRemoteValue).Value, Is.Not.Null); } [Test] @@ -132,7 +132,7 @@ async function() { """, awaitPromise: false); Assert.That(res, Is.Not.Null); - Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); + Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); } [Test] @@ -156,9 +156,9 @@ public async Task CanCallFunctionWithOwnershipRoot() }); Assert.That(res, Is.Not.Null); - Assert.That((res.AsSuccess().Result as ObjectRemoteValue).Handle, Is.Not.Null); - Assert.That((string)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.AsSuccessResult().Result as ObjectRemoteValue).Handle, Is.Not.Null); + Assert.That((string)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] @@ -170,9 +170,9 @@ public async Task CanCallFunctionWithOwnershipNone() }); Assert.That(res, Is.Not.Null); - Assert.That((res.AsSuccess().Result as ObjectRemoteValue).Handle, Is.Null); - Assert.That((string)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.AsSuccessResult().Result as ObjectRemoteValue).Handle, Is.Null); + Assert.That((string)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] @@ -192,7 +192,7 @@ public async Task CanCallFunctionInASandBox() var res = await context.Script.CallFunctionAsync("() => window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); + Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); // Make changes in the sandbox await context.Script.CallFunctionAsync("() => { window.foo = 2; }", true, targetOptions: new() { Sandbox = "sandbox" }); @@ -200,8 +200,8 @@ public async Task CanCallFunctionInASandBox() // Check if the changes are present in the sandbox res = await context.Script.CallFunctionAsync("() => window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); - Assert.That((res.AsSuccess().Result as NumberRemoteValue).Value, Is.EqualTo(2)); + Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); + Assert.That((res.AsSuccessResult().Result as NumberRemoteValue).Value, Is.EqualTo(2)); } [Test] diff --git a/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs index 2a4dbd94c20e0..b1f8800a60c48 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs @@ -30,7 +30,7 @@ public async Task CanCallFunctionAndReturnUndefined() { var response = await context.Script.CallFunctionAsync("() => { return undefined; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); } [Test] @@ -38,7 +38,7 @@ public async Task CanCallFunctionAndReturnNull() { var response = await context.Script.CallFunctionAsync("() => { return null; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); } [Test] @@ -46,8 +46,8 @@ public async Task CanCallFunctionAndReturnTrue() { var response = await context.Script.CallFunctionAsync("() => { return true; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(((BooleanRemoteValue)response.AsSuccess().Result).Value, Is.True); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(((BooleanRemoteValue)response.AsSuccessResult().Result).Value, Is.True); } [Test] @@ -55,8 +55,8 @@ public async Task CanCallFunctionAndReturnFalse() { var response = await context.Script.CallFunctionAsync("() => { return false; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(((BooleanRemoteValue)response.AsSuccess().Result).Value, Is.False); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(((BooleanRemoteValue)response.AsSuccessResult().Result).Value, Is.False); } @@ -65,8 +65,8 @@ public async Task CanCallFunctionAndReturnEmptyString() { var response = await context.Script.CallFunctionAsync("() => { return ''; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(((StringRemoteValue)response.AsSuccess().Result).Value, Is.Empty); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(((StringRemoteValue)response.AsSuccessResult().Result).Value, Is.Empty); } [Test] @@ -74,8 +74,8 @@ public async Task CanCallFunctionAndReturnNonEmptyString() { var response = await context.Script.CallFunctionAsync("() => { return 'whoa'; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(((StringRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo("whoa")); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(((StringRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo("whoa")); } [Test] @@ -85,8 +85,8 @@ public async Task CanCallFunctionAndReturnRecentDate() var response = await context.Script.CallFunctionAsync($$"""() => { return new Date('{{PinnedDateTimeString}}'); }""", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(response.AsSuccess().Result, Is.EqualTo(new DateRemoteValue(PinnedDateTimeString))); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult().Result, Is.EqualTo(new DateRemoteValue(PinnedDateTimeString))); } [Test] @@ -96,8 +96,8 @@ public async Task CanCallFunctionAndReturnUnixEpoch() var response = await context.Script.CallFunctionAsync($$"""() => { return new Date('{{EpochString}}'); }""", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(response.AsSuccess().Result, Is.EqualTo(new DateRemoteValue(EpochString))); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult().Result, Is.EqualTo(new DateRemoteValue(EpochString))); } [Test] @@ -105,8 +105,8 @@ public async Task CanCallFunctionAndReturnNumberFive() { var response = await context.Script.CallFunctionAsync("() => { return 5; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(5)); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(5)); } [Test] @@ -114,8 +114,8 @@ public async Task CanCallFunctionAndReturnNumberNegativeFive() { var response = await context.Script.CallFunctionAsync("() => { return -5; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(-5)); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(-5)); } [Test] @@ -123,8 +123,8 @@ public async Task CanCallFunctionAndReturnNumberZero() { var response = await context.Script.CallFunctionAsync("() => { return 0; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.AsSuccess().Result).Value, Is.Zero); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.AsSuccessResult().Result).Value, Is.Zero); } [Test] @@ -132,9 +132,9 @@ public async Task CanCallFunctionAndReturnNumberNegativeZero() { var response = await context.Script.CallFunctionAsync("() => { return -0; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - var actualNumberValue = ((NumberRemoteValue)response.AsSuccess().Result).Value; + var actualNumberValue = ((NumberRemoteValue)response.AsSuccessResult().Result).Value; Assert.That(actualNumberValue, Is.Zero); Assert.That(double.IsNegative(actualNumberValue), Is.True); } @@ -144,9 +144,9 @@ public async Task CanCallFunctionAndReturnNumberPositiveInfinity() { var response = await context.Script.CallFunctionAsync("() => { return Number.POSITIVE_INFINITY; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.AsSuccess().Result).Value; + var expectedInfinity = ((NumberRemoteValue)response.AsSuccessResult().Result).Value; Assert.That(double.IsPositiveInfinity(expectedInfinity)); } @@ -155,9 +155,9 @@ public async Task CanCallFunctionAndReturnNumberNegativeInfinity() { var response = await context.Script.CallFunctionAsync("() => { return Number.NEGATIVE_INFINITY; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.AsSuccess().Result).Value; + var expectedInfinity = ((NumberRemoteValue)response.AsSuccessResult().Result).Value; Assert.That(double.IsNegativeInfinity(expectedInfinity)); } @@ -166,8 +166,8 @@ public async Task CanCallFunctionAndReturnNumberNaN() { var response = await context.Script.CallFunctionAsync("() => { return NaN; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.AsSuccess().Result).Value; + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + var expectedInfinity = ((NumberRemoteValue)response.AsSuccessResult().Result).Value; Assert.That(double.IsNaN(expectedInfinity)); } @@ -176,8 +176,8 @@ public async Task CanCallFunctionAndReturnRegExp() { var response = await context.Script.CallFunctionAsync("() => { return /foo*/g; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(response.AsSuccess().Result, Is.EqualTo(new RegExpRemoteValue(new RegExpValue("foo*") { Flags = "g" }))); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult().Result, Is.EqualTo(new RegExpRemoteValue(new RegExpValue("foo*") { Flags = "g" }))); } [Test] @@ -186,8 +186,8 @@ public async Task CanCallFunctionAndReturnArray() var response = await context.Script.CallFunctionAsync("() => { return ['hi']; }", false); var expectedArray = new ArrayRemoteValue { Value = [new StringRemoteValue("hi")] }; - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(((ArrayRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(expectedArray.Value)); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(((ArrayRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(expectedArray.Value)); } [Test] @@ -195,13 +195,13 @@ public async Task CanCallFunctionAndReturnObject() { var response = await context.Script.CallFunctionAsync("() => { return { objKey: 'objValue' }; }", false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); var expected = new ObjectRemoteValue { Value = [[new StringRemoteValue("objKey"), new StringRemoteValue("objValue")]] }; - Assert.That(((ObjectRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(expected.Value)); + Assert.That(((ObjectRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(expected.Value)); } [Test] @@ -220,8 +220,8 @@ public async Task CanCallFunctionAndReturnMap() } """, false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(((MapRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(expected.Value)); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(((MapRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(expected.Value)); } [Test] @@ -236,7 +236,7 @@ public async Task CanCallFunctionAndReturnSet() } """, false); - Assert.That(response.AsSuccess().Result, Is.AssignableTo()); - Assert.That(((SetRemoteValue)response.AsSuccess().Result).Value, Is.EqualTo(expected.Value)); + Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(((SetRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(expected.Value)); } } diff --git a/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs b/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs index 5ea05516cc5c0..889736ad67ee9 100644 --- a/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs +++ b/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs @@ -31,8 +31,8 @@ public async Task CanEvaluateScript() var res = await context.Script.EvaluateAsync("1 + 2", false); Assert.That(res, Is.Not.Null); - Assert.That(res.AsSuccess().Realm, Is.Not.Null); - Assert.That((res.AsSuccess().Result as NumberRemoteValue).Value, Is.EqualTo(3)); + Assert.That(res.AsSuccessResult().Realm, Is.Not.Null); + Assert.That((res.AsSuccessResult().Result as NumberRemoteValue).Value, Is.EqualTo(3)); } [Test] @@ -85,9 +85,9 @@ public async Task CanEvaluateScriptWithResulWithOwnership() }); Assert.That(res, Is.Not.Null); - Assert.That((res.AsSuccess().Result as ObjectRemoteValue).Handle, Is.Not.Null); - Assert.That((string)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.AsSuccess().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.AsSuccessResult().Result as ObjectRemoteValue).Handle, Is.Not.Null); + Assert.That((string)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] @@ -98,7 +98,7 @@ public async Task CanEvaluateInASandBox() var res = await context.Script.EvaluateAsync("window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); + Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); // Make changes in the sandbox await context.Script.EvaluateAsync("window.foo = 2", true, targetOptions: new() { Sandbox = "sandbox" }); @@ -106,8 +106,8 @@ public async Task CanEvaluateInASandBox() // Check if the changes are present in the sandbox res = await context.Script.EvaluateAsync("window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.AsSuccess().Result, Is.AssignableFrom()); - Assert.That((res.AsSuccess().Result as NumberRemoteValue).Value, Is.EqualTo(2)); + Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); + Assert.That((res.AsSuccessResult().Result as NumberRemoteValue).Value, Is.EqualTo(2)); } [Test] diff --git a/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs b/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs index d9d2c2cc79e73..4ccae5f0d370e 100644 --- a/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs +++ b/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs @@ -164,6 +164,6 @@ public async Task CanRemovePreloadedScript() var resultAfterRemoval = await context.Script.EvaluateAsync("window.bar", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(resultAfterRemoval.AsSuccess().Result, Is.AssignableFrom()); + Assert.That(resultAfterRemoval.AsSuccessResult().Result, Is.AssignableFrom()); } } From 11d4c7d01baa9c632579fd33ba28e9cd1c70a783 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Wed, 2 Apr 2025 23:53:17 -0400 Subject: [PATCH 07/16] Change `AsSuccessResult()` to return the `RemoteValue` directly. --- .../BrowsingContextScriptModule.cs | 4 +- .../BiDi/Modules/Script/EvaluateCommand.cs | 4 +- .../BiDi/Modules/Script/ScriptModule.cs | 4 +- .../BiDi/Script/CallFunctionParameterTest.cs | 38 +++++----- .../Script/CallFunctionRemoteValueTest.cs | 76 +++++++++---------- .../BiDi/Script/EvaluateParametersTest.cs | 16 ++-- .../common/BiDi/Script/ScriptCommandsTest.cs | 2 +- 7 files changed, 72 insertions(+), 72 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs index 34d18e17ee297..53b319042fd4e 100644 --- a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs @@ -60,7 +60,7 @@ public Task EvaluateAsync(string expression, bool awaitPromise, { var result = await EvaluateAsync(expression, awaitPromise, options, targetOptions).ConfigureAwait(false); - return result.AsSuccessResult().Result.ConvertTo(); + return result.AsSuccessResult().ConvertTo(); } public Task CallFunctionAsync(string functionDeclaration, bool awaitPromise, CallFunctionOptions? options = null, ContextTargetOptions? targetOptions = null) @@ -79,6 +79,6 @@ public Task CallFunctionAsync(string functionDeclaration, bool a { var result = await CallFunctionAsync(functionDeclaration, awaitPromise, options, targetOptions).ConfigureAwait(false); - return result.AsSuccessResult().Result.ConvertTo(); + return result.AsSuccessResult().ConvertTo(); } } diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs index 0e21096b5e8f0..bda3ec00ad376 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs @@ -41,11 +41,11 @@ public record EvaluateOptions : CommandOptions //[JsonDerivedType(typeof(EvaluateResultException), "exception")] public abstract record EvaluateResult { - public EvaluateResultSuccess AsSuccessResult() + public RemoteValue AsSuccessResult() { if (this is EvaluateResultSuccess success) { - return success; + return success.Result; } var exceptionResult = (EvaluateResultException)this; diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs index 51037d14b2ccb..f79314959b98b 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs @@ -38,7 +38,7 @@ public async Task EvaluateAsync(string expression, bool awaitPro { var result = await EvaluateAsync(expression, awaitPromise, target, options).ConfigureAwait(false); - return result.AsSuccessResult().Result.ConvertTo(); + return result.AsSuccessResult().ConvertTo(); } public async Task CallFunctionAsync(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null) @@ -54,7 +54,7 @@ public async Task CallFunctionAsync(string functionDeclaration, { var result = await CallFunctionAsync(functionDeclaration, awaitPromise, target, options).ConfigureAwait(false); - return result.AsSuccessResult().Result.ConvertTo(); + return result.AsSuccessResult().ConvertTo(); } public async Task GetRealmsAsync(GetRealmsOptions? options = null) diff --git a/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs index 03b750c5bcdb5..d6969e89ba962 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionParameterTest.cs @@ -31,8 +31,8 @@ public async Task CanCallFunctionWithDeclaration() var res = await context.Script.CallFunctionAsync("() => { return 1 + 2; }", false); Assert.That(res, Is.Not.Null); - Assert.That(res.AsSuccessResult().Realm, Is.Not.Null); - Assert.That((res.AsSuccessResult().Result as NumberRemoteValue).Value, Is.EqualTo(3)); + Assert.That(((EvaluateResultSuccess)res).Realm, Is.Not.Null); + Assert.That((res.AsSuccessResult() as NumberRemoteValue).Value, Is.EqualTo(3)); } [Test] @@ -75,9 +75,9 @@ public async Task CanCallFunctionWithArguments() Arguments = ["abc", 42] }); - Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); - Assert.That((string)(res.AsSuccessResult().Result as ArrayRemoteValue).Value[0], Is.EqualTo("abc")); - Assert.That((int)(res.AsSuccessResult().Result as ArrayRemoteValue).Value[1], Is.EqualTo(42)); + Assert.That(res.AsSuccessResult(), Is.AssignableFrom()); + Assert.That((string)(res.AsSuccessResult() as ArrayRemoteValue).Value[0], Is.EqualTo("abc")); + Assert.That((int)(res.AsSuccessResult() as ArrayRemoteValue).Value[1], Is.EqualTo(42)); } [Test] @@ -90,8 +90,8 @@ public async Task CanCallFunctionToGetIFrameBrowsingContext() """, false); Assert.That(res, Is.Not.Null); - Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); - Assert.That((res.AsSuccessResult().Result as WindowProxyRemoteValue).Value, Is.Not.Null); + Assert.That(res.AsSuccessResult(), Is.AssignableFrom()); + Assert.That((res.AsSuccessResult() as WindowProxyRemoteValue).Value, Is.Not.Null); } [Test] @@ -104,8 +104,8 @@ public async Task CanCallFunctionToGetElement() """, false); Assert.That(res, Is.Not.Null); - Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); - Assert.That((res.AsSuccessResult().Result as NodeRemoteValue).Value, Is.Not.Null); + Assert.That(res.AsSuccessResult(), Is.AssignableFrom()); + Assert.That((res.AsSuccessResult() as NodeRemoteValue).Value, Is.Not.Null); } [Test] @@ -132,7 +132,7 @@ async function() { """, awaitPromise: false); Assert.That(res, Is.Not.Null); - Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); + Assert.That(res.AsSuccessResult(), Is.AssignableFrom()); } [Test] @@ -156,9 +156,9 @@ public async Task CanCallFunctionWithOwnershipRoot() }); Assert.That(res, Is.Not.Null); - Assert.That((res.AsSuccessResult().Result as ObjectRemoteValue).Handle, Is.Not.Null); - Assert.That((string)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.AsSuccessResult() as ObjectRemoteValue).Handle, Is.Not.Null); + Assert.That((string)(res.AsSuccessResult() as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.AsSuccessResult() as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] @@ -170,9 +170,9 @@ public async Task CanCallFunctionWithOwnershipNone() }); Assert.That(res, Is.Not.Null); - Assert.That((res.AsSuccessResult().Result as ObjectRemoteValue).Handle, Is.Null); - Assert.That((string)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.AsSuccessResult() as ObjectRemoteValue).Handle, Is.Null); + Assert.That((string)(res.AsSuccessResult() as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.AsSuccessResult() as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] @@ -192,7 +192,7 @@ public async Task CanCallFunctionInASandBox() var res = await context.Script.CallFunctionAsync("() => window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); + Assert.That(res.AsSuccessResult(), Is.AssignableFrom()); // Make changes in the sandbox await context.Script.CallFunctionAsync("() => { window.foo = 2; }", true, targetOptions: new() { Sandbox = "sandbox" }); @@ -200,8 +200,8 @@ public async Task CanCallFunctionInASandBox() // Check if the changes are present in the sandbox res = await context.Script.CallFunctionAsync("() => window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); - Assert.That((res.AsSuccessResult().Result as NumberRemoteValue).Value, Is.EqualTo(2)); + Assert.That(res.AsSuccessResult(), Is.AssignableFrom()); + Assert.That((res.AsSuccessResult() as NumberRemoteValue).Value, Is.EqualTo(2)); } [Test] diff --git a/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs index b1f8800a60c48..96b638e5a0450 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionRemoteValueTest.cs @@ -30,7 +30,7 @@ public async Task CanCallFunctionAndReturnUndefined() { var response = await context.Script.CallFunctionAsync("() => { return undefined; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); } [Test] @@ -38,7 +38,7 @@ public async Task CanCallFunctionAndReturnNull() { var response = await context.Script.CallFunctionAsync("() => { return null; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); } [Test] @@ -46,8 +46,8 @@ public async Task CanCallFunctionAndReturnTrue() { var response = await context.Script.CallFunctionAsync("() => { return true; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(((BooleanRemoteValue)response.AsSuccessResult().Result).Value, Is.True); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(((BooleanRemoteValue)response.AsSuccessResult()).Value, Is.True); } [Test] @@ -55,8 +55,8 @@ public async Task CanCallFunctionAndReturnFalse() { var response = await context.Script.CallFunctionAsync("() => { return false; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(((BooleanRemoteValue)response.AsSuccessResult().Result).Value, Is.False); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(((BooleanRemoteValue)response.AsSuccessResult()).Value, Is.False); } @@ -65,8 +65,8 @@ public async Task CanCallFunctionAndReturnEmptyString() { var response = await context.Script.CallFunctionAsync("() => { return ''; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(((StringRemoteValue)response.AsSuccessResult().Result).Value, Is.Empty); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(((StringRemoteValue)response.AsSuccessResult()).Value, Is.Empty); } [Test] @@ -74,8 +74,8 @@ public async Task CanCallFunctionAndReturnNonEmptyString() { var response = await context.Script.CallFunctionAsync("() => { return 'whoa'; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(((StringRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo("whoa")); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(((StringRemoteValue)response.AsSuccessResult()).Value, Is.EqualTo("whoa")); } [Test] @@ -85,8 +85,8 @@ public async Task CanCallFunctionAndReturnRecentDate() var response = await context.Script.CallFunctionAsync($$"""() => { return new Date('{{PinnedDateTimeString}}'); }""", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(response.AsSuccessResult().Result, Is.EqualTo(new DateRemoteValue(PinnedDateTimeString))); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.EqualTo(new DateRemoteValue(PinnedDateTimeString))); } [Test] @@ -96,8 +96,8 @@ public async Task CanCallFunctionAndReturnUnixEpoch() var response = await context.Script.CallFunctionAsync($$"""() => { return new Date('{{EpochString}}'); }""", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(response.AsSuccessResult().Result, Is.EqualTo(new DateRemoteValue(EpochString))); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.EqualTo(new DateRemoteValue(EpochString))); } [Test] @@ -105,8 +105,8 @@ public async Task CanCallFunctionAndReturnNumberFive() { var response = await context.Script.CallFunctionAsync("() => { return 5; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(5)); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.AsSuccessResult()).Value, Is.EqualTo(5)); } [Test] @@ -114,8 +114,8 @@ public async Task CanCallFunctionAndReturnNumberNegativeFive() { var response = await context.Script.CallFunctionAsync("() => { return -5; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(-5)); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.AsSuccessResult()).Value, Is.EqualTo(-5)); } [Test] @@ -123,8 +123,8 @@ public async Task CanCallFunctionAndReturnNumberZero() { var response = await context.Script.CallFunctionAsync("() => { return 0; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.AsSuccessResult().Result).Value, Is.Zero); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(((NumberRemoteValue)response.AsSuccessResult()).Value, Is.Zero); } [Test] @@ -132,9 +132,9 @@ public async Task CanCallFunctionAndReturnNumberNegativeZero() { var response = await context.Script.CallFunctionAsync("() => { return -0; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); - var actualNumberValue = ((NumberRemoteValue)response.AsSuccessResult().Result).Value; + var actualNumberValue = ((NumberRemoteValue)response.AsSuccessResult()).Value; Assert.That(actualNumberValue, Is.Zero); Assert.That(double.IsNegative(actualNumberValue), Is.True); } @@ -144,9 +144,9 @@ public async Task CanCallFunctionAndReturnNumberPositiveInfinity() { var response = await context.Script.CallFunctionAsync("() => { return Number.POSITIVE_INFINITY; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.AsSuccessResult().Result).Value; + var expectedInfinity = ((NumberRemoteValue)response.AsSuccessResult()).Value; Assert.That(double.IsPositiveInfinity(expectedInfinity)); } @@ -155,9 +155,9 @@ public async Task CanCallFunctionAndReturnNumberNegativeInfinity() { var response = await context.Script.CallFunctionAsync("() => { return Number.NEGATIVE_INFINITY; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.AsSuccessResult().Result).Value; + var expectedInfinity = ((NumberRemoteValue)response.AsSuccessResult()).Value; Assert.That(double.IsNegativeInfinity(expectedInfinity)); } @@ -166,8 +166,8 @@ public async Task CanCallFunctionAndReturnNumberNaN() { var response = await context.Script.CallFunctionAsync("() => { return NaN; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.AsSuccessResult().Result).Value; + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + var expectedInfinity = ((NumberRemoteValue)response.AsSuccessResult()).Value; Assert.That(double.IsNaN(expectedInfinity)); } @@ -176,8 +176,8 @@ public async Task CanCallFunctionAndReturnRegExp() { var response = await context.Script.CallFunctionAsync("() => { return /foo*/g; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(response.AsSuccessResult().Result, Is.EqualTo(new RegExpRemoteValue(new RegExpValue("foo*") { Flags = "g" }))); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.EqualTo(new RegExpRemoteValue(new RegExpValue("foo*") { Flags = "g" }))); } [Test] @@ -186,8 +186,8 @@ public async Task CanCallFunctionAndReturnArray() var response = await context.Script.CallFunctionAsync("() => { return ['hi']; }", false); var expectedArray = new ArrayRemoteValue { Value = [new StringRemoteValue("hi")] }; - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(((ArrayRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(expectedArray.Value)); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(((ArrayRemoteValue)response.AsSuccessResult()).Value, Is.EqualTo(expectedArray.Value)); } [Test] @@ -195,13 +195,13 @@ public async Task CanCallFunctionAndReturnObject() { var response = await context.Script.CallFunctionAsync("() => { return { objKey: 'objValue' }; }", false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); var expected = new ObjectRemoteValue { Value = [[new StringRemoteValue("objKey"), new StringRemoteValue("objValue")]] }; - Assert.That(((ObjectRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(expected.Value)); + Assert.That(((ObjectRemoteValue)response.AsSuccessResult()).Value, Is.EqualTo(expected.Value)); } [Test] @@ -220,8 +220,8 @@ public async Task CanCallFunctionAndReturnMap() } """, false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(((MapRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(expected.Value)); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(((MapRemoteValue)response.AsSuccessResult()).Value, Is.EqualTo(expected.Value)); } [Test] @@ -236,7 +236,7 @@ public async Task CanCallFunctionAndReturnSet() } """, false); - Assert.That(response.AsSuccessResult().Result, Is.AssignableTo()); - Assert.That(((SetRemoteValue)response.AsSuccessResult().Result).Value, Is.EqualTo(expected.Value)); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); + Assert.That(((SetRemoteValue)response.AsSuccessResult()).Value, Is.EqualTo(expected.Value)); } } diff --git a/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs b/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs index 889736ad67ee9..967037d9a9b06 100644 --- a/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs +++ b/dotnet/test/common/BiDi/Script/EvaluateParametersTest.cs @@ -31,8 +31,8 @@ public async Task CanEvaluateScript() var res = await context.Script.EvaluateAsync("1 + 2", false); Assert.That(res, Is.Not.Null); - Assert.That(res.AsSuccessResult().Realm, Is.Not.Null); - Assert.That((res.AsSuccessResult().Result as NumberRemoteValue).Value, Is.EqualTo(3)); + Assert.That(((EvaluateResultSuccess)res).Realm, Is.Not.Null); + Assert.That((res.AsSuccessResult() as NumberRemoteValue).Value, Is.EqualTo(3)); } [Test] @@ -85,9 +85,9 @@ public async Task CanEvaluateScriptWithResulWithOwnership() }); Assert.That(res, Is.Not.Null); - Assert.That((res.AsSuccessResult().Result as ObjectRemoteValue).Handle, Is.Not.Null); - Assert.That((string)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); - Assert.That((int)(res.AsSuccessResult().Result as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); + Assert.That((res.AsSuccessResult() as ObjectRemoteValue).Handle, Is.Not.Null); + Assert.That((string)(res.AsSuccessResult() as ObjectRemoteValue).Value[0][0], Is.EqualTo("a")); + Assert.That((int)(res.AsSuccessResult() as ObjectRemoteValue).Value[0][1], Is.EqualTo(1)); } [Test] @@ -98,7 +98,7 @@ public async Task CanEvaluateInASandBox() var res = await context.Script.EvaluateAsync("window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); + Assert.That(res.AsSuccessResult(), Is.AssignableFrom()); // Make changes in the sandbox await context.Script.EvaluateAsync("window.foo = 2", true, targetOptions: new() { Sandbox = "sandbox" }); @@ -106,8 +106,8 @@ public async Task CanEvaluateInASandBox() // Check if the changes are present in the sandbox res = await context.Script.EvaluateAsync("window.foo", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(res.AsSuccessResult().Result, Is.AssignableFrom()); - Assert.That((res.AsSuccessResult().Result as NumberRemoteValue).Value, Is.EqualTo(2)); + Assert.That(res.AsSuccessResult(), Is.AssignableFrom()); + Assert.That((res.AsSuccessResult() as NumberRemoteValue).Value, Is.EqualTo(2)); } [Test] diff --git a/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs b/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs index 4ccae5f0d370e..6bb4d0cb78167 100644 --- a/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs +++ b/dotnet/test/common/BiDi/Script/ScriptCommandsTest.cs @@ -164,6 +164,6 @@ public async Task CanRemovePreloadedScript() var resultAfterRemoval = await context.Script.EvaluateAsync("window.bar", true, targetOptions: new() { Sandbox = "sandbox" }); - Assert.That(resultAfterRemoval.AsSuccessResult().Result, Is.AssignableFrom()); + Assert.That(resultAfterRemoval.AsSuccessResult(), Is.AssignableFrom()); } } From 23e7e8e0e05705999a0b043d2f6155c59d00c6da Mon Sep 17 00:00:00 2001 From: Michael Render Date: Wed, 2 Apr 2025 23:57:45 -0400 Subject: [PATCH 08/16] Rename ScriptEvaluateException.ExceptionDetails --- .../webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs index 701a60290089b..458a860da0db2 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs @@ -23,7 +23,7 @@ namespace OpenQA.Selenium.BiDi.Modules.Script; public class ScriptEvaluateException(ExceptionDetails evaluateResultException) : Exception { - public ExceptionDetails EvaluateResultException { get; } = evaluateResultException; + public ExceptionDetails ExceptionDetails { get; } = evaluateResultException; - public override string Message => $"{EvaluateResultException.Text}{Environment.NewLine}{string.Join(Environment.NewLine, EvaluateResultException.StackTrace.CallFrames)}"; + public override string Message => $"{ExceptionDetails.Text}{Environment.NewLine}{string.Join(Environment.NewLine, ExceptionDetails.StackTrace.CallFrames)}"; } From 68e11ca165ce4c715ac5a6265953abb3ab9d93f5 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Tue, 8 Apr 2025 01:35:11 -0400 Subject: [PATCH 09/16] Remove unnecessary attribute --- dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs b/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs index 79bfadd910deb..3bf3668dfb377 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs @@ -162,7 +162,6 @@ public record ChannelLocalValue(ChannelProperties Value) : LocalValue { // AddPreloadScript takes arguments typed as ChannelLocalValue but still requires "type":"channel" [JsonInclude] - [JsonPropertyName("type")] internal string Type => "channel"; } From 3845f058db8999c99b30ab1c170fab3a604742e9 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Tue, 8 Apr 2025 02:03:25 -0400 Subject: [PATCH 10/16] Remove string evaluation exception, use BiDiException with string formatting in place --- .../BiDi/Modules/Script/EvaluateCommand.cs | 11 +++++-- .../Modules/Script/ScriptEvaluateException.cs | 29 ------------------- .../BiDi/Modules/Script/StackFrame.cs | 12 +++++++- .../BiDi/Modules/Script/StackTrace.cs | 20 ++++++++++++- 4 files changed, 39 insertions(+), 33 deletions(-) delete mode 100644 dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs index bda3ec00ad376..90f7db46af2a8 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs @@ -18,6 +18,7 @@ // using OpenQA.Selenium.BiDi.Communication; +using System; namespace OpenQA.Selenium.BiDi.Modules.Script; @@ -50,7 +51,7 @@ public RemoteValue AsSuccessResult() var exceptionResult = (EvaluateResultException)this; - throw new ScriptEvaluateException(exceptionResult.ExceptionDetails); + throw new BiDiException(exceptionResult.ExceptionDetails.FormatException()); } } @@ -61,4 +62,10 @@ public record EvaluateResultSuccess(RemoteValue Result, Realm Realm) : EvaluateR public record EvaluateResultException(ExceptionDetails ExceptionDetails, Realm Realm) : EvaluateResult; -public record ExceptionDetails(long ColumnNumber, long LineNumber, StackTrace StackTrace, string Text); +public record ExceptionDetails(long ColumnNumber, long LineNumber, StackTrace StackTrace, string Text) +{ + public string FormatException() + { + return $"{Text} ({LineNumber},{ColumnNumber}){StackTrace.FormatStackTrace(indent: 2)}"; + } +} diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs deleted file mode 100644 index 458a860da0db2..0000000000000 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs +++ /dev/null @@ -1,29 +0,0 @@ -// -// Licensed to the Software Freedom Conservancy (SFC) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The SFC licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -// - -using System; - -namespace OpenQA.Selenium.BiDi.Modules.Script; - -public class ScriptEvaluateException(ExceptionDetails evaluateResultException) : Exception -{ - public ExceptionDetails ExceptionDetails { get; } = evaluateResultException; - - public override string Message => $"{ExceptionDetails.Text}{Environment.NewLine}{string.Join(Environment.NewLine, ExceptionDetails.StackTrace.CallFrames)}"; -} diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs b/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs index 150d4c4effc83..afd2d3cdfe21e 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs @@ -19,4 +19,14 @@ namespace OpenQA.Selenium.BiDi.Modules.Script; -public record StackFrame(long LineNumber, long ColumnNumber, string Url, string FunctionName); +public record StackFrame(long LineNumber, long ColumnNumber, string Url, string FunctionName) +{ + public string FormatStackFrame() + { + if (string.IsNullOrEmpty(FunctionName)) + { + return $"({LineNumber},{ColumnNumber}) {Url}"; + } + return $"{FunctionName} ({LineNumber},{ColumnNumber}) {Url}"; + } +} diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs b/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs index 8ac037b88119b..46c8d348efa72 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs @@ -17,8 +17,26 @@ // under the License. // +using System; using System.Collections.Generic; +using System.Linq; namespace OpenQA.Selenium.BiDi.Modules.Script; -public record StackTrace(IReadOnlyCollection CallFrames); +public record StackTrace(IReadOnlyList CallFrames) +{ + public string FormatStackTrace(int indent) + { + if (CallFrames.Count == 0) + { + return string.Empty; + } + + const string Preamble = "---> "; + string firstLineIndentString = Environment.NewLine + new string(' ', indent); + string indentString = Environment.NewLine + new string(' ', indent + Preamble.Length); + + string firstLine = firstLineIndentString + Preamble + CallFrames[0].FormatStackFrame(); + return firstLine + indentString + string.Join(indentString, CallFrames.Skip(1).Select(frame => frame.FormatStackFrame())); + } +} From daed241ed55fa74265931872d58d2aa512c851d3 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Tue, 8 Apr 2025 02:07:20 -0400 Subject: [PATCH 11/16] whitespace --- dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs | 1 + dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs b/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs index afd2d3cdfe21e..59c2317f7780f 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs @@ -27,6 +27,7 @@ public string FormatStackFrame() { return $"({LineNumber},{ColumnNumber}) {Url}"; } + return $"{FunctionName} ({LineNumber},{ColumnNumber}) {Url}"; } } diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs b/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs index 46c8d348efa72..4f5d95c1f52be 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs @@ -33,10 +33,11 @@ public string FormatStackTrace(int indent) } const string Preamble = "---> "; - string firstLineIndentString = Environment.NewLine + new string(' ', indent); - string indentString = Environment.NewLine + new string(' ', indent + Preamble.Length); + string firstLineIndentString = Environment.NewLine + new string(' ', indent); string firstLine = firstLineIndentString + Preamble + CallFrames[0].FormatStackFrame(); + + string indentString = Environment.NewLine + new string(' ', indent + Preamble.Length); return firstLine + indentString + string.Join(indentString, CallFrames.Skip(1).Select(frame => frame.FormatStackFrame())); } } From a8b7410ddcbd76ba183139ad1f4437d11de06b55 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 13 Apr 2025 19:16:34 +0300 Subject: [PATCH 12/16] Fix merge --- dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs index 4dec981432bb2..2a219aac8af95 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs @@ -40,7 +40,7 @@ public record EvaluateOptions : CommandOptions //[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")] //[JsonDerivedType(typeof(EvaluateResultSuccess), "success")] //[JsonDerivedType(typeof(EvaluateResultException), "exception")] -public abstract record EvaluateResult +public abstract record EvaluateResult : EmptyResult { public RemoteValue AsSuccessResult() { From ed99506fc0c830cc905143dd667a07c75f7a6d9d Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 13 Apr 2025 19:31:34 +0300 Subject: [PATCH 13/16] Remove FormatException --- .../webdriver/BiDi/Modules/Script/EvaluateCommand.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs index 2a219aac8af95..cd1ba70403b0b 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs @@ -49,9 +49,7 @@ public RemoteValue AsSuccessResult() return success.Result; } - var exceptionResult = (EvaluateResultException)this; - - throw new BiDiException(exceptionResult.ExceptionDetails.FormatException()); + throw new BiDiException(ToString()); } } @@ -62,10 +60,4 @@ public record EvaluateResultSuccess(RemoteValue Result, Realm Realm) : EvaluateR public record EvaluateResultException(ExceptionDetails ExceptionDetails, Realm Realm) : EvaluateResult; -public record ExceptionDetails(long ColumnNumber, long LineNumber, StackTrace StackTrace, string Text) -{ - public string FormatException() - { - return $"{Text} ({LineNumber},{ColumnNumber}){StackTrace.FormatStackTrace(indent: 2)}"; - } -} +public record ExceptionDetails(long ColumnNumber, long LineNumber, StackTrace StackTrace, string Text); From 7902e1da758694fa9676deffb73367f00c66fd18 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 13 Apr 2025 19:37:24 +0300 Subject: [PATCH 14/16] Reveal original non-success error --- dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs index cd1ba70403b0b..2812ba64e0e1c 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs @@ -49,7 +49,7 @@ public RemoteValue AsSuccessResult() return success.Result; } - throw new BiDiException(ToString()); + throw new InvalidCastException($"Expected the result to be {nameof(EvaluateResultSuccess)}, but received {this}"); } } From 254a377ba519de6e909cef753ff3c3521f4ef697 Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 13 Apr 2025 22:36:03 +0300 Subject: [PATCH 15/16] Remove formatting, the same will be done in another way --- .../BiDi/Modules/Script/StackFrame.cs | 13 +----------- .../BiDi/Modules/Script/StackTrace.cs | 21 +------------------ 2 files changed, 2 insertions(+), 32 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs b/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs index 59c2317f7780f..150d4c4effc83 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/StackFrame.cs @@ -19,15 +19,4 @@ namespace OpenQA.Selenium.BiDi.Modules.Script; -public record StackFrame(long LineNumber, long ColumnNumber, string Url, string FunctionName) -{ - public string FormatStackFrame() - { - if (string.IsNullOrEmpty(FunctionName)) - { - return $"({LineNumber},{ColumnNumber}) {Url}"; - } - - return $"{FunctionName} ({LineNumber},{ColumnNumber}) {Url}"; - } -} +public record StackFrame(long LineNumber, long ColumnNumber, string Url, string FunctionName); diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs b/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs index 4f5d95c1f52be..aec16270b84b0 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs @@ -17,27 +17,8 @@ // under the License. // -using System; using System.Collections.Generic; -using System.Linq; namespace OpenQA.Selenium.BiDi.Modules.Script; -public record StackTrace(IReadOnlyList CallFrames) -{ - public string FormatStackTrace(int indent) - { - if (CallFrames.Count == 0) - { - return string.Empty; - } - - const string Preamble = "---> "; - - string firstLineIndentString = Environment.NewLine + new string(' ', indent); - string firstLine = firstLineIndentString + Preamble + CallFrames[0].FormatStackFrame(); - - string indentString = Environment.NewLine + new string(' ', indent + Preamble.Length); - return firstLine + indentString + string.Join(indentString, CallFrames.Skip(1).Select(frame => frame.FormatStackFrame())); - } -} +public record StackTrace(IReadOnlyList CallFrames); From 575984638c05a169af8e91fc90386cc5e44770df Mon Sep 17 00:00:00 2001 From: Nikolay Borisenko <22616990+nvborisenko@users.noreply.github.com> Date: Sun, 13 Apr 2025 23:21:48 +0300 Subject: [PATCH 16/16] Update ScriptModule.cs --- dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs index f79314959b98b..1839d52969a89 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs @@ -29,9 +29,7 @@ public async Task EvaluateAsync(string expression, bool awaitPro { var @params = new EvaluateCommandParameters(expression, target, awaitPromise, options?.ResultOwnership, options?.SerializationOptions, options?.UserActivation); - var result = await Broker.ExecuteCommandAsync(new EvaluateCommand(@params), options).ConfigureAwait(false); - - return result; + return await Broker.ExecuteCommandAsync(new EvaluateCommand(@params), options).ConfigureAwait(false); } public async Task EvaluateAsync(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null) @@ -45,9 +43,7 @@ public async Task CallFunctionAsync(string functionDeclaration, { var @params = new CallFunctionCommandParameters(functionDeclaration, awaitPromise, target, options?.Arguments, options?.ResultOwnership, options?.SerializationOptions, options?.This, options?.UserActivation); - var result = await Broker.ExecuteCommandAsync(new CallFunctionCommand(@params), options).ConfigureAwait(false); - - return result; + return await Broker.ExecuteCommandAsync(new CallFunctionCommand(@params), options).ConfigureAwait(false); } public async Task CallFunctionAsync(string functionDeclaration, bool awaitPromise, Target target, CallFunctionOptions? options = null)