This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use junit RunListener control screenshot
- By default all tests will have screenshot and screenshots are deleted if test passes - Add NoScreenshot annotation to disable screenshot for test class or method
- Loading branch information
Patrick Huang
committed
Jan 14, 2014
1 parent
958ec16
commit 15294ed
Showing
8 changed files
with
85 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
16 changes: 16 additions & 0 deletions
16
functional-test/src/test/java/org/zanata/util/NoScreenshot.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.zanata.util; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotate on test class or method level to disable screenshot for the target. | ||
* @author Patrick Huang <a | ||
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a> | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
public @interface NoScreenshot { | ||
} |
63 changes: 63 additions & 0 deletions
63
functional-test/src/test/java/org/zanata/util/ScreenshotEnabledTestRunListener.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.zanata.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.junit.runner.Description; | ||
import org.junit.runner.notification.Failure; | ||
import org.junit.runner.notification.RunListener; | ||
import org.zanata.page.WebDriverFactory; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* @author Patrick Huang | ||
* <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a> | ||
*/ | ||
@Slf4j | ||
public class ScreenshotEnabledTestRunListener extends RunListener { | ||
private volatile boolean testFailed; | ||
|
||
@Override | ||
public void testStarted(Description description) throws Exception { | ||
super.testStarted(description); | ||
enableScreenshotForTest(description.getDisplayName()); | ||
} | ||
|
||
@Override | ||
public void testFinished(Description description) throws Exception { | ||
super.testFinished(description); | ||
unregisterScreenshot(); | ||
if (!testFailed) { | ||
deleteScreenshots(description.getDisplayName()); | ||
} | ||
} | ||
|
||
@Override | ||
public void testFailure(Failure failure) throws Exception { | ||
super.testFailure(failure); | ||
testFailed = true; | ||
} | ||
|
||
private static void enableScreenshotForTest(String testDisplayName) | ||
throws Exception { | ||
WebDriverFactory.INSTANCE.updateListenerTestName(testDisplayName); | ||
String date = new Date().toString(); | ||
ScreenshotEnabledTestRunListener.log.debug("[TEST] {}:{}", testDisplayName, date); | ||
} | ||
|
||
private static void deleteScreenshots(String testDisplayName) { | ||
File testDir = ScreenshotDirForTest.screenshotForTest(testDisplayName); | ||
try { | ||
FileUtils.deleteDirectory(testDir); | ||
} catch (IOException e) { | ||
ScreenshotEnabledTestRunListener.log.warn("error deleting screenshot base directory: {}", | ||
e.getMessage()); | ||
} | ||
} | ||
|
||
private static void unregisterScreenshot() { | ||
WebDriverFactory.INSTANCE.unregisterScreenshot(); | ||
} | ||
} |
61 changes: 0 additions & 61 deletions
61
functional-test/src/test/java/org/zanata/util/TakeScreenshotRule.java
This file was deleted.
Oops, something went wrong.