Last active
December 23, 2015 23:18
-
-
Save huguogang/6708618 to your computer and use it in GitHub Desktop.
JUnit test rule to take screenshot using Selenium Web Driver when test failed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TakeScreenshotOnFailRule extends TestWatcher { | |
protected String _folder; | |
protected WebDriver _driver; | |
public TestRule(WebDriver driver, String folder) { | |
_folder = folder; | |
_driver = driver; | |
} | |
@Override | |
protected void failed(Throwable e, Description description) { | |
super.failed(e, description); | |
String name = description.getDisplayName(); | |
name = _folder + "\\" + name + ".png"; | |
File screenshot = ((TakesScreenshot)_driver).getScreenshotAs(OutputType.FILE); | |
try { | |
FileUtils.copyFile(screenshot, new File(name)); | |
} catch (Exception ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment