Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
use junit RunListener control screenshot
Browse files Browse the repository at this point in the history
- 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
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 79 deletions.
6 changes: 6 additions & 0 deletions functional-test/pom.xml
Expand Up @@ -516,6 +516,12 @@
<!--index page-->
<include>**/FeatureTest.java</include>
</includes>
<properties>
<property>
<name>listener</name>
<value>org.zanata.util.ScreenshotEnabledTestRunListener</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
Expand Down
Expand Up @@ -32,7 +32,6 @@
import org.zanata.page.utility.DashboardPage;
import org.zanata.page.utility.HomePage;
import org.zanata.util.AddUsersRule;
import org.zanata.util.TakeScreenshotRule;
import org.zanata.workflow.BasicWorkFlow;
import org.zanata.workflow.LoginWorkFlow;

Expand All @@ -48,10 +47,6 @@ public class ChangePasswordTest {
@Rule
public AddUsersRule addUsersRule = new AddUsersRule();

@Rule
public TakeScreenshotRule screenshotRule =
new TakeScreenshotRule();

@Before
public void setUp() {
new BasicWorkFlow().goToHome().deleteCookiesAndRefresh();
Expand Down
Expand Up @@ -35,7 +35,6 @@
import org.zanata.page.webtrans.EditorPage;
import org.zanata.util.RetryRule;
import org.zanata.util.SampleProjectRule;
import org.zanata.util.TakeScreenshotRule;
import org.zanata.workflow.BasicWorkFlow;
import org.zanata.workflow.ClientPushWorkFlow;
import org.zanata.workflow.LoginWorkFlow;
Expand All @@ -52,9 +51,6 @@ public class GlossaryDeleteTest {
@Rule
public TestRule sampleProjectRule = new SampleProjectRule();

@Rule
public TakeScreenshotRule screenshotRule = new TakeScreenshotRule();

@Rule
public RetryRule retryRule = new RetryRule(3);

Expand Down
Expand Up @@ -29,7 +29,6 @@
import org.zanata.feature.DetailedTest;
import org.zanata.page.projects.ProjectMaintainersPage;
import org.zanata.util.SampleProjectRule;
import org.zanata.util.TakeScreenshotRule;
import org.zanata.workflow.LoginWorkFlow;

import static org.junit.Assume.assumeTrue;
Expand All @@ -45,9 +44,6 @@ public class EditMaintainersTest {
@Rule
public SampleProjectRule sampleProjectRule = new SampleProjectRule();

@Rule
public TakeScreenshotRule screenshotRule = new TakeScreenshotRule();

@Test
public void maintainerDetailsAreDisplayed() {
ProjectMaintainersPage projectMaintainersPage = new LoginWorkFlow()
Expand Down
Expand Up @@ -30,7 +30,6 @@
import org.zanata.page.projects.CreateVersionPage;
import org.zanata.page.projects.ProjectVersionPage;
import org.zanata.util.SampleProjectRule;
import org.zanata.util.TakeScreenshotRule;
import org.zanata.workflow.LoginWorkFlow;

import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -45,10 +44,6 @@ public class ProjectVersionTest {
@ClassRule
public static SampleProjectRule sampleProjectRule = new SampleProjectRule();

@Rule
public TakeScreenshotRule screenshotRule = new TakeScreenshotRule();


@Test
public void idFieldMustNotBeEmpty() {
CreateVersionPage createVersionPage = new LoginWorkFlow()
Expand Down
16 changes: 16 additions & 0 deletions functional-test/src/test/java/org/zanata/util/NoScreenshot.java
@@ -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 {
}
@@ -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();
}
}

This file was deleted.

0 comments on commit 15294ed

Please sign in to comment.