If you have upgraded to new version of Selenium 3.6.0 or newer version then you will not get new FileUtils class which we used to get in older versions of Selenium. FileHandler Class in Selenium for Screenshot and help us to copy screenshot from source to destination.
FileHandler is new Class in Selenium which help us the to store screenshots, create directory and so on. You can get full documentation of FileHandler using this link.
Note- ChromeOption and FirefoxOption etc classes are added in new release of Selenium added few more features which you can try in your framework.
Previously we have already seen how to capture screenshot in Selenium and How to capture screenshot only for failed test cases.
Steps to use FileHandler Class in Selenium for Screenshot
Step 1- Import the new package which is
import org.openqa.selenium.io.FileHandler;
Step 2- Call copy method of FileHandler Class which is static method and will ask two argument First is src and another is destination
Code will look like
FileHandler.copy(ts.getScreenshotAs(OutputType.FILE), new File(“C\\Selenium\\Screenshot.png”));
Copy method will throw checked exception which you can handle either throws or try catch block of Java.
Final code will look like.
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.io.FileHandler;
import org.testng.annotations.Test;
public class ChromeHeadlessBrowser {
@Test
public void myfirst()
{
// Set the chrome driver
System.setProperty("webdriver.chrome.driver","./chromedriver.exe");
// pass the option object in ChromeDriver constructor
WebDriver driver=new ChromeDriver();
// Provide any url
driver.get("https://learn-automation.com/");
System.out.println("Title is "+driver.getTitle());
TakesScreenshot ts=(TakesScreenshot)driver;
try {
FileHandler.copy(ts.getScreenshotAs(OutputType.FILE), new File("C\\Selenium\\Screenshot.png"));
} catch (WebDriverException e) {
} catch (IOException e) {
}
// Close the instance
driver.quit();
}
}
Another way to use old way
In older version of Selenium we used Apache Commons library which used to come along with Selenium but in new version of Selenium (3.6.0 or newer) we have to download it separately and then we can use the old Classes and methods.
Library name is – Apache Commons Library
Solution 1- if you are using Normal Java Project then go to below URL, download this library and then add into current project https://commons.apache.org/
Solution 2- If you are using Maven project which I always recommend then add below dependency which will add Apache commons library which you can use in your project.
That’s all I have for this post and you like this post or you find it useful then share with your friends and if you have any though to share then please let me know in the comment section.
Kaustav Bag says
Hi Mukesh, I was stuck with the old way as I didn’t add apache.comms dependency but this really helped. Been following your youtube over a year but never commented on anything. Today sending you an appreciation note on your blog. Thanks for your contribution towards testing community.
Mukesh Otwani says
Thank you so much Kaustav, really means alot to me. Keep learning.
avish sharma says
Great I am stucking at the fileutils but now everything is going smoothly after learning from this.
Thanks
Mukesh Otwani says
Hi Avish,
Glad that my blog content has assisted you to achieve your task. Feel free to ask your doubts and concerns 🙂
Asha says
Thank you. its very helpful. saved my day!
Kulal says
Thank you
Mukesh Otwani says
You’re welcome…:)
shailaja says
Thanks this article was very helpful
Mukesh Otwani says
Hi Shailaja,
Thanks…:)
Gaurank says
Nice Article !!
Mukesh Otwani says
Hi Gaurank,
Thanks buddy…:)
Ruchi says
Great, you keep us updated about new changes. Thanks.
Mukesh Otwani says
Hi Ruchi,
I’ll try my level best to keep my subscribers with new topics…:)