Can anyone tell whether taking screenshot using selenium(casting takesScreenshot type to driver inst

439 views
Skip to first unread message

Karthik Deepan

unread,
Nov 28, 2018, 1:12:15 PM11/28/18
to Selenium Users

Can anyone tell whether taking screenshot using selenium(casting takesScreenshot type to driver instance) is up casting or down casting?


WebDriver driver = new FirefoxDriver();

driver.get("http://www.google.com/");

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);


FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

connect2tech

unread,
Dec 1, 2018, 1:53:19 PM12/1/18
to Selenium Users
It is downcasting.

If you look at source code of FirefoxDriver.

public class FirefoxDriver extends RemoteWebDriver {

}

and

public class RemoteWebDriver implements WebDriver, JavascriptExecutor,
     
FindsById, FindsByClassName, FindsByLinkText, FindsByName,
     
FindsByCssSelector, FindsByTagName, FindsByXPath,
     
HasInputDevices, HasCapabilities, Interactive, TakesScreenshot {
}


Regards,
~NC

jshw...@gmail.com

unread,
Dec 1, 2018, 10:03:30 PM12/1/18
to Selenium Users
Refer following link for Classes and Interfaces hierarchy in selenium : https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/TakesScreenshot.html

The answer lies in whether you are up casting browser driver class object to WebDriver.

If you are writing -> WebDriver driver = new ChromeDriver()  [Which is up casting your browser class object to WebDriver]
you need to down cast the object to use the method getScreenShotAs of Interface TakesScreenshot.
Reply all
Reply to author
Forward
0 new messages