下面列出了org.junit.jupiter.api.extension.ExtensionContext#getRequiredTestInstance() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void afterEach(ExtensionContext extensionContext) throws Exception {
if (extensionContext.getExecutionException().isPresent()) {
Object testInstance = extensionContext.getRequiredTestInstance();
Field containerField = testInstance.getClass().getDeclaredField("container");
containerField.setAccessible(true);
BrowserWebDriverContainer browserContainer = (BrowserWebDriverContainer) containerField.get(testInstance);
byte[] screenshot = browserContainer.getWebDriver().getScreenshotAs(OutputType.BYTES);
try {
Path path = Paths
.get("target/selenium-screenshots")
.resolve(String.format("%s-%s-%s.png",
LocalDateTime.now(),
extensionContext.getRequiredTestClass().getName(),
extensionContext.getRequiredTestMethod().getName()));
Files.createDirectories(path.getParent());
Files.write(path, screenshot);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Delegates to {@link TestContextManager#beforeTestMethod}.
*/
@Override
public void beforeEach(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Method testMethod = context.getRequiredTestMethod();
getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
}
/**
* Delegates to {@link TestContextManager#beforeTestExecution}.
*/
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Method testMethod = context.getRequiredTestMethod();
getTestContextManager(context).beforeTestExecution(testInstance, testMethod);
}
/**
* Delegates to {@link TestContextManager#afterTestExecution}.
*/
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Method testMethod = context.getRequiredTestMethod();
Throwable testException = context.getExecutionException().orElse(null);
getTestContextManager(context).afterTestExecution(testInstance, testMethod, testException);
}
/**
* Delegates to {@link TestContextManager#afterTestMethod}.
*/
@Override
public void afterEach(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Method testMethod = context.getRequiredTestMethod();
Throwable testException = context.getExecutionException().orElse(null);
getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException);
}
/**
* Delegates to {@link TestContextManager#beforeTestMethod}.
*/
@Override
public void beforeEach(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Method testMethod = context.getRequiredTestMethod();
getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
}
/**
* Delegates to {@link TestContextManager#beforeTestExecution}.
*/
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Method testMethod = context.getRequiredTestMethod();
getTestContextManager(context).beforeTestExecution(testInstance, testMethod);
}
/**
* Delegates to {@link TestContextManager#afterTestExecution}.
*/
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Method testMethod = context.getRequiredTestMethod();
Throwable testException = context.getExecutionException().orElse(null);
getTestContextManager(context).afterTestExecution(testInstance, testMethod, testException);
}
/**
* Delegates to {@link TestContextManager#afterTestMethod}.
*/
@Override
public void afterEach(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Method testMethod = context.getRequiredTestMethod();
Throwable testException = context.getExecutionException().orElse(null);
getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException);
}
@Override
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public void beforeEach(ExtensionContext context) {
TestClassModel model = getModel(context);
Object testInstance = context.getRequiredTestInstance();
Multimap<EventListener, Browser> eventListeners = initializeEventListener(model, testInstance);
context.getStore(BaseExtension.NAMESPACE).put("registered-eventlisteners", eventListeners);
}
@Override
public void beforeEach(ExtensionContext context) {
Object testInstance = context.getRequiredTestInstance();
getModel(context).getBrowserFields()
.stream()
.filter(browserField -> browserField.isAnnotationPresent(EntryPoint.class))
.forEach(browserField -> openEntryPoint(browserField, testInstance));
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
IInjectorProvider injectorProvider = getOrCreateInjectorProvider(context);
if (injectorProvider instanceof IRegistryConfigurator) {
final IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) injectorProvider;
registryConfigurator.setupRegistry();
}
if (injectorProvider != null) {
Injector injector = injectorProvider.getInjector();
if (injector != null) {
Object testInstance = context.getRequiredTestInstance();
injector.injectMembers(testInstance);
try {
TestInstances requiredTestInstances = context.getRequiredTestInstances();
for (Object o : requiredTestInstances.getEnclosingInstances()) {
injector.injectMembers(o);
}
} catch (NoSuchMethodError e) {
if (!Modifier.isStatic(testInstance.getClass().getModifiers())) {
if (testInstance.getClass().getDeclaringClass() != null) {
throw new ExtensionConfigurationException("Injection of nested classes needs Junit5 >= 5.4", e);
}
}
// OK, getRequiredTestInstances is not there in Junit5 < 5.4
}
}
}
}
/**
* Delegates to {@link TestContextManager#beforeTestMethod}.
*/
@Override
public void beforeEach(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Method testMethod = context.getRequiredTestMethod();
getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
}
/**
* Delegates to {@link TestContextManager#afterTestMethod}.
*/
@Override
public void afterEach(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Method testMethod = context.getRequiredTestMethod();
Throwable testException = context.getExecutionException().orElse(null);
getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException);
}
@Override
public void afterEach(ExtensionContext context) {
try {
Object target = context.getRequiredTestInstance();
AnnotationsHandlerRunner annotationsAdapter = findAnnotationsHandlerAdapterInStore(context);
annotationsAdapter.afterEach(target);
}
finally {
unregisterEmbeddedServer(context, false);
removeAnnotationsHandlerAdapterFromStore(context);
}
}