Capture Screenshot Selenium WebDriver
Most of the time we think to Capture Screenshot in Selenium WebDriver when some kind of error or exception surfaces while practicing testing, to resolve the same WebDriver has provided us one interface TakesScreenshot for capturing the screenshot of web application and This interface provides one method names as getScreenshotAs() to capture screenshot in the same instance of driver. This getScreenshotAs() method takes argument of type OutputType.File or OutputType.BASE64 or Output.BYTES. So that it could return captured screenshot in File type, or Base 64 string type or in raw bytes.
So this would look like this
For File type
File scrnshot= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
For Base64 string above code would be like
((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
For BYTES
((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
We have taken the screenshot with the help of getScreenshotsAs() method and and now its time to copy this file somewhere in our file system or in our desktop. So for this purpose we further use copyFile() method of the FileUtils class from the org.apache.commons.io.FileUtils class.
Placing file in file system by using this line
FileUtils.copyFile(scrFile, new File(“e:\main_page.png”));
As I have told you that copyFile is a method of Class FileUtils and to call this method we need to write class.method() and in above code copyFile is taking argument from getScreenShotsAs() and new location where we want to save this captured Screenshot with name and with extension.
Now we would use write small running code that would open Google page and it would take a snap shot and that snap shot would be saved as google.png in my e: driver. One thing we need to remember whenever we work with File system or java.io then chances of exception is more so we would use try and catch in our code.
import java.io.File;
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.BeforeMethod;
import org.testng.annotations.Test;
public class Chrome {
WebDriver driver;
@BeforeMethod
public void launchChrome()
{
System.setProperty("webdriver.chrome.driver", "E:\DD MISHRA\workspace\chromedriver_win_26.0.1383.0\chromedriver.exe");
driver = new FirefoxDriver();
driver.get("http://google.co.in");
}
@Test
public void googleScreenshot()
{
try {
File scrnsht =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrnsht, new
File("e:\google_page.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterTest
public void kill()
{
driver.close();
}
}
Original Resource available on this place
http://stackoverflow.com/questions/13246477/how-to-capture-the-screen-shot-of-a-webpage-using-selenium-webdriver
I’m so happy to read this. This is the type of manual that needs to be given and not the
accidental misinformation that is at the other blogs. Appreciate your sharing this best doc.
It’s in reality a nice and helpful piece of
info. I am satisfied that you simply shared this helpful
info with us. Please stay us up to date like this. Thank you for
sharing.
Hai Thanks for sharing this information.
but this is not working with google chrome.It gives black screen shot.
Thanks Mishra for sharing the valuable information. Helped a lot..!!!
Thanks a lot for your valuable information to on capture screenshot using selenium webdriver.
It could be useful if you you provide more in depth information on Edit box,Check/Uncheck,Drop down,scrollpage,radion button,Link text implicit wait condition, etc.,,
sure very soon I will bring these articles
I am getting unsupported operatin exception while trying to execute the same.
ping me on skype id dwarika.dhish.mishra and will see the actual problem, What is happening there or send me the stack trace of this problem
java.lang.UnsupportedOperationException: captureEntirePageScreenshot
at org.openqa.selenium.WebDriverCommandProcessor.execute(WebDriverCommandProcessor.java:137)
at org.openqa.selenium.WebDriverCommandProcessor.doCommand(WebDriverCommandProcessor.java:70)
at com.thoughtworks.selenium.DefaultSelenium.captureEntirePageScreenshot(DefaultSelenium.java:723)
at testNG.AccountMisMatch.generateBulkReturn(AccountMisMatch.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
i am using same code which working well on normal pages
buton a page with a video running like youtube the screenshot of video is coming as black
can u check once and suggest any solution
Hi
I am trying to take screen shot using following code but its not working
File scht=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scht, newFile(“c:\\google_page.png”));
newFile is showing error saying method newFile(String) is undefined. Can you kindly help me to solve this issue
Thank you
Hi
My mistake….Its working fine. Thank you
Hi All,
I have Flex application in my project . Can anyone suggest me, What are the additional setup needs to be done.
Hi Guys,
I found following useful link in Github to take MultiScreenShot
https://github.com/MultiScreenShot/MultiScreenShot
Regards,
Pavankumar Nagaraj
+91 8971673487
pavankumar.nagaraj@gmail.com
in.linkedin.com/in/pavankumarnagaraj/
Bangalore.
Thank you. I’ve been looking for this for a while since in the IDE is pretty simple but the I couldn’t make it work using the backed webdriver after exporting.
Best.
Is there any way to get screen shot of the entire page using selenium and java.I tried many API’s present in web but for chrome and IE they are not working.Please suggest.
There is one solution just use getpagesource method and save the code as html and it would be the complete code…but let me explore some options at my end
Thanks for your Post! Really it will be helpful for both beginners and experienced professional. Your information is very clear.
Along with your post, I put my input here.
The following example is in Java:
WebDriver driver = new FirefoxDriver();
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
FileUtils.copyFile(scrFile, new File(“c:\\tmp\\screenshot.png”));
If your audience is also interested in Selenium Testing, they can take a look here: Selenium Training
This article is really very helpful but if the screenshot is deleted than i won’t able to see screenshot in extent report or if i send this report to someone else they won’t see that snapshot in report. Can anyone suggest some way to store snapshot on web so that it can be accessed anytime or some other better way.
Thanks,
Arpit Jain
Hi,
I am getting error at this point:
File screenshot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Exception taking screenshot is null.
The code is:
public static String getScreenshot(WebDriver getDriver, String ScreenshotName)
{
try
{
String dateName = new SimpleDateFormat(“yyyyMMddhhmmss”).format(new Date());
// TakesScreenshot ts = (TakesScreenshot)driver;
System.out.println(“awshjkrhjkshdfjk”);
File source = ((TakesScreenshot)getDriver).getScreenshotAs(OutputType.FILE);
System.out.println(“awshjkrhjkshdfsdasfsdgfsjk”);
String dest=System.getProperty(“user.dir”) + “/FailedTestsScreenshots/”+ScreenshotName+dateName+”.png”;
File snapshotDest =new File(dest);
FileUtils.copyFile(source, snapshotDest);
System.out.println(“Screenshot Taken at “+System.currentTimeMillis());
return dest;
}
catch (Exception e)
{
System.out.println(“Exception while taking screenshot “+e);
return e.getMessage();
}
}
Tried calling this method in another class:
@AfterMethod
public void teardown(ITestResult result) throws Exception
{
if (result.getStatus()==ITestResult.FAILURE)
{
test.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + “Test Case failed”, ExtentColor.RED));
System.out.println(“egwwr”);
String screenshotPath = screenshot.getScreenshot(driver, result.getName());
System.out.println(“egwwrgdfh”);
test.log(Status.FAIL, screenshotPath);
System.out.println(“dhfuiseryuhrjh”);
}
else if(result.getStatus()==ITestResult.SUCCESS)
{
test.log(Status.PASS, MarkupHelper.createLabel(result.getName() + “Test Case passed”, ExtentColor.GREEN));
System.out.println(“egwwr”);
String screenshotPath = screenshot.getScreenshot(driver, result.getName());
test.log(Status.PASS, screenshotPath);
System.out.println(“dhfuiseryuhrjh”);
}
else
{
test.log(Status.SKIP, MarkupHelper.createLabel(result.getName() + “Test Case skipped”, ExtentColor.YELLOW));
String screenshotPath = screenshot.getScreenshot(AllDrivers.getDriver(), result.getName());
}
extent.flush();
}
I tried figuring out a solution,but i was unable to.Please help me with a solution.
Thanks,
Anideep .