Hello guys,
i spend this weekend with this problem and below is the two things
that i tried but with no any success:
1. First i tried to run the get function as a thread and use the join
function with a time out in order to be sute that will take too much
time to finish:
WebDriver driver = new FirefoxDriver();
String url = "
http://www.google.com";
//The TestGet class is just a Runnable which executes:
this.driver.get(url);
TestGet task = new TestGet(driver,url);
Thread t = new Thread(task, "Timeout guard");
t.setDaemon(true);
t.start();
try {
TimeoutController.execute(t, 2000ms);
} catch (TimeoutException e) {
System.out.println("Thread interrupted:");
driver.close();
}
//TimeoutController.execute is simply a join to the given Thread:
public static void execute(Thread task, long timeout) throws
TimeoutException {
try {
task.join(timeout);
} catch (InterruptedException e) { }
if (task.isAlive()) {
task.interrupt();
throw new TimeoutException();
}
}
Now this Throws an exception:
Invalid use of SingleClientConnManager: connection still allocated.
Which probably is because WebDriver is not thread safe...please
correct me if am i wrong..
2. The second thing that i tried was to execute the window.stop()
method before i will run the get() method with the WebDriver:
WebDriver d = new FirefoxDriver();
executJavaScript("var loadTimeout=setTimeout(\"window.stop();\",
timeOut);",d);
d.get("
http://www.google.com");
d.quit();
But no again. Should stop loading after the timeout but only works
when i put a very small timeout. This is probably because :
"Because of the order in which scripts are loaded, the stop() method
cannot stop the document in which it is contained from loading, but it
will stop the loading of large images, new windows, and other objects
whose loading is deferred."[1]
I suppose the best solution is to implement the get() timeout on our
selfs but i really dont know from where to start since i am newbie on
Http things...
Best,
Dimitris
[1]
https://developer.mozilla.org/en/DOM/window.stop