I was going through Screenshot code in selenium. Below is the code for it :
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.File);
In the above line, TakesScreenshot is an interface and getScreenshotAs is a method. So what I understand from this is, we are typecasting driver into TakesScreenshot interface which essentially means that our driver will behave like TakesScreenshot from now after which getScreenshotAs method will be executed.
My question here is that an interface can only have abstract methods. So, how is getScreenshotAs method is executed through TakesScreenshot interface as it would not have any definition of this method. More precisely, where is getScreenshotAs method defined and how does the above line of code works ?
Adding a few more details :
TakesScreenshot --> an interface
driver --> instance of ChromeDriver class (WebDriver driver = new ChromeDriver())
getScreenshotAs --> method in TakesScreenshot interface.
The above code is used to take screenshot of webpages in selenium.
Well, as I understand, it's not actually a selenium-specific question, but basic Java question.
The meaning of the expression you provided:
is following: no matter what is the type of
driver
variable, in this line we are sure that it implementsTakesScreenshot
interface which hasgetScreenshotAs
method. So we're casting type toTakesScreenshot
and callgetScreenshotAs
method on thedriver
object. The implementation of this method is inside realdriver
class whichever it is.To give you an example which will be really close to the question code (I made this method to accept
Object
so we really need to casto
to the target interface. Don't do it in real code):where
Printable
is some interface with methodprint
:so if we have some implementation of
Printable
likewe can call
which result in line "Hello, username"
Edit:
As I can see in JavaDoc to selenium,
WebDriver
interface does not extendTakesScreenshot
interface. So if the type ofdriver
variable isWebDriver
interface you have to cast it.WebDriver driver = new ChromeDriver()
- there is only reference of typeWebDriver
for compiler. Despite the fact that real class isChromeDriver
compiler doesn't know it. So in this case in order to callgetScreenshotAs
method you have to castdriver
toTakesScreenshot
(and it's safe asdriver
is instance ofChromeDriver
which implements bothWebDriver
andTakesScreenshot
interfaces). Only after that you can callgetScreenshotAs
method fromTakesScreenshot
interface.Well, as I understand, it's not actually a selenium-specific question, but basic Java question.
The meaning of the expression you provided:
is following: no matter what is the type of
driver
variable, in this line we are sure that it implementsTakesScreenshot
interface which hasgetScreenshotAs
method. So we're casting type toTakesScreenshot
and callgetScreenshotAs
method on thedriver
object. The implementation of this method is inside realdriver
class whichever it is.To give you an example which will be really close to the question code (I made this method to accept
Object
so we really need to casto
to the target interface. Don't do it in real code):where
Printable
is some interface with methodprint
:so if we have some implementation of
Printable
likewe can call
which result in line "Hello, username"
Edit:
As I can see in JavaDoc to selenium,
WebDriver
interface does not extendTakesScreenshot
interface. So if the type ofdriver
variable isWebDriver
interface you have to cast it.WebDriver driver = new ChromeDriver()
- there is only reference of typeWebDriver
for compiler. Despite the fact that real class isChromeDriver
compiler doesn't know it. So in this case in order to callgetScreenshotAs
method you have to castdriver
toTakesScreenshot
(and it's safe asdriver
is instance ofChromeDriver
which implements bothWebDriver
andTakesScreenshot
interfaces). Only after that you can callgetScreenshotAs
method fromTakesScreenshot
interface.