0

I have set up a small test suite with geb, everything is working fine with chrome and firefox, but now i wanted to extend the browsers i execute the tests on with IE and the tests always fail because of remaining cookies from previous tests (I'm still logged in after executing the login test)

To load the IEDriver i use the webdrivermanager from bonigarcia which executes Version 2.53.1.0 of InternetExplorerDriver.

Snippet from my GebConfig:

environments {
    ie {
        InternetExplorerDriverManager.getInstance().setup(Architecture.x32)
        driver = { new InternetExplorerDriver() }
    }

My test setup looks like this:

def setup() {
    resetBrowser()
    Database.resetDatabase()
}

The method resetBrowser() is from Geb and does this:

 void resetBrowser() {
    if (_browser?.config?.autoClearCookies) {
        _browser.clearCookiesQuietly()
    }
    _browser = null
}

And here's my cleanup (also tried it without it, didn't change anything):

def cleanup() {
    CachingDriverFactory.clearCacheAndQuitDriver()
}

resetBrowser() should delete the cookies, but I read, that only cookies from the current domain will be deleted. So I thought adding something like browser.clearCookies(browser.baseUrl) or browser.clearCookies("http://localhost:8080/") before, after or instead of resetBrowser() should work. But as soon as I add this line, the IEDriver stops working with the following exception (same exception is thrown when i use driver.manage().deleteAllCookies()):

org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'MiiKEs', ip: '192.168.0.123', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_66'
Driver info: driver.version: RemoteWebDriver
Capabilities [{browserAttachTimeout=0, ie.enableFullPageScreenshot=true, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.forceShellWindowsApi=false, pageLoadStrategy=normal, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:21317/, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss}]
Session ID: 75c52825-b9e6-4876-a1f9-454b450b839e

    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:665)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:701)
    at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions.deleteAllCookies(RemoteWebDriver.java:773)
    at TestSpec.register test(TestSpec.groovy:84)
Caused by: org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:21317 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'MiiKEs', ip: '192.168.0.123', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_66'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:91)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:644)
    ... 3 more
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:21317 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:144)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:90)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    ... 4 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
    ... 17 more
Boni García
  • 4,618
  • 5
  • 28
  • 44
Michael K.
  • 2,392
  • 4
  • 22
  • 35
  • The error you're seeing might be down to the fact that you are quitting the driver after each test which usually is not necessary and wasteful as it means a new browser has to be started up for each test. May I ask why do you call `CachingDriverFactory.clearCacheAndQuitDriver()` from `cleanup()`? Quitting the driver will clean all cookies as the new browser will have a new, clean session so both quitting and then manually cleaning the cookies is not necessary either. Where exactly do you place the `browser.clearCookies()` calls? In `cleanup()` or in `setup()`? Or somewhere entirely different? – erdi Apr 10 '16 at 16:22
  • ```CachingDriverFactory.clearCacheAndQuitDriver()``` is not a problem, at least it does not cause this exception, I have also ran the tests without it -> same behaviour! ```resetBrowser()``` is a method from geb (I'll add it to the question). But what you write is only partly true. Only in Chrome and Firefox I have a clean session after ```quit()``` or ```cleanCookies()```! IE always remembers the previous session! :-/ – Michael K. Apr 11 '16 at 07:20
  • And I tried to place ```browser.clearCookies()``` before, after and instead ```resetBrowser()``` – Michael K. Apr 11 '16 at 07:26
  • Do I maybe have to clean localStorage separately? – Michael K. Apr 11 '16 at 07:27
  • Doh.. just realized who you are *facepalm* Sorry for explaining resetBrowser ;) – Michael K. Apr 11 '16 at 07:30
  • How does the fact that you don't have a clear session in IE manifest itself? Did you double check that the cookies are not getting cleared? Is your test gathering cookies across more than one domain? – erdi Apr 11 '16 at 07:47
  • I'm still logged in. Normally I should get forwared to the loginpage when I open localhost:8080 but in IE the mainpage opens and I'm logged in as the previously created used user. – Michael K. Apr 11 '16 at 08:04
  • I just checked the cookies in IE while putting a breakpoint on the spot where i expect to have a clean session. No cookies for localhost are there, but there is still the session-token in localStorage! So I don't have a problem with cookies, it's because of localStorage that is not cleaned in IE – Michael K. Apr 11 '16 at 08:11
  • 1
    There you go. Sometimes it pays off to double check what the real issue really is. There is no code in Geb that deals with clearing local storage but apparently in latest versions in WebDriver there is a WebStorage interface which provides ability to interact with local storage. Feel free to submit issues against Geb to: expose clearing local storage functionality via a method on browser and clear local storage together with clearing cookies as part of the test lifecycle. – erdi Apr 11 '16 at 15:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108846/discussion-between-michael-k-and-erdi). – Michael K. Apr 11 '16 at 17:49
  • 1
    I took care of adding the [Geb ticket](https://github.com/geb/issues/issues/472). – kriegaex Feb 20 '17 at 12:00

1 Answers1

1

My Problem was not, that the cookies are not deleted, it was that LocalStorage is not cleaned!

To clean LocalStorage Geb doesn't offer any functionality yet, but this can be achieved with the following method which can be called in the setup() routine:

def clearLocalStorage() {
    //first navigate to any page within the domain to which you want to clear the LocalStorage
    // baseUrl can be set in
    go(browser.baseUrl) 
    js.exec("localStorage.clear()") // and then just clear it
}

The suggestion to use the WebStorage interface of WebDriver is as far as I anderstood not possible till InternetExplorerDriver does not implement this interface!

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Michael K.
  • 2,392
  • 4
  • 22
  • 35
  • 1
    FYI, [Geb ticket 472](https://github.com/geb/issues/issues/472) about adding support for local storage (for those WebDrivers supporting it) has just been closed with [this commit](https://github.com/geb/geb/commit/f35a0401d8075d1cc707df56cc00acfd8f4342c8). – kriegaex Jul 21 '18 at 03:38