Spring 4集成WebDriver AbstractTestNGSpringContextTests

时间:2016-09-19 20:16:08

标签: java spring selenium junit4

集成WebDriver,Spring运行的项目对系统进行测试。每个视图的测试分为5个步骤(登录,输入字段,搜索结果,有效结果,更改查找和删除一个结果)。

项目的主要构成

  • Spring 4.2.4.RELEASE
  • selenium-java 2.53.1
  • testng 6.9.10

第一次测试确定,当浏览器运行Firefox时,如果我注入相同的测试并关闭。

@Autowired
@Qualifier("firefoxDriver")
protected WebDriver firefox;

但更改策略以使用关闭驱动程序,更改为:

@ComponentScan
@ApplicationScoped
@Configuration(value = "configDriver")
public class SysNGSeleniumDriver extends AnnotationConfigApplicationContext implements BeanFactory, DisposableBean
{
    private static final Logger LOG = SysUtilLogLevel.logClass(SysNGSeleniumDriver.class);

    private FirefoxDriver   driver;

    @Bean(name = "firefoxDriver")
    @Autowired
    public WebDriver getFirefoxDriver() {
        if (driver == null){
            FirefoxProfile firefoxProfile = new FirefoxProfile();
            firefoxProfile.setPreference("capability.policy.default.Window.frameElement", "allAccess");
            driver = new FirefoxDriver(firefoxProfile);
        }
        return driver;
    }

    @PreDestroy
    @Override
    public void destroy(){
        try
        {
            driver.close();
        }catch (Exception e){
            LOG.warn(SysUtilLogLevel.ERROR, "", e);
        }
    }
}

为什么要改变这个策略我需要插入

  • 登录系统
  • 然后为测试创建相同的输入字段
  • 有效结果
  • 执行另一个测试序列后,系统的其他功能

此时此错误:

WARN  - 16:34:24 131  - com.SysNGSeleniumDriver.destroy(52)                                    -  
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: 'qosmio', ip: '192.168.2.30', os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.8.0_73'
Driver info: driver.version: SysNGSeleniumDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=47.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: ddb4ecc7-4f8c-49ae-9b65-b4831541c6d3
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:665) ~[selenium-remote-driver-2.53.1.jar:na]
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:701) ~[selenium-remote-driver-2.53.1.jar:na]
    at org.openqa.selenium.remote.RemoteWebDriver.close(RemoteWebDriver.java:521) ~[selenium-remote-driver-2.53.1.jar:na]
    at com.SysNGSeleniumDriver.destroy(SysNGSeleniumDriver.java:48) ~[test-classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_73]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_73]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_73]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_73]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:354) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeDestroyMethods(InitDestroyAnnotationBeanPostProcessor.java:319) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeDestruction(InitDestroyAnnotationBeanPostProcessor.java:153) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:243) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:972) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:979) [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1006) [spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:982) [spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:901) [spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
Caused by: org.apache.http.NoHttpResponseException: 127.0.0.1:7055 failed to respond
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143) ~[httpclient-4.5.jar:4.5]
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57) ~[httpclient-4.5.jar:4.5]
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:261) ~[httpcore-4.4.1.jar:4.4.1]
    at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:165) ~[httpcore-4.4.1.jar:4.4.1]
    at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167) ~[httpclient-4.5.jar:4.5]
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272) ~[httpcore-4.4.1.jar:4.4.1]
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124) ~[httpcore-4.4.1.jar:4.4.1]
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271) ~[httpclient-4.5.jar:4.5]
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) ~[httpclient-4.5.jar:4.5]
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) ~[httpclient-4.5.jar:4.5]
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[httpclient-4.5.jar:4.5]
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ~[httpclient-4.5.jar:4.5]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71) ~[httpclient-4.5.jar:4.5]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) ~[httpclient-4.5.jar:4.5]
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:162) ~[selenium-remote-driver-2.53.1.jar:na]
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:90) ~[selenium-remote-driver-2.53.1.jar:na]
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142) ~[selenium-remote-driver-2.53.1.jar:na]
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:160) ~[selenium-firefox-driver-2.53.1.jar:na]
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:380) ~[selenium-firefox-driver-2.53.1.jar:na]
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:644) ~[selenium-remote-driver-2.53.1.jar:na]
    ... 19 common frames omitted

1 个答案:

答案 0 :(得分:0)

使用

driver.quit()

而不是

driver.close();