Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions TomLonghurst.Selenium.PlaywrightWebDriver.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,62 @@ public async Task Script_With_Element_Argument()
Console.WriteLine();
}

[TestCase("5", 5L)]
[TestCase("'foo'", "foo")]
[TestCase("false", false)]
[TestCase("true", true)]
[TestCase("1.5", 1.5d)]
[TestCase("null", null)]
public async Task Script_With_Primitive_Return_Value(string script, object? expectedValue)
{
await using var driver = await PlaywrightWebDriver.CreateAsync();

var result = driver.ExecuteScript(script);

Assert.That(result, Is.EqualTo(expectedValue));
}

[Test]
public async Task Script_With_Array_Return_Value()
{
await using var driver = await PlaywrightWebDriver.CreateAsync();

var result = driver.ExecuteScript("[1, '2', 3]");

Assert.That(result, Is.EqualTo(new object[] {1, "2", 3}));
}

[Test, Ignore("Doesn't work. It throws Microsoft.Playwright.PlaywrightException : SyntaxError: Unexpected token ':'. , though with Selenium ChromeDriver the same script works just fine.")]
public async Task Script_With_Object_Return_Value()
{
await using var driver = await PlaywrightWebDriver.CreateAsync();

var result = driver.ExecuteScript("return {a: 5, b:'foo'}");

Assert.That(result, Is.InstanceOf<Dictionary<string, object>>());
var dict = (Dictionary<string, object>)result;
using (Assert.EnterMultipleScope())
{
Assert.That(dict["a"], Is.EqualTo(5L));
Assert.That(dict["b"], Is.EqualTo("foo"));
}
}

[Test, Ignore("Doesn't work. Result is 'ValueKind = String : \"ref: <Node>\"', but doesn't contain the actual reference to the element. This does work with ChromeDriver though.")]
public async Task Script_With_Element_Return_Value()
{
await using var driver = await PlaywrightWebDriver.CreateAsync();

driver.Url = "file://" + Path.Combine(Environment.CurrentDirectory, "HtmlPages", "LocatorsTest.html");

var idElement = driver.FindElement(By.Id("id1"));
var result = driver.ExecuteScript("return arguments[0]", idElement);

Assert.That(result, Is.AssignableFrom<IWebElement>());
var returnedElement = (IWebElement)result;
Assert.That(returnedElement.Text, Is.EqualTo("Id One"));
}

[Test]
public async Task Locators_Test()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
29 changes: 24 additions & 5 deletions TomLonghurst.Selenium.PlaywrightWebDriver/PlaywrightWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright;
Expand Down Expand Up @@ -225,13 +226,31 @@ public string Url
public object? ExecuteScript(string script, params object[] args)
{
script = ConvertScript(script, args);

if (CurrentFrameLocators.Any())

var playwrightResult = CurrentFrameLocators.Any()
? CurrentFrameLocators.Last().Owner.EvaluateAsync(script, args).Synchronously()
: CurrentPage.EvaluateAsync(script, args).Synchronously();

return ConvertResult(playwrightResult);
}

private static object? ConvertResult(JsonElement? playwrightResult)
{
if (playwrightResult == null)
{
return CurrentFrameLocators.Last().Owner.EvaluateAsync(script, args).Synchronously();
return null;
}

return CurrentPage.EvaluateAsync(script, args).Synchronously();

var value = playwrightResult.Value;
return value.ValueKind switch
{
JsonValueKind.False => false,
JsonValueKind.True => true,
JsonValueKind.Number => value.TryGetInt64(out var intValue) ? intValue : value.GetDouble(),
JsonValueKind.String => value.GetString(),
JsonValueKind.Array => value.EnumerateArray().Select(x => ConvertResult(x)).ToArray(),
_ => value
};
}

#if SeleniumVersion_4
Expand Down