Well, as I understand, it's not actually a selenium-specific question, but basic Java question.
The meaning of the expression you provided:
((TakesScreenshot) driver).getScreenshotAs(OutputType.File)
is following: no matter what is the type of driver
variable, in this line we are sure that it implements TakesScreenshot
interface which has getScreenshotAs
method. So we're casting type to TakesScreenshot
and call getScreenshotAs
method on the driver
object. The implementation of this method is inside real driver
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 cast o
to the target interface. Don't do it in real code):
public void log(Object o) {
((Printable) o).print();
}
where Printable
is some interface with method print
:
public interface Printable {
void print();
}
so if we have some implementation of Printable
like
public class Greeting implements Printable {
@Override
public void print() {
System.out.println("Hello, username");
}
}
we can call
log(new Greeting())
which result in line "Hello, username"
Edit:
As I can see in JavaDoc to selenium, WebDriver
interface does not extend TakesScreenshot
interface. So if the type of driver
variable is WebDriver
interface you have to cast it. WebDriver driver = new ChromeDriver()
- there is only reference of type WebDriver
for compiler. Despite the fact that real class is ChromeDriver
compiler doesn't know it. So in this case in order to call getScreenshotAs
method you have to cast driver
to TakesScreenshot
(and it's safe as driver
is instance of ChromeDriver
which implements both WebDriver
and TakesScreenshot
interfaces). Only after that you can call getScreenshotAs
method from TakesScreenshot
interface.
Well, as I understand, it's not actually a selenium-specific question, but basic Java question.
The meaning of the expression you provided:
((TakesScreenshot) driver).getScreenshotAs(OutputType.File)
is following: no matter what is the type of driver
variable, in this line we are sure that it implements TakesScreenshot
interface which has getScreenshotAs
method. So we're casting type to TakesScreenshot
and call getScreenshotAs
method on the driver
object. The implementation of this method is inside real driver
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 cast o
to the target interface. Don't do it in real code):
public void log(Object o) {
((Printable) o).print();
}
where Printable
is some interface with method print
:
public interface Printable {
void print();
}
so if we have some implementation of Printable
like
public class Greeting implements Printable {
@Override
public void print() {
System.out.println("Hello, username");
}
}
we can call
log(new Greeting())
which result in line "Hello, username"
Edit:
As I can see in JavaDoc to selenium, WebDriver
interface does not extend TakesScreenshot
interface. So if the type of driver
variable is WebDriver
interface you have to cast it. WebDriver driver = new ChromeDriver()
- there is only reference of type WebDriver
for compiler. Despite the fact that real class is ChromeDriver
compiler doesn't know it. So in this case in order to call getScreenshotAs
method you have to cast driver
to TakesScreenshot
(and it's safe as driver
is instance of ChromeDriver
which implements both WebDriver
and TakesScreenshot
interfaces). Only after that you can call getScreenshotAs
method from TakesScreenshot
interface.
WebDriver driver = new ChromeDriver();
// driver.getScreenshotAs(OutputType.File); // compilation error as there is no method getScreenshotAs in WebDriver interface
((TakesScreenshot) driver).getScreenshotAs(OutputType.File); // ok after explicit casting