javaseleniumremotewebdriver

RemoteWebDriver cannot be cast to org.openqa.selenium.TakesScreenshot (Java)


I'm trying to take a screenshot with RemoteWebDriver of Winium.Desktop. As RemoteWebDriver does not implement TakesScreenshot I declared the object as WebDriver.

WebDriver driver = new RemoteWebDriver(new URL(remoteUrl), desiredCapabilities);

WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);

When I try to cast it to I get: java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebDriver cannot be cast to org.openqa.selenium.TakesScreenshot

When I print the driver class name I get RemoteWebDriver although it was declared as WebDriver.

What am I missing?


Solution

  • RemoteWebDriver does implement TakesScreenshot, WebDriver does not. So maybe you can get rid of the cast and it'll solve your problem, though it's very weird you get that exception. My guess is that it's probably because Winium's implementation of RemoteWebDriver isn't complete.

    However, if you tried checking the class name with getClass() during runtime, it resolves to the runtime class of the instance, that's why you get RemoteWebDriver...

    So to me it seems like a defect in Winium.