Tuesday 26 August 2014

To take screen shots in Selenium Web Driver


To take screen shot in Selenium Web Driver.


1.       Open a new firefox browser and open the URL.


  WebDriver driver=new FirefoxDriver();

 driver.get("https://in.yahoo.com/?p=us");

                  

 driver.manage().window().maximize();



2.       Write the below code where screen shot needs to be taken. It will show following  errors.


 System.out.println("Taking Screen Shot");

File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(screenshot, new File("C: \\test\\screenshot.jpg"));







3.       For resolving the error : Right click and import the org.apache.commons.io.FileUtils and import the library org.openqa.selenium.TakesScreenshot;

And org.openqa.selenium.OutputType;



import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;



4.       Entire source code for taking a screen shot in selenium web driver. This will store the screen shot at the desired location.


package Testcases;

import java.io.File;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

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;



public class test_screenshots {
   
    public static void main(String[] args) throws Throwable
    {
        WebDriver driver=new FirefoxDriver();
        driver.get("http://demo.mahara.org/");
       
        System.out.println("Taking Screen Shot");
        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshot, new File("C:\\Users\\archana.krushnan\\Desktop\\test\\screenshot.jpg"));

       
    }

}

No comments:

Post a Comment