Here, we discuss that how to capture a screenshot in Selenium WebDriver.
Scenario to be automated:
- Launch the web browser
- Open your application
- Take a screenshot
- Save the screenshot in a location
WebDriver Code goes as below:
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
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.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CaptureScreenshot {
public static void main(String[] args) throws IOException, InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
driver.manage().window().maximize();
WebElement textField = driver.findElement(By.xpath(".//*[@id='sb_ifc0']"));
textField.click();
textField.sendKeys("testing");
driver.findElement(By.xpath(".//*[@id='sblsbb']/button")).click();
Thread.sleep(3000);
File image = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(image, new File("D:\\screenshot_1.png"));
}
}
0 Comment(s)