Single Post

Header

Sunday, November 1, 2015

How to take screen shot in Selenium WebDriver

Take screen shot using Selenium WebDriver

package SeleniumPractise;

import static org.junit.Assert.assertEquals;

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

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.By;
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 TakeScreenShot {
 
    @Test
    public void takeScreenShot() throws InterruptedException, IOException  {
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.co.in");
        driver.manage().window().maximize();
        Thread.sleep(2000);
        driver.findElement(By.className("gsfi")).sendKeys("abcdefghijkl1234324254");
        driver.findElement(By.name("btnG")).click();
        Thread.sleep(3000);

        // take the screenshot
        File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        // now save the screenshot to a file at some place
        // FileUtils.copyFile(srcFile, new File("e:\\tmp\\screenshot.png")); // On windows

        FileUtils.copyFile(srcFile, new File("/usr/local/google/home/venkat/Desktop/testing1/s.png")); // On Ubuntu
         
        WebElement resultsLabel = driver.findElement(By.id("resultStats"));
        assertEquals(true, resultsLabel.isDisplayed());
        driver.close(); // This will not execute if the above statement fails.
    }

}

No comments:

Post a Comment