0

I am writing tests for a web application using Selenium on top of Ruby and I have run into some trouble.

In my automated test, selenium webdriver clicks on an element, which in turn, executes some Javascript.

The Javascript creates an alert window, which I would usually accept with:

@IE.switch_to.alert.accept

In this case however, control is not passed back to the Webdriver and I am unable to accept the alert.

It seems as if the Javascript is still executing - probably just waiting for the alert to be accepted.

But because the webdriver requires the javascript to finish executing BEFORE it can accept the alert, the alert can not be accepted and the javascript will never finish executing.


I do not have permission to modify the Javascript so the fix must be on the selenium side.

Is there any way I can execute the Javascript without forcing the webdriver to wait for it?

----UPDATE 1-----

As a commenter suggested running the javascript in a separate thread has enabled me to regain control with the webdriver: ************WRONG -- SEE UPDATE 2*******************

Thread.new{
  button.press
}

Unfortunately I cannot handle the alert as I usually would:

@IE.switch_to.alert

^ is not working

----UPDATE 2-----

Even when run in a thread, the execution of the javascript still blocks any further execution by the webdriver.

I am using Selenium Grid to execute the tests remotely.

Because of this, I am unable to send keystrokes or any kind of signal to the remote PC unless that method is provided by the remote webdriver, and since the webdriver is blocked by the Javascript execution (even if it is running in a separate thread) I am unable to communicate with the remote PC once the alert is thrown.

The closest idea I have for a solution is like what Murali suggested below, and overide the javascript methods with ones that do not return alerts. However this is very far from ideal, the javascript methods I would be overriding are numerous and complex, some of them are hundreds of lines long.

If anybody has anything to suggest on this matter it would be hugely appreciated as this issue is blocking any further progress on our project.

Thanks!

3
  • Running the JS in a separate thread has worked for me in the past. Don't know Ruby, but perhaps this: tutorialspoint.com/ruby/ruby_multithreading.htm Commented Mar 1, 2016 at 23:42
  • Thanks: Thread.new{ button.press } Has allowed the control to pass back to the Webdriver. However my usual way of accessing alerts is not working in this case: @IE.switch_to.alert It doesn't seem to recognize that the alert exists. Commented Mar 2, 2016 at 1:03
  • Unfortunately the webdriver is blocked unilaterally at Javascript execution, so as soon as a webdriver method is called (which happens to be the piece of code that handles the alert) the script is unable to progress. (even if it is being run in a separate thread). Commented Jul 19, 2016 at 1:32

1 Answer 1

1

If you know the function which triggers that alert, then you can try by using javascript executor. something like below

  ((JavascriptExecutor)driver).executeScript("window.alert = function(msg){return true;};");  

Thank You, Murali

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

1 Comment

Thanks for the advice, this is not ideal however. There are many such cases throughout my tests and I am looking for a generic solution that can be called on every such case.

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.