diff --git a/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/BrowsingContext/BrowsingContextScriptModule.cs index 82ae97d28bd72..53b319042fd4e 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.AsSuccessResult().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.AsSuccessResult().ConvertTo(); } } diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs b/dotnet/src/webdriver/BiDi/Modules/Script/EvaluateCommand.cs index 45317ddeb1e94..2812ba64e0e1c 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; @@ -39,7 +40,18 @@ public record EvaluateOptions : CommandOptions //[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")] //[JsonDerivedType(typeof(EvaluateResultSuccess), "success")] //[JsonDerivedType(typeof(EvaluateResultException), "exception")] -public abstract record EvaluateResult : EmptyResult; +public abstract record EvaluateResult : EmptyResult +{ + public RemoteValue AsSuccessResult() + { + if (this is EvaluateResultSuccess success) + { + return success.Result; + } + + throw new InvalidCastException($"Expected the result to be {nameof(EvaluateResultSuccess)}, but received {this}"); + } +} public record EvaluateResultSuccess(RemoteValue Result, Realm Realm) : EvaluateResult { diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs b/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs index 2e458c0377920..0c66ec3c54862 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/LocalValue.cs @@ -299,9 +299,9 @@ public record BigIntLocalValue(string Value) : PrimitiveProtocolLocalValue; public record ChannelLocalValue(ChannelProperties Value) : LocalValue { - // TODO: Revise why we need it + // AddPreloadScript takes arguments typed as ChannelLocalValue but still requires "type":"channel" [JsonInclude] - internal string type = "channel"; + internal string Type => "channel"; } public record ArrayLocalValue(IEnumerable Value) : LocalValue; 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 6b8f92deedcbc..0000000000000 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptEvaluateException.cs +++ /dev/null @@ -1,33 +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(EvaluateResultException evaluateResultException) : Exception -{ - private readonly EvaluateResultException _evaluateResultException = evaluateResultException; - - public string Text => _evaluateResultException.ExceptionDetails.Text; - - public long ColumNumber => _evaluateResultException.ExceptionDetails.ColumnNumber; - - public override string Message => $"{Text}{Environment.NewLine}{_evaluateResultException.ExceptionDetails.StackTrace}"; -} diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs index fde575506f88e..1839d52969a89 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/ScriptModule.cs @@ -25,46 +25,32 @@ 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 await Broker.ExecuteCommandAsync(new EvaluateCommand(@params), options).ConfigureAwait(false); } 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.AsSuccessResult().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 await Broker.ExecuteCommandAsync(new CallFunctionCommand(@params), options).ConfigureAwait(false); } 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.AsSuccessResult().ConvertTo(); } public async Task GetRealmsAsync(GetRealmsOptions? options = null) diff --git a/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs b/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs index 8ac037b88119b..aec16270b84b0 100644 --- a/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs +++ b/dotnet/src/webdriver/BiDi/Modules/Script/StackTrace.cs @@ -21,4 +21,4 @@ namespace OpenQA.Selenium.BiDi.Modules.Script; -public record StackTrace(IReadOnlyCollection CallFrames); +public record StackTrace(IReadOnlyList CallFrames); diff --git a/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs b/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs index 68fb29a1a1145..ae2650426320c 100644 --- a/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs +++ b/dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs @@ -19,363 +19,351 @@ 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 CanCallFunctionWithArgumentTrue() + public async Task CanCallFunctionWithArgumentTrue() { 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 CanCallFunctionWithArgumentFalse() + public async Task CanCallFunctionWithArgumentFalse() { var arg = new BooleanLocalValue(false); - Assert.That(async () => - { - await context.Script.CallFunctionAsync($$""" + + var result = await context.Script.CallFunctionAsync($$""" (arg) => { if (arg !== false) { 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..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.Realm, Is.Not.Null); - Assert.That((res.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.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.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.Result, Is.AssignableFrom()); - Assert.That((res.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.Result, Is.AssignableFrom()); - Assert.That((res.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.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.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.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,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.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] - 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.AsSuccessResult(), 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.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 9b9fde3ecfb6b..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.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.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.Result, Is.AssignableTo()); - Assert.That(((BooleanRemoteValue)response.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.Result, Is.AssignableTo()); - Assert.That(((BooleanRemoteValue)response.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.Result, Is.AssignableTo()); - Assert.That(((StringRemoteValue)response.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.Result, Is.AssignableTo()); - Assert.That(((StringRemoteValue)response.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.Result, Is.AssignableTo()); - Assert.That(response.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.Result, Is.AssignableTo()); - Assert.That(response.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.Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.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.Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.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.Result, Is.AssignableTo()); - Assert.That(((NumberRemoteValue)response.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.Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); - var actualNumberValue = ((NumberRemoteValue)response.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.Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.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.Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.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.Result, Is.AssignableTo()); - var expectedInfinity = ((NumberRemoteValue)response.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.Result, Is.AssignableTo()); - Assert.That(response.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.Result, Is.AssignableTo()); - Assert.That(((ArrayRemoteValue)response.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.Result, Is.AssignableTo()); + Assert.That(response.AsSuccessResult(), 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.AsSuccessResult()).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.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.Result, Is.AssignableTo()); - Assert.That(((SetRemoteValue)response.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 4d389643e808d..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.Realm, Is.Not.Null); - Assert.That((res.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] @@ -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.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] @@ -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.AsSuccessResult(), 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.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 7bb43889b4182..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.Result, Is.AssignableFrom()); + Assert.That(resultAfterRemoval.AsSuccessResult(), Is.AssignableFrom()); } }