Reputation: 1239
When I am using a proxy in webdriver like FirefoxDriver, if the proxy is bad then the get method will block forever. I set some timeout parameters, but this did not work out.
This is my code:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override", ua);
Proxy p = new Proxy();
p.setHttpProxy(proxy);
profile.setProxyPreferences(p);
profile.setEnableNativeEvents(true);
// create a driver
WebDriver driver = new FirefoxDriver(profile);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
driver.get("www.sina.com.cn")
The call to driver.get will block for ever, but I want it to wait for 30 seconds and if the page is not loaded then throw an exception.
Upvotes: 48
Views: 204995
Reputation: 191
try
driver.executeScript("window.location.href='http://www.sina.com.cn'")
If you have set pageLoadStrategy
to none
, this statement will return immediately.
And after that , you can add a WebDriverWait with timeout to check if the page title or any element is ok.
Hope this will help you.
Upvotes: 19
Reputation: 544
The timeouts()
methods are not implemented in some drivers and are very unreliable in general.
I use a separate thread for the timeouts (passing the url to access as the thread name):
Thread t = new Thread(new Runnable() {
public void run() {
driver.get(Thread.currentThread().getName());
}
}, url);
t.start();
try {
t.join(YOUR_TIMEOUT_HERE_IN_MS);
} catch (InterruptedException e) { // ignore
}
if (t.isAlive()) { // Thread still alive, we need to abort
logger.warning("Timeout on loading page " + url);
t.interrupt();
}
This seems to work most of the time, however it might happen that the driver is really stuck and any subsequent call to driver will be blocked (I experience that with Chrome driver on Windows). Even something as innocuous as a driver.findElements() call could end up being blocked. Unfortunately I have no solutions for blocked drivers.
Upvotes: 30
Reputation: 21
The solution of driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS)
will work on the pages with synch loading. This does not solve, however, the problem on pages loading stuff in async, then the tests will fail all the time if we set the pageLoadTimeOut
.
Upvotes: 1
Reputation: 1
Used below code in similar situation
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
and embedded driver.get code in a try catch, which solved the issue of loading pages which were taking more than 1 minute.
Upvotes: -1
Reputation: 126
You can set the timeout on the HTTP Client like this
int connectionTimeout=5000;
int socketTimeout=15000;
ApacheHttpClient.Factory clientFactory = new ApacheHttpClient.Factory(new HttpClientFactory(connectionTimeout, socketTimeout));
HttpCommandExecutor executor =
new HttpCommandExecutor(new HashMap<String, CommandInfo>(), new URL(seleniumServerUrl), clientFactory);
RemoteWebDriver driver = new RemoteWebDriver(executor, capabilities);
Upvotes: 2
Reputation: 378
I had the same problem and thanks to this forum and some other found the answer. Initially I also thought of separate thread but it complicates the code a bit. So I tried to find an answer that aligns with my principle "elegance and simplicity".
Please have a look at such forum: https://sqa.stackexchange.com/questions/2606/what-is-seleniums-default-timeout-for-page-loading
#SOLUTION: In the code, before the line with 'get' method you can use for example:
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
#
One thing is that it throws timeoutException so you have to encapsulate it in the try catch block or wrap in some method.
I haven't found the getter for the pageLoadTimeout so I don't know what is the default value, but probably very high since my script was frozen for many hours and nothing moved forward.
#NOTICE: 'pageLoadTimeout' is NOT implemented for Chrome driver and thus causes exception. I saw by users comments that there are plans to make it.
Upvotes: 8
Reputation: 399
I find that the timeout calls are not reliable enough in real life, particularly for internet explorer , however the following solutions may be of help:
You can timeout the complete test by using @Test(timeout=10000) in the junit test that you are running the selenium process from. This will free up the main thread for executing the other tests, instead of blocking up the whole show. However even this does not work at times.
Anyway by timing out you do not intend to salvage the test case, because timing out even a single operation might leave the entire test sequence in inconsistent state. You might just want to proceed with the other testcases without blocking (or perhaps retry the same test again). In such a case a fool-proof method would be to write a poller that polls processes of Webdriver (eg. IEDriverServer.exe, Phantomjs.exe) running for more than say 10 min and kill them. An example could be found at Automatically identify (and kill) processes with long processing time
Upvotes: 0
Reputation: 2253
Try this:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Upvotes: 43