2012-10-18 64 views
2

我想运行并行的硒测试(使用webriver和Spring JUnit亚军)。 Webdriver是一个具有自定义线程范围的spring bean。但是,我收到以下警告SimpleThreadScope does not support descruction callbacks因此浏览器未关闭。任何想法如何关闭他们(更确切地说是调用quit方法)?Spring SimpleThreadScope不支持描述回调

Spring配置

<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> 

行家配置

<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的扩展点类型在哪里?

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(); 
     } 
    } 

} 
+0

不错 - 很高兴知道我不是唯一试图从JUnit运行Selenium的人。此外,也可能从多个webdrivers运行。 – ManRow

回答

1

这里有一个解决方法,不是完美的解决方案,因为它阻止浏览器直到所有测试结束。

你必须创建一个处理其销毁的线程范围bean的寄存器。

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类。

public class CustomWebDriver extends RemoteWebDriver { 

    @Autowired 
    private BeanRegister beanRegister; 

    @PreConstruct 
    public void init() { 
     beanRegister.register(this); 
    } 

} 

就是这样。