7

I tried to set a cookie programmatically and PhantomJSDriver rejects that cookie even if cookie belongs to the current domain. The error message is very generic not revealing much details:

Caused by: org.openqa.selenium.UnableToSetCookieException: {"errorMessage":"Unable to set Cookie",...

Same command works with FirefoxDriver and ChromeDriver (see test cases to reproduce below).

  1. Is there some workaround or fix for that?
  2. How can I troubleshoot the problem better, e.g., get more informations about rejection reason from PhantomJS?

I'm using PhantomJSDirver 1.2.1 with phantomjs 2.2.1

import org.openqa.selenium.Cookie;
import org.openqa.selenium.InvalidCookieDomainException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

import java.util.function.Supplier;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.startsWith;

public class CookiesTest {


    @Test(dataProvider = "webDrivers")
    public void shouldAcceptCookieForCurrentDomain(String type, Supplier<WebDriver> drivers) {
        WebDriver driver = drivers.get();

        driver.get("http://www.bbc.com");
        assertThat(driver.getCurrentUrl(), startsWith("http://www.bbc.com"));
        driver.manage().addCookie(
                new Cookie.Builder("MYCOOKIE", "value").domain("www.bbc.com").build());
        assertThat(driver.manage().getCookieNamed("MYCOOKIE").getValue(), equalTo("value"));

        driver.close();
    }

    @Test(dataProvider = "webDrivers", expectedExceptions = InvalidCookieDomainException.class)
    public void shouldRejectedCookieForDifferentDomain(String type, Supplier<WebDriver> drivers) {
        WebDriver driver = drivers.get();

        driver.get("http://www.bbc.com");
        assertThat(driver.getCurrentUrl(), startsWith("http://www.bbc.com"));
        driver.manage().addCookie(
                new Cookie.Builder("MYCOOKIE", "value").domain("different.domain.com").build());

        driver.close();
    }

    @DataProvider
    public Object[][] webDrivers() {
        System.setProperty("webdriver.chrome.driver", "/Users/mgawinec/dev-tools/chromedriver/chromedriver");

        Supplier<WebDriver> phantomJSDrivers = () -> new PhantomJSDriver();
        Supplier<WebDriver> firefoxDrivers = () -> new FirefoxDriver();
        Supplier<WebDriver> chromeDrivers = () -> new ChromeDriver();
        return new Object[][]{
                {"phantomJS", phantomJSDrivers},
                {"firefox", firefoxDrivers},
                {"chrome", chromeDrivers}};
    }

}

2 Answers 2

1

I was running into the same problem, and I got it to work by adding the cookies with javascript.

JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript("document.cookie = \"cookieName=cookieValue;path=/;domain=www.bbc.com\"");

I am running PhantomJS version 2.1.1

0

The latest development version of PhantomJS (2.2.0-development) contains a fix for this problem.

Since PhantomJS releases are oncea year and I couldn't wait that much time I downloaded their sources and compiled them (they do not provide daily builds, aka snapshots).

Release of PhantomJS 2.2: https://github.com/ariya/phantomjs/issues/13937

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.