null => object <null>
undefined => object <null>
42 => Int64 42
3.1415 => Double 3.1415
'text' => String "text"
{} => Dictionary<String, Object> { }
{ x: 42 } => Dictionary<String, Object> { "x": Int64 42 }
{ [0]: 42 } => Dictionary<String, Object> { "0": Int64 42 }
[1, 2, 3] => ReadOnlyCollection<Object> [ Int64 1, Int64 2, Int64 3 ]
['a', 'b', 'c'] => ReadOnlyCollection<Object> [ String "a", String "b", String "c" ]
[1, 2.5, 'str'] => ReadOnlyCollection<Object> [ Int64 1, Double 2.5, String "str" ]
[{ x: 1 }, { x: 2 }] => ReadOnlyCollection<Object> [ Dictionary<String, Object> { "x": Int64 1 }, Dictionary<String, Object> { "x": Int64 2 } ]
function(x) { return x * x; } => Dictionary<String, Object> { }
new Set([1, 2, 3]) => Dictionary<String, Object> { }
new Map([['x', 42], ['y', 43]]) => Dictionary<String, Object> { }
Created
October 26, 2018 13:21
-
-
Save brunnerh/97064bc19f0f1ce6e8f9c32c4be41aa7 to your computer and use it in GitHub Desktop.
Info about Selenium JavaScript to .NET conversions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenQA.Selenium.Chrome; | |
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace SeleniumJavaScriptConversions | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var options = new ChromeOptions(); | |
options.AddArgument("--headless"); | |
using (var driver = new ChromeDriver(options)) | |
{ | |
var scripts = new[] | |
{ | |
"null", | |
"undefined", | |
"42", | |
"3.1415", | |
"'text'", | |
"{}", | |
"{ x: 42 }", | |
"{ [0]: 42 }", | |
"[1, 2, 3]", | |
"['a', 'b', 'c']", | |
"[1, 2.5, 'str']", | |
"[{ x: 1 }, { x: 2 }]", | |
"function(x) { return x * x; }", | |
"new Set([1, 2, 3])", | |
"new Map([['x', 42], ['y', 43]])", | |
}; | |
IEnumerable<(string script, string clr)> columns = scripts.Select(script => | |
{ | |
var value = driver.ExecuteScript("return " + script); | |
var pad = scripts.Select(x => x.Length).Max(); | |
string getTypeName(Type type) | |
{ | |
if (type == null) | |
return "object"; | |
if (type.IsGenericType == false) | |
return type.Name; | |
var generics = String.Join(", ", type.GetGenericArguments().Select(getTypeName)); | |
return $"{type.Name.Split('`')[0]}<{generics}>"; | |
} | |
string objectWithType(object obj) | |
{ | |
var valueString = (string)null; | |
switch (obj) | |
{ | |
case IReadOnlyCollection<object> collection: | |
var itemStrings = collection.Select(objectWithType); | |
valueString = $"[ {String.Join(", ", itemStrings)} ]"; | |
break; | |
case IDictionary<string, object> dict: | |
var kvpStrings = dict.Select(kvp => $"\"{kvp.Key}\": {objectWithType(kvp.Value)}"); | |
valueString = $"{{ {String.Join(", ", kvpStrings)} }}"; | |
break; | |
case string str: | |
valueString = $@"""{str}"""; | |
break; | |
case double d: | |
valueString = d.ToString(CultureInfo.InvariantCulture); | |
break; | |
default: | |
valueString = obj?.ToString() ?? "<null>"; | |
break; | |
} | |
return $"{getTypeName(obj?.GetType())} {valueString}"; | |
}; | |
return (script, objectWithType(value)); | |
}); | |
var scriptPad = columns.Select(c => c.script.Length).Max(); | |
foreach (var (script, clr) in columns) | |
{ | |
Console.WriteLine($"{script.PadRight(scriptPad)} => {clr}"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment