4

I am building an automation framework on top of Selenium (Node.js) consisting on a number of steps. Each step follows the previous one, after it completes, returning a promise (like the one returned by Selenium's driver.click(), etc). Is it possible to wait for a JavaScript event to trigger on the browser? If so, what is the pattern to follow?

3
  • do you mean wait until your java script fully executed and returned using selenium? Commented Jun 9, 2016 at 11:23
  • No, I meant what I said: wait for a JavaScript event to trigger. Waiting for a script to execute is trivial using driver.executeScript(). Commented Jun 9, 2016 at 11:32
  • For example, I would like to inject window.onmessage = function() { ... continue with the node.js promise chain ... } Commented Jun 9, 2016 at 11:33

1 Answer 1

4

Use .executeAsyncScript to wait for an event to occur :

driver.executeAsyncScript(function(callback) {
  window.addEventListener('message', function onmessage() {
    window.removeEventListener('message', onmessage);
    callback();
  });
});

The doc:

http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_WebDriver.html#executeAsyncScript

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.