We use the following code to take screenshots in selenium.
WebDriver driver = new FirefoxDriver();
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("D:\\TestNGScreenshots\\screenshot.png"));
Here is what I understood:
- TakesScreenshot is an interface that provides a method getScreenshotAs() to get screenshots.
- But WebDriver doesn't extend this interface.
- FirexfoxDriver class also doesn't implement this interface
- The getScreenshotAs() method is implemented in a separate class RemoteWebDriver which implements TakesScreenshot.
Here we are casting the driver object to another interface TakesScreenshot and we are using it's method getScreenshotAs() which is implemented in a completely different class.
So if we want to use an interface methods which were implemented in some classes can we use them by casting our object (which was created from a class implementing to another interface) to that interface?
Also, if we create the driver like
FirefoxDriver driver = new FirefoxDriver()
We can't cast the Interface to the driver here. We have to use it like TakesScreenshot ts = drvier and then we can use the method getScreenshotAs(). Here also not sure what exactly happening?
Can someone explain please?
Thank you.