Tuesday 7 January 2014

How to take the screen shots in seelnium 2.0



// store screenshots
public static void captureScreenShot(String filePath) {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
   try {
FileUtils.copyFile(scrFile, new File(filePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
   }

Back to Interview Questions and Answers

6 comments:

  1. Hi,

    Find the running code for screen-shot capture:

    import java.io.File;
    import java.io.IOException;
    import org.apache.commons.io.FileUtils;

    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;


    public class ScreenshotDemo {

    public WebDriver driver;

    @BeforeTest
    public void setup()
    {
    driver = new FirefoxDriver();

    }

    @Test
    public void takeScreenshot() throws InterruptedException{

    driver.get("http://www.google.com/");
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    // Now you can do whatever you need to do with it, for example copy somewhere
    try {
    FileUtils.copyFile(scrFile, new File("d:\\tmp\\screenshot.png"));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    @AfterTest
    public void tearDown()
    {
    driver.quit();
    }
    }

    ReplyDelete
  2. import java.io.*;
    import org.apache.commons.io.FileUtils;
    import org.openqa.selenium.OutputType;

    import org.openqa.selenium.chrome.ChromeDriver;


    public class snapshot{

    public static void main(String agr[]) throws IOException{
    // I am using chrom driver //

    System.setProperty("webdriver.chrome.driver", "D://chromedriver_win32//chromedriver.exe");
    ChromeDriver driver = new ChromeDriver();
    driver.get("http://www.rediff.com");
    File f = driver.getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(f, new File("C:\\abc.png"));

    }


    }

    ReplyDelete
  3. It was very nice article and it is very useful to Selenium learners.We also provide Cub training software online training.

    ReplyDelete
  4. Interest to learn Selenium Online Training by Real Time Software Testing Professional Trainer having 10+ Years of Expert.
    Selenium Training in Hyderabad

    ReplyDelete
  5. thank for you information your blog isa excellent any problem sovle this movement
    once visit selenium free course online trainings

    ReplyDelete
  6. Croma Campus gives continuous selenium Training in Noida. Our selenium testing instrument course incorporates essential to cutting edge level and our selenium course is intended to get the position in great MNC organizations in India as fast as once you finish the Selenium instructional class.

    ReplyDelete