Source:
public void test_BrowserFunction_callback_with_javaReturningString () { assumeFalse(webkit1SkipMsg(), isWebkit1); AtomicReference<String> returnString = new AtomicReference<>(); class JavascriptCallback extends BrowserFunction { JavascriptCallback(Browser browser, String name) { super(browser, name); } @Override public Object function(Object[] arguments) { return "a � string"; } } class JavascriptCallback_javascriptReceivedJavaString extends BrowserFunction { JavascriptCallback_javascriptReceivedJavaString(Browser browser, String name) { super(browser, name); } @Override public Object function(Object[] arguments) { System.out.println("JavascriptCallback_javascriptReceivedJavaString"); String returnVal = (String) arguments[0]; returnString.set(returnVal); return null; } } String htmlWithScript = "<html><head>\n" + "<script language=\"JavaScript\">\n" + "function callCustomFunction() {\n" + " document.body.style.backgroundColor = 'red'\n" + " var retVal = jsCallbackToJava()\n" + " console.log('val:' + retVal)\n" + " document.write(retVal)\n" + " jsSuccess(retVal)\n" + "}" + "</script>\n" + "</head>\n" + "<body> If you see this, javascript did not receive anything from Java. This page should just be a string </body>\n" + "</html>\n"; browser.setText(htmlWithScript); new JavascriptCallback(browser, "jsCallbackToJava"); new JavascriptCallback_javascriptReceivedJavaString(browser, "jsSuccess"); browser.addProgressListener(callCustomFunctionUponLoad); shell.open(); boolean passed = waitForPassCondition(() -> "a � string".equals(returnString.get())); String message = "Java should have returned something back to javascript. But something went wrong"; assertTrue(message, passed); }

Target:
public void test_BrowserFunction_callback_with_javaReturningString () { AtomicReference<String> returnString = new AtomicReference<>(); class JavascriptCallback extends BrowserFunction { JavascriptCallback(Browser browser, String name) { super(browser, name); } @Override public Object function(Object[] arguments) { return "a � string"; } } class JavascriptCallback_javascriptReceivedJavaString extends BrowserFunction { JavascriptCallback_javascriptReceivedJavaString(Browser browser, String name) { super(browser, name); } @Override public Object function(Object[] arguments) { System.out.println("JavascriptCallback_javascriptReceivedJavaString"); String returnVal = (String) arguments[0]; returnString.set(returnVal); return null; } } String htmlWithScript = "<html><head>\n" + "<script language=\"JavaScript\">\n" + "function callCustomFunction() {\n" + " document.body.style.backgroundColor = 'red'\n" + " var retVal = jsCallbackToJava()\n" + " console.log('val:' + retVal)\n" + " document.write(retVal)\n" + " jsSuccess(retVal)\n" + "}" + "</script>\n" + "</head>\n" + "<body> If you see this, javascript did not receive anything from Java. This page should just be a string </body>\n" + "</html>\n"; browser.setText(htmlWithScript); new JavascriptCallback(browser, "jsCallbackToJava"); new JavascriptCallback_javascriptReceivedJavaString(browser, "jsSuccess"); browser.addProgressListener(callCustomFunctionUponLoad); shell.open(); boolean passed = waitForPassCondition(() -> "a � string".equals(returnString.get())); String message = "Java should have returned something back to javascript. But something went wrong"; assertTrue(message, passed); }