我试图在量角器测试案例中设置一个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)
我做错了什么?
修改
我做了一些进一步的实验。请注意,我已经更改了域名,并且在成功读取/写入cookie之前没有browser.get
。
(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)版本的量角器中,它需要一个对象而不是参数列表。但我终于找到了它,并认为我可以在这里包含一个链接:https://github.com/SeleniumHQ/selenium/blob/022644c47c643ce6fe797d65e074329190129c1b/javascript/node/selenium-webdriver/lib/webdriver.js#L1075
编辑4:
越来越近了!将域设置为.localhost
(注意开头的点(。))似乎有效。将做进一步的调查和报告。与此同时,如果有人能说出为什么点是重要的以及为什么这个确切的域名有效,我将非常感激:)
答案 0 :(得分:1)
在我的protractor.conf.js中将baseUrl设置为127.0.0.1似乎解决了我一直面临的类似问题。