WebDriverError:无法设置cookie javascript

时间:2018-02-01 11:59:48

标签: javascript cookies protractor

我试图在Protractor中设置一个cookie而不允许我。

这是错误:

    WebDriverError: unable to set cookie
  (Session info: chrome=63.0.3239.132)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64)
    at WebDriverError (e:\node_modules\selenium-webdriver\lib\error.js:27:5)
    at Object.checkLegacyResponse (e:\node_modules\selenium-webdriver\lib\error.js:505:15)
    at parseHttpResponse (e:\node_modules\selenium-webdriver\lib\http.js:509:13)
    at doSend.then.response (e:\node_modules\selenium-webdriver\lib\http.js:440:13)
    at process._tickCallback (internal/process/next_tick.js:109:7)

以下是负责设置cookie的测试代码

beforeEach(function() {
        //Enable Angular synchonization
        protractor_1.browser.waitForAngularEnabled(true);
        //Disable animations
        protractor_1.browser.manage().addCookie({ name: 'disableAnimations', value: 'true' }, '/', '127.0.0.1');
    });

为什么我的测试代码无法设置cookie?

1 个答案:

答案 0 :(得分:5)

如果那是完整代码 - 我只能建议您必须先打开要添加Cookie的域的页面,例如:

var popup = window.open("http://x.x.x.x.");
popup.document.open();
popup.document.write('<script src="http://x.x.x.x/file.js" type="text/javascript"></script>');
popup.document.close();

Selenium只能为已打开的域名页面设置Cookie