Prerequisites

Firstly, you need to download the jar file of Apache Commons IO. Once downloaded, you can import the library by right-clicking on the project -> Properties -> Java Build Path -> Libraries -> Add External JARs...

Selenium Java to take screenshots using TakesScreenshot interface
KW.java
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.chrome.ChromeDriver;
class KW
{
    static WebDriver driver;
    public static void Screenshot()
    {
        File f = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try 
        {
            FileUtils.copyFile(f, new File("D:/Selenium/"+System.currentTimeMillis()+".png"));
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) 
    {   
        System.setProperty("webdriver.chrome.driver", "/home/kodingwindow/drivers/chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.navigate().to("https://kodingwindow.com/testapp/");
        Screenshot();
        
        driver.findElement(By.name("username")).sendKeys("kodingwindow");
        driver.findElement(By.name("username")).clear();
        Screenshot();
        
        driver.findElement(By.name("username")).sendKeys("kodingwindow");
        driver.findElement(By.name("password")).sendKeys("kodingwindow");
        Screenshot();
        driver.findElement(By.name("login")).click();
        Screenshot();

        driver.close();
    }
}
Advertisement