cobrabb
cobrabb

Reputation: 141

Exiting a program after a certain amount of time - Java

I am writing a program in Selenium using Java. In my code, I have:

driver = new FirefoxDriver();

My problem is this: if I am not connected to the internet when the program executes this line of code, the program will hang for a long time (on the order of minutes) before finally opening up a Firefox Window and then crashing on the next line of code

driver.doWhatever();

I am trying to make this as user friendly as possible, so I would love to have my program exit if

driver = new FirefoxDriver();

does not complete within fifteen to twenty seconds.

Is there a way to do this in Java? I could always go into Selenium and tinker with it so that it works the way I want it to, but that seems like the "wrong" way to go about solving this problem.

Thanks in advance.

Upvotes: 2

Views: 281

Answers (1)

Stephen B.
Stephen B.

Reputation: 204

The driver class may be modified to adjust the wait time that you are having trouble with.

It should look something like this:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

You could adjust the time to whatever you want, but be warned that reducing it too low may cause other issues, for example if the page is simply taking a little longer to load, it will timeout prematurely.

Source: WebDriver: Advanced Usage -- Selenium Documentation

Upvotes: 4

Related Questions