Skip to content

Commit

Permalink
Merge pull request #233 from featurist/execute-script-return-type
Browse files Browse the repository at this point in the history
Fix return type of ExecuteScript for Playwright; Add generic overload
  • Loading branch information
adiel authored Feb 27, 2024
2 parents a5102e0 + 1e2a0d4 commit 4f77292
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/Coypu.Drivers.Tests/When_executing_script.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Coypu.Finders;
using Shouldly;
using NUnit.Framework;
using System;
using Coypu.Tests;

namespace Coypu.Drivers.Tests
{
Expand Down Expand Up @@ -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<string>("return 'miles';", Root);
str.GetType().ShouldBe(typeof(string));

int integer = Driver.ExecuteScript<int>("return 1234;", Root);
integer.GetType().ShouldBe(typeof(int));

DateTime dateTime = Driver.ExecuteScript<DateTime>("return new Date();", Root);
dateTime.GetType().ShouldBe(typeof(DateTime));

try {
Driver.ExecuteScript<int>("return 'bob';", Root);
throw new TestException("Expected an exception");
} catch(Exception e) {
if (e is TestException) throw;
};
}
}
}
9 changes: 8 additions & 1 deletion src/Coypu.Tests/TestDoubles/FakeDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@ public object ExecuteScript(string javascript,
Scope scope,
params object[] args)
{
return Find<string>(_stubbedExecuteScriptResults, javascript, scope);
return ExecuteScript<string>(javascript, scope, args);
}

public ReturnType ExecuteScript<ReturnType>(string javascript,
Scope scope,
params object[] args)
{
return Find<ReturnType>(_stubbedExecuteScriptResults, javascript, scope);
}

public IEnumerable<Element> FindFrames(string locator,
Expand Down
7 changes: 7 additions & 0 deletions src/Coypu.Tests/TestDoubles/StubDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public object ExecuteScript(string javascript,
return null;
}

public ReturnType ExecuteScript<ReturnType>(string javascript,
Scope scope,
params object[] args)
{
return default;
}

public void Hover(Element element) { }

public IEnumerable<Cookie> GetBrowserCookies()
Expand Down
11 changes: 9 additions & 2 deletions src/Coypu/Drivers/Playwright/PlaywrightDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<object>(javascript, scope, args);
}

public ReturnType ExecuteScript<ReturnType>(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<ReturnType>(func, ConvertScriptArgs(args)).Sync();
}

// TODO: extract duplication between Drivers
Expand Down
8 changes: 7 additions & 1 deletion src/Coypu/Drivers/Selenium/SeleniumWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>(javascript, scope, args);
}
public ReturnType ExecuteScript<ReturnType>(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()
Expand Down
2 changes: 2 additions & 0 deletions src/Coypu/IDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public interface IDriver : IDisposable
String Title(Scope scope);
object ExecuteScript(string javascript, Scope scope, params object[] args);

ReturnType ExecuteScript<ReturnType>(string javascript, Scope scope, params object[] args);

[Obsolete("Please use instead: _browserSession.Driver.Cookies.GetAll()")]
IEnumerable<Cookie> GetBrowserCookies();

Expand Down

0 comments on commit 4f77292

Please sign in to comment.