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();
}


}

Selenium WebDriver,Running Scripts in a Remote Machine

Selenium is famous for its Cross Browser Testing.With the new changes in Selenium2(Re-implementation of Web driver),running test scripts becomes easier compared to Selenium RC.Selenium web driver does not have any server,but its native API initiates the browser directly.
But this is not suitable for the scenarios where there is a need of running selenium scripts remotely,because we are not running any server.In RC since we are running RC server,we can do that easily.
People of Thought Works came up with a solution for running selenium test remotely.
Here is the procedure for running a selenium test Remotely.

1.Download selenium-server-standalone-2.*.jar Click here to download the jar(If this jar is deprecated please try for a new jar.Just type selenium standalone jar for web driver in Google for new jar).Download the jar file in remote machine.

2.Run the following command java -jar < downloaded jar filepath>in remote machine.

3.Then run the following java code from the machine from which you are going to initiate the remote machine.


import java.io.File;
import java.net.URL;
import org.openqa.selenium.OutputType;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
public class SeleniumTest {
public static void main(String s[]) throws Exception {
URL url = new URL( "http", "localhost", 4444, "/wd/hub" );
DesiredCapabilities capabilities =DesiredCapabilities.internetExplorer();
System.out.println("1");
capabilities.setJavascriptEnabled(true);
System.out.println("2");
WebDriver driver = new RemoteWebDriver(url,capabilities);
System.out.println("4");
driver.get("http://www.google.com");
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\screenshot\\googlesearch-webdriverapi.png"));
driver.quit();
}
}

Change the following statement in your code.

URL url = new URL( "http", "localhost", 4444, "/wd/hub" );

instead of localhost give the remote machine ip address.