mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Make a screenshot when test fails to help with debugging
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package org.briarproject.briar.android;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import androidx.test.espresso.Espresso;
|
||||
import androidx.test.espresso.FailureHandler;
|
||||
import androidx.test.espresso.base.DefaultFailureHandler;
|
||||
import androidx.test.runner.screenshot.BasicScreenCaptureProcessor;
|
||||
import androidx.test.runner.screenshot.ScreenCapture;
|
||||
import androidx.test.runner.screenshot.ScreenCaptureProcessor;
|
||||
import androidx.test.runner.screenshot.Screenshot;
|
||||
|
||||
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
|
||||
|
||||
@NotNullByDefault
|
||||
public class ScreenshotOnFailureRule implements TestRule {
|
||||
|
||||
FailureHandler defaultFailureHandler =
|
||||
new DefaultFailureHandler(getApplicationContext());
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
HashSet<ScreenCaptureProcessor> processors = new HashSet<>(1);
|
||||
processors.add(new BasicScreenCaptureProcessor());
|
||||
Screenshot.addScreenCaptureProcessors(processors);
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
AtomicBoolean errorHandled = new AtomicBoolean(false);
|
||||
Espresso.setFailureHandler((throwable, matcher) -> {
|
||||
takeScreenshot(description);
|
||||
errorHandled.set(true);
|
||||
defaultFailureHandler.handle(throwable, matcher);
|
||||
});
|
||||
try {
|
||||
base.evaluate();
|
||||
} catch (Throwable t) {
|
||||
if (!errorHandled.get()) {
|
||||
takeScreenshot(description);
|
||||
}
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void takeScreenshot(Description description) {
|
||||
String name = description.getTestClass().getSimpleName();
|
||||
ScreenCapture capture = Screenshot.capture();
|
||||
capture.setName(name);
|
||||
try {
|
||||
capture.process();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||
import org.briarproject.briar.R;
|
||||
import org.junit.ClassRule;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@@ -23,6 +24,10 @@ import static org.briarproject.briar.android.settings.SettingsFragment.SETTINGS_
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public abstract class UiTest {
|
||||
|
||||
@ClassRule
|
||||
public static final ScreenshotOnFailureRule screenshotOnFailureRule =
|
||||
new ScreenshotOnFailureRule();
|
||||
|
||||
protected final String USERNAME =
|
||||
getApplicationContext().getString(R.string.screenshot_alice);
|
||||
protected static final String PASSWORD = "123456";
|
||||
|
||||
Reference in New Issue
Block a user