does not work
does not work
does not work
Tested page URL:
Test code
'use strict';
const { Selector ,ClientFunction} = require('testcafe');
module.exports = {
login : async function (cafe) {
const setCookie = ClientFunction(() => {
document.cookie = "key=value"
}).with({ boundTestRun: cafe });
await setCookie()
await cafe.navigateTo('somepage');
await setCookie();
await cafe.wait(10000)
}
}
fixture My Fixture
.page pageurl;
test('测试cookie', async t => {
await require('./tasks/logintest').login(t);
await t.wait(1000)
});
Set cookies on client side,,,
But did not succeed。。。
Hi @haoshasha,
I've created small test example and it works perfectly:
import { ClientFunction } from 'testcafe';
fixture `#1721`
.page `https://wikipedia.org/`;
test(`test`, async t => {
const setCookie = ClientFunction(() => {
document.cookie = "key=value"
});
const getCookie = ClientFunction(() => {
return document.cookie;
});
await setCookie();
await t.navigateTo('https://wikipedia.org/');
console.log(await getCookie());
});
Please try to update TestCafe to the latest version, and if it doesnt help you, change my example to reproduce the problem.
I'm closing this since there were no activity here for a long time. Feel free to reopen the issue if it's still needed.
Also, you can try to run your tests on the latest TestCafe version.
Cookies are not proxied with a 403 response. I will try to reproduce this with a small project (because I need a backend and a frontend to test this)
@javiercbk It would be great if you reproduce this with a simple example and share it with us.
Ok, I'll make a node.js app and an e2e test and publish the project in github
This test case reproduces the bug consistently. The problem arises when a cookie is created within a response of an iframe.
To reproduce this issue run the application and test it for yourself, you'll see that the cookies are shown, but when running with testcafe, the cookies are not shown. The e2e-cookies.test.js asserts this bug
@javiercbk Thanks a lot! @LavrovArtem will take a look at this
Hmm i'm getting this issue locally after adapting @churkin 's solution
Shouldn't have posted too quickly, browser needs to load the URL before setting the cookie or it will not work, working code below
const {
ClientFunction,
} = require('testcafe')
const setCookie = ClientFunction(() => {
document.cookie = 'cookiename=cookievalue'
})
module.exports = function defineSteps({ given, then }) {
given('I am logged in', async (t) => {
await t.navigateTo('http://localhost:8008/login')
await setCookie()
})
}
(wasted more time on this than i'd like to admit)
browser needs to load the URL before setting the cookie
I was having the same problem and this solved the problem for me.
Changed my test to:
fixture `List Page`
.page `http://comunicacao.local.plurall.net:5000`
test('Renders the message title', async t => {
await t
.useRole(user)
.navigateTo('http://comunicacao.local.plurall.net:5000/mensagens')
.expect(Selector('.title').withText('TÃtulo'))
})
I have removed the .page from the fixture definition, and just had to put it back.
But it makes the page renders twice. Is it possible to set the cookie before the first render?
Hi @dimascyriaco,
For now TestCafe allows to set cookie only by using ClientFunctions. So it's necessary to load the pages before setting the cookie.
But it seems you use the useRole command and doesn't set cookie directly? It seems the page directive is not required in this case. Could you please provide more details about your case (a Role initialization code, the page url if it's public)?
Sorry, forgot the role definition.
Don't know if this is the best place to put the setCookie function, but it's workind all right.
const user = Role('http://comunicacao.local.plurall.net:5000/test', async t => {
const setCookie = ClientFunction(() => {
document.cookie = 'access_token=31ac8a8bc4853d10b5b596aedb5d2df0'
})
await setCookie()
})
And since then I have removed the .page from the fixture definition.
But I think the role definition still renders the page, so we still have two pages rendering.
Sorry, forgot the role definition.
Don't know if this is the best place to put the
setCookiefunction, but it's workind all right.const user = Role('http://comunicacao.local.plurall.net:5000/test', async t => { const setCookie = ClientFunction(() => { document.cookie = 'access_token=31ac8a8bc4853d10b5b596aedb5d2df0' }) await setCookie() })And since then I have removed the
.pagefrom the fixture definition.But I think the role definition still renders the page, so we still have two pages rendering.
GJ, my bro! Same problem and solved!
For now TestCafe allows to set cookie only by using
ClientFunctions. So it's necessary to load the pages before setting the cookie.
Are there plans for TestCafe to natively handle (get, set, remove) cookies? I need to remove a server side cookie, and I haven't found a way to do this using a ClientFunction.
We don't have the feature for handling cookies natively in our plans, but you can create a corresponding suggestion and we'll consider its implementation in the future.
To delete cookies, you can set an empty value and the expires parameter to a passed date, e.g.
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";.
Make sure that you defined the path to ensure that you delete the right cookie.
If you face some other difficulties, feel free to create a new issue.
Hi Helen, I'm able to do that for all Client side cookies, however I can't interact with Server side _(httponly)_ cookies in that way. I'll create a suggestion as you mentioned, but do you have a way for me to expire Server side cookies?
As far as I understand, you need to observe the current state of the cookie key-value pairs.
It should look like the same with the browser development tools.

Â
You can remove the cookie as @helen-dikareva suggested.
Â
There is a workaround that allows you to see a list of cookies with which the request will be sent:
node_modules/testcafe-hammerhead/lib/request-pipeline/destination-request/index.js file in your IDEthis.opts.headers = (0, _headerTransforms.transformHeadersCaseToRaw)(this.opts.headers, this.opts.rawHeaders); code line (How to debug tests in IDEs).Thanks for the advice. If I'm unable to see the cookies I require _(httponly)_ while debugging, then I'll create a Stack Overflow issue with my problem.
This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests. We recommend you ask TestCafe API, usage and configuration inquiries on StackOverflow.