Can I have a Selenium executeAsyncScript() callback call back multiple times?

0

I’m looking for a way, if possible, to have a callback that can be called multiple times and handled as such by Selenium multiple times.

What I know

Assuming I’m using the terminology correctly in Selenium, the WebDriver.executeAsyncScript() function uses the callback pattern to allow Selenium in Node.js to await a thenable for a client-side operation rather than blocking the testing process.

So, you pass a function that either explicitly uses a callback function, or registers it with client side code to be called at some point in the future (like with a setTimeout(), which then triggers the function provided to the thenable returned by executeAsyncScript()

function checkInFiveSeconds() {
    driver.executeAsyncScript(callback => {

        setTimeout(function(){
            let val = getSomeValue();
            callback(val);
        }, 5000)
    })

    .then(value => {
        console.log("After five seconds: '" + value + "'");
    });
}

This will execute console.log("After five seconds…, well… after five seconds.

What I want

I want to modify that slightly, to use an interval instead of a timeout… something like:

function checkEveryFiveSeconds() {
    driver.executeAsyncScript(callback => {

        setInterval(function(){
            let val = getSomeValue();
            callback(val);
        }, 5000)
    })

    .then(value => {
        console.log("Every five seconds: '" + value + "'");
    });
}

Where console.log("Every five seconds… etc. is executed every five seconds with the recent value returned by getSomeValue(), which would be more elegant than the alternative below, but I only get the first callback. It’s probably pretty safe to assume that whatever receives the callback in the client context disconnects it from the WebDriver instance in Selenium and can’t be called again…

I’m thinking the only way to get what I want is by manually setting a series of “call executeAsyncScript(), register callback with timeout, call timeout, handle callback, call executeAsyncScript() again, register callback again, etc…”

Is what I want to do or something very similar to it possible?