Spring SimpleThreadScope 不支持解析回调 [英] Spring SimpleThreadScope does not support descruction callbacks
问题描述
我想运行并行 selenium 测试(使用 webriver 和 Spring JUnit 运行程序).Webdriver 是一个具有自定义线程作用域的 spring bean.但是我收到以下警告 SimpleThreadScope 不支持解析回调
所以浏览器没有关闭.知道如何关闭它们(更准确地说是调用 quit 方法)?
I would like to run parallel selenium tests (using webriver and the Spring JUnit runner). Webdriver is a spring bean with the custom thread scope. But I get a following warning SimpleThreadScope does not support descruction callbacks
So the browsers are not closed. Any idea how to close them (more precisely call the quit method)?
弹簧配置
<bean id="threadScope" class="org.springframework.context.support.SimpleThreadScope" />
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="thread" value-ref="threadScope" />
</map>
</property>
</bean>
<bean id="webDriver" class="org.openqa.selenium.remote.RemoteWebDriver" scope="thread" destroy-method="quit">
<constructor-arg name="remoteAddress" value="http://localhost:4444/wd/hub" />
<constructor-arg name="desiredCapabilities" ref="browserAgent" />
</bean>
maven 配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<configuration>
<includes>
<include>**/*Test.class</include>
</includes>
<reportsDirectory>${basedir}/target/surefire-reports</reportsDirectory>
<parallel>classes</parallel>
<threadCount>2</threadCount>
<perCoreThreadCount>false</perCoreThreadCount>
</configuration>
</plugin>
这篇文章 http://www.springbyexample.org/examples/custom-thread-scope-module-code-example.html 建议自定义线程实现.但是使用任何 JUnit 运行程序的 Runnable 扩展点类型在哪里?
This post http://www.springbyexample.org/examples/custom-thread-scope-module-code-example.html suggests a custom Thread implementations. But where is an extension point type of Runnable using any JUnit runner?
public class ThreadScopeRunnable implements Runnable {
protected Runnable target = null;
/**
* Constructor
*/
public ThreadScopeRunnable(Runnable target) {
this.target = target;
}
/**
* Runs <code>Runnable</code> target and
* then afterword processes thread scope
* destruction callbacks.
*/
public final void run() {
try {
target.run();
} finally {
ThreadScopeContextHolder.currentThreadScopeAttributes().clear();
}
}
}
推荐答案
这里有一个变通方法,但不是完美的解决方案,因为它会阻止浏览器直到所有测试结束.
Here there is a workaround, not perfect solution, because it blocks browsers until the end of all tests.
您必须创建一个线程作用域 bean 的寄存器来处理它们的销毁.
You have to create a register of thread scope beans which handles their destruction.
public class BeanRegister {
private Set<CustomWebDriver> beans= new HashSet<CustomWebDriver>();
public void register(CustomWebDriver bean) {
beans.add(bean);
}
@PreDestroy
public void clean() {
for (CustomWebDriver bean : beans) {
bean.quit();
}
}
}
将其配置为单例.
<bean class="BeanRegister" />
您必须编写一个扩展 RemoteWebDriver 的类.
You have to write a class extending RemoteWebDriver.
public class CustomWebDriver extends RemoteWebDriver {
@Autowired
private BeanRegister beanRegister;
@PreConstruct
public void init() {
beanRegister.register(this);
}
}
就是这样.
这篇关于Spring SimpleThreadScope 不支持解析回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!