Capture the Screen on Test failure WebDriver
Capture the Screen on Test failure WebDriver
Screen captures of the browser session helps you to show error conditions.
#JAVA
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:sample.jpeg"),true);
#Python
#!/usr/bin/env python
from selenium import webdriver
browser = webdriver.Firefox()
browser.get(http://www.google.com/)
browser.save_screenshot(screenie.png)
browser.quit()
Example Test
[Note:- "Scripts in Orange color are mandatory"]
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
public class classname{
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://in.yahoo.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public void testWww() throws Exception {
driver.get(baseUrl + "/?p=us");
driver.findElement(By.id("p_13838465-p")).clear();
driver.findElement(By.id("p_13838465-p")).sendKeys("screen");
driver.findElement(By.id("search-submit")).click();
Thread.sleep(2000);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:sample.jpeg"),true);
Thread.sleep(2000);
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
Use the command: captureEntirePageScreenshotAndWait
Target: C:$(image).jpg
This cmd wont work for WebDriver
Example with captureScreenshot()
Selenium selenium;
selenium = new DefaultSelenium(seleniumServer, 4444,
"*firefox", "http://www.google.com");
selenium.start();
selenium.open("/");
selenium.captureScreenshot("C:screenshot.png");
Examples with captureEntirePageScreenshot()
Selenium selenium;
selenium = new DefaultSelenium(seleniumServer, 4444,
"*firefox", "http://www.google.com");
selenium.start();
selenium.open("/");
selenium.captureEntirePageScreenshot("C:screenshot.png","");