Understanding execute async script in Selenium

后端 未结 2 766
粉色の甜心
粉色の甜心 2020-11-28 09:10

I\'ve been using selenium (with python bindings and through protractor mostly) for a rather long time and every time I needed to execute a javascript code, I\'v

2条回答
  •  北海茫月
    2020-11-28 10:01

    Here's the reference to the two APIs (well it's Javadoc, but the functions are the same), and here's an excerpt from it that highlights the difference

    [executeAsyncScript] Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window. Unlike executing synchronous JavaScript, scripts executed with this method must explicitly signal they are finished by invoking the provided callback. This callback is always injected into the executed function as the last argument.

    Basically, execSync blocks further actions being performed by the selenium browser, while execAsync does not block and calls on a callback when it's done.


    Since you've worked with protractor, I'll use that as example. Protractor uses executeAsyncScript in both get and waitForAngular

    In waitForAngular, protractor needs to wait until angular announces that all events settled. You can't use executeScript because that needs to return a value at the end (although I guess you can implement a busy loop that polls angular constantly until it's done). The way it works is that protractor provides a callback, which Angular calls once all events settled, and that requires executeAsyncScript. Code here

    In get, protractor needs to poll the page until the global window.angular is set by Angular. One way to do it is driver.wait(function() {driver.executeScript('return window.angular')}, 5000), but that way protractor would pound at the browser every few ms. Instead, we do this (simplified):

    functions.testForAngular = function(attempts, callback) {
      var check = function(n) {
        if (window.angular) {
          callback('good');
        } else if (n < 1) {
          callback('timedout');
        } else {
          setTimeout(function() {check(n - 1);}, 1000);
        }
      };
      check(attempts);
    };
    

    Again, that requires executeAsyncScript because we don't have a return value immediately. Code here


    All in all, use executeAsyncScript when you care about a return value in a calling script, but that return value won't be available immediately. This is especially necessary if you can't poll for the result, but must get the result using a callback or promise (which you must translate to callback yourself).

提交回复
热议问题