Alp
Alp

Reputation: 29739

Selenium 2 set timeout for specific commands

I have a command webDriver.switchTo().alert(); which has a timeout and waits until it found an alert or the elapsed time is greater than the timeout value. I want to decrease the timeout for this particular command, but didn't find a way. How can i do it?

If possible i'd like to set the timeout to zero, but then back to its default after the alert command.

Update

That's the Selenium core functionality, and i use it as follows:

    try {
        // TODO PERFORMANCE PROBLEM BECAUSE OF TIMEOUT IF NO ALERT EXISTS
        Alert alert = webDriver.switchTo().alert();

        // check if alert exists
        if(alert != null) {
            String alertText = alert.getText();

            // TODO alert handling
            log.info("Alert detected: {}", alertText);
        }
    } catch (Exception e) {
        // timeout if alert does not exist
    }

Upvotes: 1

Views: 451

Answers (1)

CBRRacer
CBRRacer

Reputation: 4659

Without seeing the class that's wrapping that method call, I'll have to make a guess. My guess is that you have this wrapped in a wait for alert or some other function that's blocking until the page is either done loading or until document.ready is complete. So whatever is blocking the script, which should be verifiable by running the application in debug and stepping through the methods. If you want to post some more code up then I might be able to help you better.

-------- EDIT ----------

The only way your going to be able to do that is to extend the Alert class in the Selenium namespace. overload it with an instant fail and I am guessing that it's using some form of javascript to switch to the alert which you should be able to specify fail on the first try. I can't give you any specific code because I can't find the source for the Alert class.

Upvotes: 1

Related Questions