Skip to content

Commit

Permalink
MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Dec 2, 2024
1 parent e999a29 commit c53eae9
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 9 deletions.
165 changes: 165 additions & 0 deletions lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,171 @@
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *should accept element handle as an argument*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *tricky*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *circular*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *should throw*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *objects*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *exposed*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *should properly serialize null fields*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *promise*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *error messages*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *should work right after framenavigated*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *should work with unicode chars*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[fixtures.spec] *",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public async Task BasicEvaluationTest(string script, object expected)
public async Task ShouldAcceptNullAsOneOfMultipleParameters()
{
var result = await Page.EvaluateFunctionAsync<bool>(

"(a, b) => Object.is(a, null) && Object.is(b, 'foo')",
null,
"foo");
Expand Down
76 changes: 67 additions & 9 deletions lib/PuppeteerSharp/Bidi/BidiRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand Down Expand Up @@ -71,12 +72,12 @@ public void Dispose()
internal override Task<IJSHandle> EvaluateFunctionHandleAsync(string script, params object[] args) => throw new System.NotImplementedException();

internal override async Task<T> EvaluateExpressionAsync<T>(string script)
=> DeserializeEvaluationResult<T>(await EvaluateAsync(false, true, script).ConfigureAwait(false));
=> DeserializeResult<T>((await EvaluateAsync(true, true, script).ConfigureAwait(false)).Result.Value);

internal override Task<JsonElement?> EvaluateExpressionAsync(string script) => throw new System.NotImplementedException();

internal override async Task<T> EvaluateFunctionAsync<T>(string script, params object[] args)
=> DeserializeEvaluationResult<T>(await EvaluateAsync(false, false, script, args).ConfigureAwait(false));
=> DeserializeResult<T>((await EvaluateAsync(true, false, script, args).ConfigureAwait(false)).Result.Value);

internal override Task<JsonElement?> EvaluateFunctionAsync(string script, params object[] args) => throw new System.NotImplementedException();

Expand Down Expand Up @@ -148,22 +149,74 @@ private async Task<EvaluateResultSuccess> EvaluateAsync(bool returnByValue, bool
return result as EvaluateResultSuccess;
}

private T DeserializeEvaluationResult<T>(EvaluateResultSuccess result)
private T DeserializeResult<T>(object result)
{
if (result is null)
{
return default;
}

if (typeof(T) == typeof(JsonElement?))
{
return (T)(object)JsonSerializer.SerializeToElement(result);
}

// Convert known types first
if (typeof(T) == typeof(int))
{
return (T)(object)Convert.ToInt32(result.Result.Value, CultureInfo.InvariantCulture);
return (T)(object)Convert.ToInt32(result, CultureInfo.InvariantCulture);
}

if (typeof(T) == typeof(double))
{
return (T)(object)Convert.ToDouble(result, CultureInfo.InvariantCulture);
}

switch (result.Result.Type)
if (typeof(T) == typeof(string))
{
case "number":
// TODO: Way too many todos here.
return (T)result.Result.Value;
return (T)result;
}

return default;
if (typeof(T) == typeof(bool))
{
return (T)(object)Convert.ToBoolean(result, CultureInfo.InvariantCulture);
}

if (typeof(T).IsArray)
{
// Get the element type of the array
var elementType = typeof(T).GetElementType();

if (elementType != null && result is IEnumerable enumerable)
{
// Create a list of the element type
var list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType));

// Iterate over the input and add converted items to the list
foreach (var item in enumerable)
{
var itemToSerialize = item;

if (item is RemoteValue remoteValue)
{
itemToSerialize = remoteValue.Value;
}

// Maybe there is a better way to do this.
var deserializedItem = typeof(BidiRealm)
.GetMethod(nameof(DeserializeResult), BindingFlags.Instance | BindingFlags.NonPublic)
?.MakeGenericMethod(elementType)
.Invoke(this, [itemToSerialize]);

list.Add(deserializedItem);
}

// Convert the list to an array
return (T)list.GetType().GetMethod("ToArray")!.Invoke(list, null)!;
}
}

return (T)result;
}

private async Task<LocalValue> FormatArgumentAsync(object arg)
Expand All @@ -178,6 +231,11 @@ private async Task<LocalValue> FormatArgumentAsync(object arg)
arg = await lazyArg(this).ConfigureAwait(false);
}

if (arg is null)
{
return LocalValue.Null;
}

switch (arg)
{
case BigInteger big:
Expand Down

0 comments on commit c53eae9

Please sign in to comment.