1

I am currently working on Selenium WebDriver, Java and the TestNG framework.

The code I had written in Java.

If my Java code is in Boolean, how I can take a screenshot?

Example:

public boolean test() throws Exception {
    // Some implementation
}

I tried this step, but it is showing an 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"));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
testing
  • 1,736
  • 15
  • 46
  • 75
  • 3
    [check this link](http://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver). Did you really do any searching before posting this question? – Abhijeet Vaikar Mar 21 '14 at 07:03
  • This is the highest voted question in [Selenium FAQ](http://stackoverflow.com/questions/tagged/selenium?sort=frequent&pageSize=50). – Ajinkya Mar 21 '14 at 10:22
  • 1
    Possible duplicate: *[How can I take a screenshot with Selenium WebDriver?](https://stackoverflow.com/questions/3422262/how-can-i-take-a-screenshot-with-selenium-webdriver)* – Peter Mortensen Nov 05 '20 at 21:43
  • What do you mean by *"my Java code is in Boolean"*? – Peter Mortensen Nov 05 '20 at 21:51

7 Answers7

2
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));

    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
loknath
  • 1,362
  • 16
  • 25
  • An explanation would be in order, e.g. what is the intent/idea behind this solution? You can [edit your answer](https://stackoverflow.com/posts/22552491/edit). – Peter Mortensen Nov 05 '20 at 21:49
0

Here is the sample program for this:

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"));
Sathish D
  • 4,854
  • 31
  • 44
  • An explanation would be in order, e.g. what is the intent/idea behind this solution? You can [edit your answer](https://stackoverflow.com/posts/22552667/edit). – Peter Mortensen Nov 05 '20 at 21:50
0

Another thing you can do, instead of screenshots, to make sure a page looks the way you want it to, is to:

  1. Use WebElement.getLocation() to get the coordinates of the element or page section.
  2. Use WebElement.getSize() to get the dimensions of that element or page section.
  3. Use WebElement.getText() to verify contents of that element or section.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
djangofan
  • 28,471
  • 61
  • 196
  • 289
0

If we have a Firefox Driver object then:

    FirefoxDriver Driver = new FirefoxDriver();
    Driver.get("http://facebook.com/");
    File ScrFile = Driver.getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(ScrFile, new File("D://img.jpg"));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ER.swatantra
  • 125
  • 1
  • 5
0

Download the Shutterbug JAR file from here.

public void takeSnapShot(String i) throws Exception {

    /* Function: This function is used to take a snapshot. It
                 will take a snapshot of the entire page.

       Function Call: takeSnapShot("Screen_name"); 
    */

    Shutterbug.shootPage(browser, ScrollStrategy.BOTH_DIRECTIONS).save(Path + i);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0
// Convert web driver object to the TakeScreenshot
TakesScreenshot scrst = ((TakesScreenshot)webdriver);

// Call getScreenshotAs method to create an image file
File SrcFile = scrst.getScreenshotAs(OutputType.FILE);

// Move image file to the new destination
File DestFile = new File(fileWithPath);

// Copy file at the destination
FileUtils.copyFile(SrcFile, DestFile);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0
    TakesScreenshot scrShot = ((TakesScreenshot)webdriver);

    File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);

    File DestFile = new File(fileWithPath);

    FileUtils.copyFile(SrcFile, DestFile);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Java By Kiran
  • 95
  • 1
  • 2
  • 2
    An explanation would be in order, especially when the code seems to be incomplete (e.g., is it a fragment or is it really missing something). Preferably, [edit you answer](https://stackoverflow.com/posts/60669265/edit). – Peter Mortensen Nov 05 '20 at 21:46