Taking screenshot is an essential task in Test automation

Its very necessary to take the screenshot when we execute a test script. When executing number of test scripts, and if some test fails, we need to check why the test has failed. In such scenarios screenshots will be helpful. Below is the line of code we will use to take screenshots.

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

To achieve this task, lets find an element with invalid selectors and as a result it will throw an error.

package seleniumBasics;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
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;

public class TakeScreenshot {
    static WebDriver driver;
    public static void main(String[] args) {
        try{
            // TODO Auto-generated method stub
            System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");  
            driver = new FirefoxDriver();
            driver.navigate().to("http://www.google.com");
            driver.findElement(By.name("textbox")).sendKeys("Bishop heber trichy");
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            try {
                takeScreenshot();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }

    // This method will take the screenshot
    private static void takeScreenshot() throws IOException {

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

        //The below method will save the screen shot in d drive with name "screenshot.png"
        FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"));

    }

}

Below is the snap of my D drive where the screenshot is saved in the above program.

results matching ""

    No results matching ""