Skip to content

Commit

Permalink
Core - EvaluateScriptAsPromiseAsync not working when JavascriptBindin…
Browse files Browse the repository at this point in the history
…gApiGlobalObjectName specified

Resolves #3281
  • Loading branch information
amaitland committed Nov 14, 2020
1 parent 145fbab commit e9bbcd6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 23 additions & 0 deletions CefSharp.Test/OffScreen/OffScreenBrowserBasicFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@ public async Task CanEvaluateScriptAsyncWithEncodedStringArguments()
}
}

[Theory]
[InlineData("lowerCustom")]
[InlineData("UpperCustom")]
public async Task CanEvaluateScriptAsPromiseAsyncJavascriptBindingApiGlobalObjectName(string rootObjName)
{
using (var browser = new ChromiumWebBrowser("http://www.google.com", automaticallyCreateBrowser:false))
{
browser.JavascriptObjectRepository.Settings.JavascriptBindingApiGlobalObjectName = rootObjName;
browser.CreateBrowser();

await browser.LoadPageAsync();

var mainFrame = browser.GetMainFrame();
Assert.True(mainFrame.IsValid);

var javascriptResponse = await browser.EvaluateScriptAsPromiseAsync("return new Promise(function(resolve, reject) { resolve(42); });");

Assert.True(javascriptResponse.Success);

Assert.Equal("42", javascriptResponse.Result.ToString());
}
}

[Theory]
[InlineData("return 42;", true, "42")]
[InlineData("return new Promise(function(resolve, reject) { resolve(42); });", true, "42")]
Expand Down
6 changes: 3 additions & 3 deletions CefSharp/WebBrowserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ public static Task<JavascriptResponse> EvaluateScriptAsPromiseAsync(this IWebBro
/// When the promise either trigger then/catch this returned Task will be completed.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown when one or more arguments are outside the required range.</exception>
/// <param name="chromiumWebBrowser">The ChromiumWebBrowser instance this method extends.</param>
/// <param name="frame">The <seealso cref="IFrame"/> instance this method extends.</param>
/// <param name="script">The Javascript code that should be executed.</param>
/// <param name="timeout">(Optional) The timeout after which the Javascript code execution should be aborted.</param>
/// <returns>
Expand All @@ -1039,11 +1039,11 @@ private static string GetPromiseHandlerScript(string script, string javascriptBi

if (char.IsLower(internalJsFunctionName[0]))
{
internalJsFunctionName += "sendEvalScriptResponse";
internalJsFunctionName += ".sendEvalScriptResponse";
}
else
{
internalJsFunctionName += "SendEvalScriptResponse";
internalJsFunctionName += ".SendEvalScriptResponse";
}
}
var promiseHandlerScript = "let innerImmediatelyInvokedFuncExpression = (function() { " + script + " })(); Promise.resolve(innerImmediatelyInvokedFuncExpression).then((val) => " + internalJsFunctionName + "(cefSharpInternalCallbackId, true, val)).catch ((reason) => " + internalJsFunctionName + "(cefSharpInternalCallbackId, false, String(reason))); return 'CefSharpDefEvalScriptRes';";
Expand Down

0 comments on commit e9bbcd6

Please sign in to comment.