1
votes

When I run my selenium tests using ChromeDriver in headless mode addCookie method calls cause exception; the tests all work in normal mode

My Chrome Browser version is 75.0.3770.90 and ChromeDriver version is 75.0.3770.8. The OS is Mac OS X 10.14.5

The exception I'm getting is:

    org.openqa.selenium.UnableToSetCookieException: unable to set cookie
    (Session info: headless chrome=75.0.3770.90)
    Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
    System info: host: 'A0353.local', ip: 'fe80:0:0:0:84c:de05:352a:4bcc%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.5', java.version: '1.8.0_45'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities {acceptInsecureCerts: false, browserName: chrome,         browserVersion: 75.0.3770.90, chrome: {chromedriverVersion: 75.0.3770.8 (681f24ea911fe7..., userDataDir: /var/folders/nc/gqkjhl8920j...}, goog:chromeOptions: {debuggerAddress: localhost:63795}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
    Session ID: db1528ca16e944bb6596563337e1ba40
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions.addCookie(RemoteWebDriver.java:682)
    at com.anatwine.testing.common.selenium.PageObject.addCookie(PageObject.java:187)
    at com.anatwine.testing.component.page.landingpage.LandingPage.addUsernameAndRolesCookies(LandingPage.java:54)
    at com.anatwine.testing.component.steps.portallandingpage.RoleBasedAuthorisationSteps.lambda$new$347(RoleBasedAuthorisationSteps.java:32)
    at com.anatwine.testing.component.steps.portallandingpage.RoleBasedAuthorisationSteps$$Lambda$494/785340693.accept(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at cucumber.runtime.Utils$1.call(Utils.java:26)
    at cucumber.runtime.Timeout.timeout(Timeout.java:16)
    at cucumber.runtime.Utils.invoke(Utils.java:20)
    at cucumber.runtime.java8.Java8StepDefinition.execute(Java8StepDefinition.java:112)
    at cucumber.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:50)
    at cucumber.runner.TestStep.executeStep(TestStep.java:63)
    at cucumber.runner.TestStep.run(TestStep.java:49)
    at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:43)
    at cucumber.runner.TestCase.run(TestCase.java:44)
    at cucumber.runner.Runner.runPickle(Runner.java:40)
    at cucumber.runtime.Runtime$1.run(Runtime.java:84)
    at cucumber.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:220)
    at cucumber.runtime.Runtime.run(Runtime.java:81)
    at cucumber.api.cli.Main.run(Main.java:26)
    at cucumber.api.cli.Main.main(Main.java:8)
    at ✽.A user has 'ROLE_LANDING,ROLE_BRAND_ORDERSIM' roles(feature/component/portal-landingpage/PortalLandingPageRoleAuthorisations.feature:12)

The code causing the problem is:

    driver.get(initialPageUrl);    
    Cookie cookie = new Cookie(cookieName, encode(cookieValue, "UTF-8"), "localhost","/", null);
    driver.manage().addCookie(cookie);
2

2 Answers

1
votes

Probably you added the cookie before navigating to the site. You should first navigate to your url

driver.get("yourWebPage");

and only after this add cookies

driver.manage().addCookie(cookie);

For a better analysis of what is going wrong, if above won't help, you have to add more code of your program.

Also maybe duplicate of this.

0
votes

It looks in the headless mode you MUST have protocol in the page url while in the normal mode the web driver defaults to http. So once we replaced "localhost:8080/path" with "http://localhost:8080/path" it worked.