Narendar Singam
Narendar Singam

Reputation: 1

Taking a screen shot using Selenium

I imported this class from another package and try to call this method, but it is not working.

When I created this method in the same class and called it, it is working.

 private void getScreenshot() throws IOException
 {
      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-YYYY/hh-mm-ssaa");
      String destfile = dateFormat.format(new Date()) + ".png";
      FileUtils.copyFile(scrFile, new File("D:\\workspace\\Firewall\\Resources\\ScreenShots\\"+destfile));
 }

Upvotes: 0

Views: 1672

Answers (2)

Preeti Sharma
Preeti Sharma

Reputation: 1

By using this you can capture screenshot, just need to call captureScreenShot() method for taking screenshot by sending file path

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

Upvotes: 0

Schroet
Schroet

Reputation: 395

I guess the main reason is that you import wrong libraries. Check out:

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

And in case, if your libraries will be the same, try to use my method:

public class TakeScreenshot {
    WebDriver driver;
    public TakeScreenshot(WebDriver driver){
        this.driver = driver;
    }
 public void ScreenShot(String nameTc)
{
// Take screenshot and store as a file format
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
 // now copy the  screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("bin/" + nameTc + ".png"));
}
catch (IOException e)
 {
  System.out.println(e.getMessage());
 }} }

Upvotes: 1

Related Questions