Monkey.D.Luffy
Monkey.D.Luffy

Reputation: 103

How to take screenshots in Selenium at different time intervals and save it at different place error

So I'm using this exact code by @Pawel_Awdmski. I get the error under (OutputType.FILE); says FILE cannot be resolved or is not in a field. Why does it give me that error?

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.server.Response.OutputType;
import org.openqa.selenium.By;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import java.util.NoSuchElementException;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.io.*;
public void screenShot() throws IOException, InterruptedException
{
    File scr=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    File dest= new File("filPath/screenshot_"+timestamp()+".png");
    FileUtils.copyFile(scr, dest);
    Thread.sleep(3000); 
}

public string timestamp() {
        return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
}

Upvotes: 1

Views: 7191

Answers (2)

daya
daya

Reputation: 1

Always use maven it's good practice to add dependencies in a simple way

public class screenshots {
public static void main(String[] args) throws Exception {
    WebDriver driver;
    //put correct path for Gecko driver
    System.setProperty("webdriver.gecko.driver", "G:/Selenium Driver/Gecko/geckodriver.exe");
    driver = new FirefoxDriver();
    driver.get("https://google.com");
    screenShot(driver);
    screenShot(driver);
    screenShot(driver);
}

public static void screenShot(WebDriver driver) throws IOException, InterruptedException {
    File scr = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    File dest = new File("C:/Users/dayan/screenshot_" + timestamp() + ".png");
    FileUtils.copyFile(scr, dest);
    Thread.sleep(3000);
}

public static String timestamp() {
    return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
} }

Upvotes: 0

maxbfuer
maxbfuer

Reputation: 859

I do not know how your code is set up, but I made a test that works without issue. It navigates to Google and takes three screenshots three seconds apart. I believe you may have an import or dependency problem.

Here is the example:

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

    public static void main(String[] args) throws Exception {
        FirefoxDriver driver = new FirefoxDriver();
        driver.get("https://google.com");
        screenShot(driver);
        screenShot(driver);
        screenShot(driver);
    }

    public static void screenShot(FirefoxDriver driver) throws IOException, InterruptedException {
        File scr=(driver).getScreenshotAs(OutputType.FILE);
        File dest= new File("filPath/screenshot_"+timestamp()+".png");
        FileUtils.copyFile(scr, dest);
        Thread.sleep(3000);
    }

    public static String timestamp() {
        return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
    }

}

Upvotes: 3

Related Questions