Anil Kumar
Anil Kumar

Reputation: 23

FileUtils not showing suggestion to import predefined class for Screenshot functionality in selenium WebDriver

I am not allowed to use FileUtils in the program and error is shown when doing so. Even no suggestion is showing to import this pre-defined Class. I tried to search the solution but what I found was to import the class. But in my case even suggestion is not showing to import any class. Hovering on the "FileUtils" shows the suggestion to create FileUtils class/interface. Below is my code:

 package captureScreenshot;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils; //Getting Error at this line
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;
import org.testng.annotations.Test;

import com.google.common.io.Files;

public class FacebookScreenshot {

@Test
    public void captureScreenshot() throws IOException
{
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("https://www.facebook.com");
    driver.findElement(By.xpath("//input[@name='firstname']")).sendKeys("Anil Kumar");

    TakesScreenshot ts = (TakesScreenshot) driver;
    File source = ts.getScreenshotAs(OutputType.FILE);
    FileUtils.copyfile(source,new File("./Screenshots/facebook.png")); //Getting error at this line

    driver.quit();

    }

}

Upvotes: 2

Views: 15364

Answers (7)

cruisepandey
cruisepandey

Reputation: 29372

I was facing the same issue, now you do not need to use external librarby such as org.apache.commons.io..

With Selenium 4+, they have introduce a new class named FileHandler under package package org.openqa.selenium.io;

The below code should help you get the job done:

public void takeScreenShot() {
    TakesScreenshot tss =  (TakesScreenshot)driver;
    File source = tss.getScreenshotAs(OutputType.FILE);
    try {
        FileHandler.copy(source, new File(System.getProperty("user.dir")+"/src/main/resources/"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

Upvotes: 0

Nageswari M
Nageswari M

Reputation: 1

You have to use

Files.copy(sourcefile,destinationfile);

This will work

Upvotes: 0

Mydhili V
Mydhili V

Reputation: 1

In the above FileHandler.copy(scrFile, new File("\screenshot.png")); here copy -the method copy(File, File) is undefined for the type FileHandler is occured

Upvotes: 0

Ahmed Khaled
Ahmed Khaled

Reputation: 11

download maven commons-io jars from: https://mvnrepository.com/artifact/commons-io/commons-io and add jars to your build Path in your project

Upvotes: 0

Madhur Modi
Madhur Modi

Reputation: 11

Yes with Selenium Latest version we should use FileHandler.copy() It works and doesn't throw any error.

// Take Screenshots example
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileHandler.copy(scrFile, new File("<your path>\\screenshot.png"));

Upvotes: 1

Shashank Gupta
Shashank Gupta

Reputation: 11

The line FileUtils.copyFile(); has been updated to FileHandler.copy()

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193298

FileUtils Class

FileUtils Class is defined in org.apache.commons.io.FileUtils which provides the general file manipulation utilities in the following areas :

  • writing to a file
  • reading from a file
  • make a directory including parent directories
  • copying files and directories
  • deleting files and directories
  • converting to and from a URL
  • listing files and directories by filter and extension
  • comparing file content
  • file last changed date
  • calculating a checksum

org.apache.commons.io is bundled along with selenium-server-standalone-x.y.z by default and available ready to use.

But the behavior you are observing is pretty much inline with your usecase where you mentioned that you are not allowed to use FileUtils in the program. It can be either of the scenarios as mentioned below :

  • Incase you are using JARs from selenium-java-3.9.1 client, the JAR containing org.apache.commons.io is not being added to your project.
  • Incase you are using Maven with selenium-java-3.9.1 client dependency the modules containing FileUtils Class have been excluded.

For the following above mentioned reasons, when you mention FileUtils in your program it doesn't show any suggestion to import the class. Moreover if you forcefully provide the import, it will show error on that line.

Upvotes: 3

Related Questions