2016-09-19 32 views
0

集成WebDriver,這是Spring運行的項目,用於測試系統。對於每個視圖,測試分爲5個步驟(登錄,輸入字段,搜索結果,有效結果,更改查找和刪除一個結果)。Spring 4集成WebDriver AbstractTestNGSpringContextTests

項目

  • 春4.2.4.RELEASE
  • 硒的Java 2.53.1
  • TestNG的6.9.10

第一個測試OK,在運行時的主要成分打開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 

回答

0

使用

driver.quit() 

,而不是

driver.close(); 
+0

我」米改變你的建議,它在部分工作,我們想要觀看測試,那麼當你打開瀏覽器時,我點擊它,然後什麼時候會接近相同的錯誤,如果沒有在瀏覽器中單擊確定。 –

+0

我現在改變chromeDriver。 –