mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Make a screenshot when test fails to help with debugging
This commit is contained in:
@@ -35,12 +35,15 @@ android test:
|
||||
- start-emulator.sh
|
||||
# run normal and screenshot tests together (exclude Large tests)
|
||||
- ./gradlew -Djava.security.egd=file:/dev/urandom connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.notAnnotation=androidx.test.filters.LargeTest
|
||||
after_script:
|
||||
- adb pull /sdcard/Pictures/screenshots
|
||||
artifacts:
|
||||
name: "${CI_PROJECT_PATH}_${CI_JOB_STAGE}_${CI_COMMIT_REF_NAME}_${CI_COMMIT_SHA}"
|
||||
paths:
|
||||
- kernel.log
|
||||
- logcat.txt
|
||||
- briar-android/build/reports/androidTests/connected/flavors/*
|
||||
- screenshots
|
||||
expire_in: 3 days
|
||||
when: on_failure
|
||||
when: manual
|
||||
|
||||
@@ -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