From 1e2a0d46e14d41a66fab8c4cd26be6e31c016244 Mon Sep 17 00:00:00 2001 From: Adrian Hawkins-Longley Date: Sat, 24 Feb 2024 14:05:30 +0000 Subject: [PATCH] Fix rtn type of ExecuteScript for Playwright + generic overload --- .../When_executing_script.cs | 23 +++++++++++++++++++ src/Coypu.Tests/TestDoubles/FakeDriver.cs | 9 +++++++- src/Coypu.Tests/TestDoubles/StubDriver.cs | 7 ++++++ .../Drivers/Playwright/PlaywrightDriver.cs | 11 +++++++-- .../Drivers/Selenium/SeleniumWebDriver.cs | 8 ++++++- src/Coypu/IDriver.cs | 2 ++ 6 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/Coypu.Drivers.Tests/When_executing_script.cs b/src/Coypu.Drivers.Tests/When_executing_script.cs index a01b0e11..974aec9f 100644 --- a/src/Coypu.Drivers.Tests/When_executing_script.cs +++ b/src/Coypu.Drivers.Tests/When_executing_script.cs @@ -1,6 +1,8 @@ using Coypu.Finders; using Shouldly; using NUnit.Framework; +using System; +using Coypu.Tests; namespace Coypu.Drivers.Tests { @@ -30,6 +32,27 @@ public void Passes_the_arguments_to_the_browser() public void Returns_the_result() { Driver.ExecuteScript("return document.getElementById('firstButtonId').innerHTML;", Root).ShouldBe("first button"); + Driver.ExecuteScript("return 1234;", Root).ShouldBe(1234); + } + + [Test] + public void Returns_the_result_typed() + { + string str = Driver.ExecuteScript("return 'miles';", Root); + str.GetType().ShouldBe(typeof(string)); + + int integer = Driver.ExecuteScript("return 1234;", Root); + integer.GetType().ShouldBe(typeof(int)); + + DateTime dateTime = Driver.ExecuteScript("return new Date();", Root); + dateTime.GetType().ShouldBe(typeof(DateTime)); + + try { + Driver.ExecuteScript("return 'bob';", Root); + throw new TestException("Expected an exception"); + } catch(Exception e) { + if (e is TestException) throw; + }; } } } diff --git a/src/Coypu.Tests/TestDoubles/FakeDriver.cs b/src/Coypu.Tests/TestDoubles/FakeDriver.cs index c8203c8f..30e3fe2c 100644 --- a/src/Coypu.Tests/TestDoubles/FakeDriver.cs +++ b/src/Coypu.Tests/TestDoubles/FakeDriver.cs @@ -113,7 +113,14 @@ public object ExecuteScript(string javascript, Scope scope, params object[] args) { - return Find(_stubbedExecuteScriptResults, javascript, scope); + return ExecuteScript(javascript, scope, args); + } + + public ReturnType ExecuteScript(string javascript, + Scope scope, + params object[] args) + { + return Find(_stubbedExecuteScriptResults, javascript, scope); } public IEnumerable FindFrames(string locator, diff --git a/src/Coypu.Tests/TestDoubles/StubDriver.cs b/src/Coypu.Tests/TestDoubles/StubDriver.cs index a805cfca..996923ec 100644 --- a/src/Coypu.Tests/TestDoubles/StubDriver.cs +++ b/src/Coypu.Tests/TestDoubles/StubDriver.cs @@ -87,6 +87,13 @@ public object ExecuteScript(string javascript, return null; } + public ReturnType ExecuteScript(string javascript, + Scope scope, + params object[] args) + { + return default; + } + public void Hover(Element element) { } public IEnumerable GetBrowserCookies() diff --git a/src/Coypu/Drivers/Playwright/PlaywrightDriver.cs b/src/Coypu/Drivers/Playwright/PlaywrightDriver.cs index 4ed99a11..314fbd41 100644 --- a/src/Coypu/Drivers/Playwright/PlaywrightDriver.cs +++ b/src/Coypu/Drivers/Playwright/PlaywrightDriver.cs @@ -5,6 +5,7 @@ using System.Text.RegularExpressions; using Cookie = System.Net.Cookie; using Microsoft.Playwright; +using OpenQA.Selenium.DevTools.V85.Network; #pragma warning disable 1591 @@ -349,12 +350,18 @@ public void SelectOption(Element select, Element option, string optionToSelect) public object ExecuteScript(string javascript, Scope scope, params object[] args) + { + return ExecuteScript(javascript, scope, args); + } + + public ReturnType ExecuteScript(string javascript, + Scope scope, + params object[] args) { var func = $"(arguments) => {Regex.Replace(javascript, "^return ", string.Empty)}"; return PlaywrightPage(scope) - .EvaluateAsync(func, ConvertScriptArgs(args)).Sync() - .ToString(); + .EvaluateAsync(func, ConvertScriptArgs(args)).Sync(); } // TODO: extract duplication between Drivers diff --git a/src/Coypu/Drivers/Selenium/SeleniumWebDriver.cs b/src/Coypu/Drivers/Selenium/SeleniumWebDriver.cs index ceff279e..ece6ad88 100644 --- a/src/Coypu/Drivers/Selenium/SeleniumWebDriver.cs +++ b/src/Coypu/Drivers/Selenium/SeleniumWebDriver.cs @@ -242,12 +242,18 @@ public void SelectOption(Element select, Element option, string optionToSelect) public object ExecuteScript(string javascript, Scope scope, params object[] args) + { + return ExecuteScript(javascript, scope, args); + } + public ReturnType ExecuteScript(string javascript, + Scope scope, + params object[] args) { if (NoJavascript) throw new NotSupportedException("Javascript is not supported by " + _browser); _elementFinder.SeleniumScope(scope); - return JavaScriptExecutor.ExecuteScript(javascript, ConvertScriptArgs(args)); + return (ReturnType) JavaScriptExecutor.ExecuteScript(javascript, ConvertScriptArgs(args)); } public void Dispose() diff --git a/src/Coypu/IDriver.cs b/src/Coypu/IDriver.cs index bddcebea..8ebceef5 100644 --- a/src/Coypu/IDriver.cs +++ b/src/Coypu/IDriver.cs @@ -22,6 +22,8 @@ public interface IDriver : IDisposable String Title(Scope scope); object ExecuteScript(string javascript, Scope scope, params object[] args); + ReturnType ExecuteScript(string javascript, Scope scope, params object[] args); + [Obsolete("Please use instead: _browserSession.Driver.Cookies.GetAll()")] IEnumerable GetBrowserCookies();