public void ShouldBeAbleToExecuteSimpleJavascriptAndReturnAnObjectLiteral()
{
if (!(driver is IJavaScriptExecutor))
{
return;
}
driver.Url = javascriptPage;
Dictionary<string, object> expectedPerson = new Dictionary<string, object>();
expectedPerson.Add("first", "John");
expectedPerson.Add("last", "Doe");
Dictionary<string, object> expectedResult = new Dictionary<string, object>();
expectedResult.Add("foo", "bar");
List<object> subList = new List<object>() { "a", "b", "c" };
expectedResult.Add("baz", subList.AsReadOnly());
expectedResult.Add("person", expectedPerson);
object result = ExecuteScript(
"return {foo:'bar', baz: ['a', 'b', 'c'], " +
"person: {first: 'John',last: 'Doe'}};");
Assert.IsTrue(result is Dictionary<string, object>, "result was: " + result.GetType().ToString());
Dictionary<string, object> map = (Dictionary<string, object>)result;
Assert.AreEqual(3, map.Count, "Expected:<" + expectedResult.Count + ">, but was:<" + map.Count + ">");
foreach (string expectedKey in expectedResult.Keys)
{
Assert.IsTrue(map.ContainsKey(expectedKey));
}
Assert.AreEqual("bar", map["foo"]);
Assert.IsTrue(CompareLists((ReadOnlyCollection<object>)expectedResult["baz"], (ReadOnlyCollection<object>)map["baz"]));
Dictionary<string, object> person = (Dictionary<string, object>) map["person"];
Assert.AreEqual(2, person.Count);
Assert.AreEqual("John", person["first"]);
Assert.AreEqual("Doe", person["last"]);
}