Like us on Facebook

LightBlog

November 8, 2011

Screenshot Using Webdriver / Selenium 2.0

November 08, 2011 4
Screenshot Using Webdriver / Selenium 2.0
Hi All,


Here is a post which explains you how to capture a screenshot using Webdriver/Selenium 2.0

Its a little bit different from Selenium1/Selenium RC.

You can see the screen shot in the specified folder (D:\\screenshot)

Here is the CODE..........

package scripts;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ScreenShot {
WebDriver driver;

@Before
public void setUp() throws Exception {
driver= new FirefoxDriver();
}

@After
public void tearDown() throws Exception {
driver.close();
}
@Test
public void testTextBox() throws Exception {
//open google home page
driver.get("http://www.google.co.in/");
// click on options in top right corner
driver.findElement(By.id("gbi5")).click();
//click on Advanced Search link
driver.findElement(By.id("gmlas")).click();
// type "selenium" in all these words: text box
driver.findElement(By.name("as_q")).sendKeys("selenium");
// click on Advanced Search button
driver.findElement(By.xpath("//input[@type='submit' and @value='Advanced Search']")).click();
// click on first link from search results
driver.findElement(By.linkText("Selenium - Web Browser Automation")).click();
Thread.sleep(5000);
// below two lines code is to take screenshot
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\screenshot\\google.png"));
Thread.sleep(10000);

}

}


Hope it will help you in capturing screenshots....

Thanks,
Madhu