0

Suppose I have this (fake) JavaScript code:

asynchronousOperation.addEventListener("completed", 
    function (event) {
        if (event.property == "required value") tell Selenium we are good;
        else tell Selenium the test failed;
    });
asynchronousOperation.run();

I'm using Python for writing tests, but I think I'd find a way to adapt the code if it is in some other language.

The best I could manage so far is to write the result somewhere in the page and then check that place using timer. But this sounds like it could be done better.

5
  • You're on exactly the right track. Adam Goucher calls this type of mechanism a "JavaScript latch", and it's exactly the kind of construct you want to use. Commented Jun 25, 2013 at 14:02
  • @JimEvans the problem with this approach is that I'd need to estimate the time, when the test must terminate. Although most times I would want not to wait forever for the test to finish, but I might also get greedy and set very short timeout... and then the tests will fail randomly depending on server load and such... Commented Jun 25, 2013 at 14:27
  • 1
    That's why you use the WebDriverWait construct, which should be available in most language bindings, including Python. This establishes a timeout, but polls for the condition you want, returning as soon as the condition is met or the timeout is reached. In this case, the condition would be the JavaScript variable on the page having the appropriate value. Commented Jun 25, 2013 at 15:41
  • @JimEvans thanks, if you want to make it an answer, I'd accept it. Found this page in the documentation: docs.seleniumhq.org/docs/04_webdriver_advanced.jsp which basically gives the example of what I want, thanks to you mentioning WebDriverWait. Commented Jun 25, 2013 at 16:06
  • Made into an answer. Thanks! Commented Jun 25, 2013 at 17:18

1 Answer 1

1

You're on exactly the right track. Adam Goucher calls this type of mechanism a "JavaScript latch", and it's exactly the kind of construct you want to use. Typically, you'd want to use it with the WebDriverWait construct, which should be available in most language bindings, including Python. WebDriverWait establishes a timeout, but polls for the condition you want, returning as soon as the condition is met or the timeout is reached. In this case, the condition would be the JavaScript variable on the page having the appropriate value.

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.