Zyber
Zyber

Reputation: 448

Selenium screenshot on loadTimeout

I m using selenium webdriver(ver 2.45.0 with FFv38.0.5 in my ubuntu 14.04)in my framework which access some url to check its availability and returns screenshots and some data. its all good if the page is up or down and I can get all the data and screenshot, but the problem is when the page timesout, selenium fails to capture screenshot and throws an exception. I tried my best to find some way to get the screenshot from timedout page and failed thats why I m posting it here as last resort. my code as follows,

driver = new FirefoxDriver(fbp, profile, desiredCapabilities);
driver.manage().timeouts().pageLoadTimeout(loadTimeout , TimeUnit.SECONDS);
try {
    driver.get(url);
} catch (Exception e) {
   File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);           
   FileUtils.copyFile(scrFile, new File("path-to-png"));
}

and the exception I get is

Could not take screenshot of current page - Error: Page is not loaded yet, try later
Command duration or timeout: 7 milliseconds
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'
System info: host: 'santhosh-2840', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-37-generic', java.version: '1.7.0_65'
Session ID: a0754b4f-ff3a-4ee1-8ace-6886eaa958b1
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=38.0.5}]

Is there anyway to get screenshot from selenium for timeodut pages??

p.s:I do know the timedout pages will not have any dom content on which selenium will fetch content length to calculte its screenshot size, somehow I need to get screenshot for timedout pages also.

TIA

Upvotes: 1

Views: 1560

Answers (1)

makeMonday
makeMonday

Reputation: 2415

What is the error that you get when you run the code and the page times out?

I think you have to specifically capture a TimeoutException:

private void takeScreenshot() {
    try {
        File source = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        path = "./path/to/screenshots/screenshot.png";

        source.setWritable(true);

        if(source.canWrite()) {
            FileUtils.copyFile(source, new File(path));
        }
     } catch(IOException e) {
         // ...
     } catch(Exception e) {
         // ...
     }
}

...

try {
    driver.get(url);
} catch (TimeoutException e) {
    takeScreenshot();
}

This works for me.

Upvotes: 1

Related Questions