// store screenshots
public static void captureScreenShot(String filePath) {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File(filePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Back to Interview Questions and Answers
Hi,
ReplyDeleteFind the running code for screen-shot capture:
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.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ScreenshotDemo {
public WebDriver driver;
@BeforeTest
public void setup()
{
driver = new FirefoxDriver();
}
@Test
public void takeScreenshot() throws InterruptedException{
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
FileUtils.copyFile(scrFile, new File("d:\\tmp\\screenshot.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@AfterTest
public void tearDown()
{
driver.quit();
}
}
import java.io.*;
ReplyDeleteimport org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;
public class snapshot{
public static void main(String agr[]) throws IOException{
// I am using chrom driver //
System.setProperty("webdriver.chrome.driver", "D://chromedriver_win32//chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("http://www.rediff.com");
File f = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(f, new File("C:\\abc.png"));
}
}
It was very nice article and it is very useful to Selenium learners.We also provide Cub training software online training.
ReplyDeleteInterest to learn Selenium Online Training by Real Time Software Testing Professional Trainer having 10+ Years of Expert.
ReplyDeleteSelenium Training in Hyderabad
thank for you information your blog isa excellent any problem sovle this movement
ReplyDeleteonce visit selenium free course online trainings
Croma Campus gives continuous selenium Training in Noida. Our selenium testing instrument course incorporates essential to cutting edge level and our selenium course is intended to get the position in great MNC organizations in India as fast as once you finish the Selenium instructional class.
ReplyDelete