How to take screen shot on both test cases "Positive and Negative" results?
NOTE: Currently, I am working in selenium webdriver (junit
) and I did not have an idea about how to code for adding methods to take screen shots.
Take the screenshot in the @After
test tear down method, which is run after every test. This way you will always get a screenshot for both passed and failed tests.
public class TestSample
{
static WebDriver driver;
@Before
public void setUp() {
//start new webdriver session, for eg using firefox
driver = new FirefoxDriver();
}
@Test
public void aTest() {
driver.get("http://www.google.com/");
//more test logic - test might pass or fail at this point
}
@After
public void tearDown() {
// take the screenshot at the end of every test
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// now save the screenshto to a file some place
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
//quit WebDriver session
driver.quit();
}
}
I have written a blog on how to take screen shots while using webdriver APIs, This is a basic code but it will help you in taking screenshots.
package screenshot_package;
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;
public class screenshot {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://apttest.in");
try{
//take screenshot and save it in a file
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//copy the file to the required path
FileUtils.copyFile(screenshot,new File("C:\\Users\\udit\\Desktop\\screenshot.jpeg"));
}catch(Exception e){
//if it fails to take screenshot then this block will execute
System.out.println("Failure to take screenshot "+e);
}
finally
{
//Whatever happens with the screenshot, driver will quit.
driver.quit();
}
}
}
How to take a screenshot in selenium webdriver 2 in MAC O/S
Need to import->
package generic;
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;
@Test
public void browserValidate() throws IOException, InterruptedException {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Thread.sleep(4000);
FileUtils.copyFile(scrFile, new File("/Users/mhossain/Desktop/Mixed/screenshot.png"));
}
MultiScreenShot
I found following useful link in Github for Multiscreenshot
The command should be: captureEntireScreenhot
which is available in commands and the target should be the address of the folder where the screenshot is to be saved.
This has to be written manually. Example as requested:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\saving location\filename.png"))`;
Hope this clears your problem.
captureEntireScreenshot
you mentioned in your answer. Also know that you can edit your answer instead of posting a new one. I recommend you pick one to edit the code into and delete the other.
I am not sure how to do it manually, but there's a testing framework (thucydides) that can do what you need.
This tool in fact integrates Selenium Webdriver with other tools to make your life easier.
You can setup a test maven project quite fast. Since it is open-source, you could check how they capture the screenshots if you don't want to mess with another tool. However, I must say that working with such a tool makes life much easier...
Since it hasn't been mentioned so far: There is Shutterbug.
Selenium Shutterbug is a utility library written in Java for making screenshots using Selenium WebDriver and further customizing, comparing and processing them with the help of Java AWT.
Screenshot of an entire page:
Shutterbug.shootPage(driver, ScrollStrategy.WHOLE_PAGE)
.save("/path/to/screenshots/");
Get the image for a single element:
Shutterbug.shootElement( driver, element )
.getImage()
You can also add highlighting, titles, text, etc.:
Shutterbug.shootPage(driver)
.blur(searchBox)
.highlight(searchBtn)
.monochrome(googleLogo)
.highlightWithText(googleLogo, Color.blue, 3, "Monochromed logo", Color.blue, new Font("SansSerif", Font.BOLD, 20))
.highlightWithText(searchBox, "Blurred secret words")
.withTitle("Google home page - " + new Date())
.withName("home_page")
.withThumbnail(0.7)
.save("/path/to/screenshots/");