Screenshot with Selenium WebDriver

0 votes

I’m working on a code using Selenium Webdriver Java and TestNG. How can I take a screenshot using Boolean

public boolean test()throws Exception  {}

Below are the steps I used but it is showing error:

WebDriver driver = new FirefoxDriver();

driver.get("http://www.google.com/");

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

// Now you can do whatever you need to do with it, for example copy somewhere

FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

The error is showing in this line for the copyFile as

The method copyFile(File, File) is

undefined for the type FileUtils

 FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

Jul 12, 2018 in Selenium by Martin
• 4,330 points
1,879 views

1 answer to this question.

0 votes

Try the below code:

import java.io.File;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.apache.commons.io.FileUtils;



public  class Test {

public void screenShot() {

    // driver is your WebDriver

    File screenshot = ((TakesScreenshot) driver)

                            .getScreenshotAs(OutputType.FILE);

    FileUtils.copyFile(screenshot, new File(fileName));

}

}
answered Jul 12, 2018 by Samarpit
• 5,890 points
copyFile method not getting in selenium
Check if you have import FileUtils, it's an apache commons library, you need to download the jar if not yet in your workspace.

Related Questions In Selenium

+1 vote
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to take screenshot in Selenium WebDriver

In Java, it can be done like ...READ MORE

answered Apr 7, 2018 in Selenium by nsv999
• 5,510 points
879 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
2,899 views
0 votes
1 answer
0 votes
1 answer

What is the role of TestNG & JUnit frameworks in Selenium?

Well TestNG and JUnit are test frameworks ...READ MORE

answered Apr 18, 2018 in Selenium by king_kenny
• 3,710 points
712 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,237 views
0 votes
1 answer

Firefox browser not opening with given URL using Selenium Webdriver with Java

Download the latest selenium jar and replace ...READ MORE

answered Apr 30, 2018 in Selenium by Samarpit
• 5,890 points
4,406 views
+1 vote
1 answer

How to handle notifications in Python with Selenium (Chrome WebDriver)

Below will help you: You can disable the ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
5,350 views