How to Capture Screenshot In Selenium Webdrier


How to Take Screenshot In Selenium.

Syntax-The Basic Syntax To Capture screenshot given Below
getScreenshotAs()

By Using The getScreenshotsAs() Method We Capture The screenshot In Selenium.
Copy and Paste The Below Code For Capturing the Screenshot In Selenium .

File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotFile, new File("D:/sandeep/bc.png"));
      

FileUtils Error-

When you Get An Error For the FileUtils Then Click on Following Link and Download Jar File and Add it To In Build Path as External Jar.then import the file



Selenium Code For capturing  Screenshot Using Java -

package automationtesting;

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

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScreenShot {

       public static void main(String[] args) throws IOException {
              // TODO Auto-generated method stub

              System.setProperty("webdriver.chrome.driver","D:\\Sandeep\\Soft\\chromedriver.exe");
              WebDriver driver= new ChromeDriver();
              driver.manage().window().maximize();
              driver.get("https://www.google.com");
              File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
           FileUtils.copyFile(screenshotFile, new File("D:/sandeep/bc.png"));
           driver.close();
       }

}

Post a Comment

0 Comments