Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium and asynchronous JavaScript calls

i'm quite new to Selenium and JavaScript-callback functions and i have a big issue I can't solve myself. I need a specified variable using JavaScript. If I open the page with GoogleChrome and use the console to enter my JavaScript-Code I can get the variable using something like this:

1. var myId;
2. getSomeIdStuffInfo("somestuff",function(docId)(myId = docId));
3. return myId;

If I enter this lines step by step I easily get the correct value myId. But, of course, if i execute the three lines as fast as possible, i get null as return value because the callback function is not finished when i return myId. SOOOO.. if I use selenium like this:

JavascriptExecutor js = (JavascriptExecutor) driver; 
    String docInfoVal = (String) js.executeScript("" +
            "var myId; " +
            "getCurrentDocumentInfo(\"somestuff\"," +
                "function(docId) {" +
                    "myId = docId;" +
                "}" +
            ");" +
            "return myId;");

I only get null as result. So... somehow I have to "wait" for the callback function until i return myId. Do I have to use executeAsyncScript and how? I'm sitting on it for hours and tried different things but I just can't find the answer.

Thanks in advance for any help!

like image 720
spcial Avatar asked Jul 03 '15 14:07

spcial


2 Answers

For asynchronous code you have to use executeAsyncScript:

JavascriptExecutor js = (JavascriptExecutor) driver; 
String docInfoVal = (String) js.executeAsyncScript("" +
        "var done = arguments[0]; " +
        "getCurrentDocumentInfo(\"somestuff\"," +
            "function(docId) {" +
                "done(docId);" +
            "}" +
        ");");

The script you call with executeAsyncScript will have a callback added to the list of arguments passed to it. Since you pass no arguments to your script, then arguments[0] contains the callback. Your code must call this callback when it is done working. The value you give to the callback is the value that executeAsyncScript returns.

In the code above, I've spelled out the call to done by putting it in an anonymous function but the code could be written more concisely as:

JavascriptExecutor js = (JavascriptExecutor) driver; 
String docInfoVal = (String) js.executeAsyncScript("" +
        "var done = arguments[0]; " +
        "getCurrentDocumentInfo(\"somestuff\", done);");

Or even:

JavascriptExecutor js = (JavascriptExecutor) driver; 
String docInfoVal = (String) js.executeAsyncScript(
        "getCurrentDocumentInfo('somestuff', arguments[0]);");
like image 100
Louis Avatar answered Oct 15 '22 22:10

Louis


Though this is almost same as what @Louis said.You have to set the setScriptTimeout beforehand for the script to pass.

The default timeout for a script to be executed is 0ms. In most cases, including the examples below, one must set the script timeout WebDriver.Timeouts.setScriptTimeout(long, java.util.concurrent.TimeUnit) beforehand to a value sufficiently large enough.

Below is an example for which I'm waiting for 10 seconds to return a string

  driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);//important

    JavascriptExecutor executor = (JavascriptExecutor) driver;
    String val = (String) executor.executeAsyncScript(""
            + "var done=arguments[0]; "
            + "setTimeout(function() {"
            + "   done('tada');"
            + "  }, 10000);");

    System.out.println(val);
like image 23
Madhan Avatar answered Oct 16 '22 00:10

Madhan