ISTQB Certified Software Test Engineer

Tuesday, November 1, 2011

RemoteWebDriver cannot be cast to org.openqa.selenium.Takes Screenshot,error in Selenium

Finally I am running Selenium Scripts in a Remote Machine.This time I need a Screen Shot of the Browser which initiated in Remote Machine.

So I googled and found a piece of code which Can do this task for me.This is the Code.

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

I appended this to my script and executed the script,I got the following error in logs.
org.openqa.selenium.remote.RemoteWebDriver cannot be cast to
org.openqa.selenium.TakesScreenshot.

So finally i found a fix for this error,and got screen shot of the remote machine.Here is the code which worked for me.

public class GoogleTest extends TestCase{

@Test
public void testSearch() throws Exception {
URL url = new URL( "http", "localhost", 4444, "/wd/hub" );
DesiredCapabilities capabilities =DesiredCapabilities.internetExplorer();
capabilities.setJavascriptEnabled(true);
WebDriver driver = new RemoteWebDriver(url,capabilities);
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!");
element.submit();
System.out.println("Page title is: " + driver.getTitle());
Thread.sleep(3000);
assertTrue(driver.getTitle().contains("Cheese!"));
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot)augmentedDriver).
getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File(
"c:\\screenshot\\googlesearch-webdriverapi.png"));
driver.quit();
}


}

6 comments:

  1. Thanks Kumar for sharing this, I was facing the same issue and finally it was fixed.

    ReplyDelete
  2. Thank you Kumar, I had the same issue after I converted my testCases to use Selenium 2 and webdriver. this line:

    WebDriver augmentedDriver = new Augmenter().augment(driver);

    saved my time.

    thanks a lot,
    Eduardo

    ReplyDelete
  3. Using the AugmentedDriver was just the tip I needed to fix my issue. Thanks for posting this Kumar.

    ReplyDelete
  4. Hi Kumar,

    Wonderful post. Will this work for mobile browsers as well? In Android and iOS mobiles

    ReplyDelete
  5. Thanks Raghavendra, this helped.

    ReplyDelete