gpt4 book ai didi

javascript - 在 Protractor 中设置 cookie

转载 作者:行者123 更新时间:2023-11-29 15:23:03 26 4
gpt4 key购买 nike

我正在尝试在 Protractor 测试用例中设置 cookie。我们使用 Protractor 3.3.0、Angular 1.5.x 和 Node.js 6.9.1

这是规范:

(function() {
'use strict';

describe('Dummytest', function() {
beforeEach(function() {
browser.get('./');
});

it('should set a cookie', function() {
browser.manage().addCookie("test", "fail_cookie", '/', 'localhost');
});
});
})();

这是我收到的错误信息:

  Message:    Failed: {"errorMessage":"Unable to set Cookie","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"165","Content-Type":"application/json; charset=utf-8","Host":"localhost:45556","User-Agent":"Apache-HttpClient/4.5.1 (Java/1.8.0_77)"},"httpVersion":"1.1","method":"POST","post":"{\"cookie\":{\"path\":\"/\",\"domain\":\"localhost\",\"name\":\"test\",\"httpOnly\":false,\"hCode\":3556498,\"secure\":false,\"value\":\"fail_cookie\",\"class\":\"org.openqa.selenium.Cookie\"}}","url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/68875d10-f7b4-11f6-8f37-71161903ed83/cookie"}}    Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:06:42'    System info: host: 'XXXXXXXXX', ip: '10.##.##.230', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_77'    Driver info: driver.version: unknown  Stack:    UnableToSetCookieError: {"errorMessage":"Unable to set Cookie","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"165","Content-Type":"application/json; charset=utf-8","Host":"localhost:45556","User-Agent":"Apache-HttpClient/4.5.1 (Java/1.8.0_77)"},"httpVersion":"1.1","method":"POST","post":"{\"cookie\":{\"path\":\"/\",\"domain\":\"localhost\",\"name\":\"test\",\"httpOnly\":false,\"hCode\":3556498,\"secure\":false,\"value\":\"fail_cookie\",\"class\":\"org.openqa.selenium.Cookie\"}}","url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/68875d10-f7b4-11f6-8f37-71161903ed83/cookie"}}    Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:06:42'    System info: host: 'XXXXXXXXXXXXX', ip: '10.##.##.230', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_77'    Driver info: driver.version: unknown        at WebDriverError (C:\Users\[username]\dev\project\node_modules\selenium-webdriver\error.js:27:5)

What am I doing wrong?

EDIT:

I've done some further experiments. Notice that I've changed the domain, and that there is no browser.get before the successful cookie read/write.

(function() {
'use strict';

describe('Dummytest', function() {
it('should set a cookie', function() {
browser.manage().addCookie("test", "fail_cookie", '/', '127.0.0.1');
browser.manage().getCookie('test').then(function(cookie) {
console.log('cookie test', cookie);
browser.get('./');
browser.manage().getCookie('test').then(function(cookie) {
console.log('cookie test 2', cookie);
});
});
});
});
})();

这是我得到的输出:

cookie test { path: '/',  domain: '127.0.0.1',  name: 'test',  httpOnly: false,  hCode: 3556498,  secure: false,  value: 'fail_cookie',  class: 'org.openqa.selenium.Cookie' }cookie test 2 null

如您所见,我在调用 browser.get('./') 时到达的域没有设置 cookie。如何在不触发原始问题中的错误的情况下为该域设置 cookie?

编辑 2:

发现另一个问题 ( Setting cookies before browser.get ) 说先调用 browser.driver.get,然后设置 cookie,但这样做我得到了相同的 UnableToSetCookieError正如我一直得到的那样。所以解决方案是别的。

编辑 3:

关于半相关说明,我一直很难找到 addCookie 函数及其参数的文档。特别是现在,因为在最新(截至 2016-01-31)版本的 Protractor 中,它需要一个对象而不是参数列表。但我终于找到了它,并认为我不妨在此处添加一个链接:https://github.com/SeleniumHQ/selenium/blob/022644c47c643ce6fe797d65e074329190129c1b/javascript/node/selenium-webdriver/lib/webdriver.js#L1075

编辑 4:

越来越近了!将域设置为 .localhost(注意开头的点 (.))似乎可行。将做进一步调查并报告。与此同时,如果有人能告诉我为什么这个点很重要以及为什么那个确切的域有效,我将非常感激 :)

最佳答案

在我的 protractor.conf.js 中将 baseUrl 设置为 127.0.0.1 似乎解决了我一直面临的类似问题。

关于javascript - 在 Protractor 中设置 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41958226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com