I'm trying to write bare bones unit tests using selenium-webdriver
. In order to correctly test my application, I need to prepare each test case with a cookie.
I am trying to do the following:
it('should set cookies', async function() {
const driver = new webdriver.Builder().forBrowser('chrome').build();
driver.manage().window().setSize(800, 600);
await driver.manage().addCookie({
name: 'KEY',
value: 'COOKIE-VALUE',
domain: '.my.domain',
path: '/',
secure: false
});
await driver.get('');
// test following....
});
My tests run (i.e. calling addCookie
does not throw/reject), but the set cookie values will not be used when the browser makes a request against my application.
When I try to log the set cookies like:
await driver.manage().addCookie({
name: 'KEY',
value: 'COOKIE-VALUE',
domain: '.my.domain',
path: '/',
secure: false
});
const set = await driver.manage().getCookies();
console.log('set', set);
it will tell me that no cookies have been set:
set []
I also dug into the sources of selenium-webdriver
and able to use the debugger to find out that the correct cookie string will be constructed and enqueued here.
How do I correctly add a cookie so that it will be used by the driver instance?
For those wondering about async/await
, I have disabled the managed promises using:
webdriver.promise.USE_PROMISE_MANAGER = false;
I'm trying to write bare bones unit tests using selenium-webdriver
. In order to correctly test my application, I need to prepare each test case with a cookie.
I am trying to do the following:
it('should set cookies', async function() {
const driver = new webdriver.Builder().forBrowser('chrome').build();
driver.manage().window().setSize(800, 600);
await driver.manage().addCookie({
name: 'KEY',
value: 'COOKIE-VALUE',
domain: '.my.domain',
path: '/',
secure: false
});
await driver.get('http://test.my.domain');
// test following....
});
My tests run (i.e. calling addCookie
does not throw/reject), but the set cookie values will not be used when the browser makes a request against my application.
When I try to log the set cookies like:
await driver.manage().addCookie({
name: 'KEY',
value: 'COOKIE-VALUE',
domain: '.my.domain',
path: '/',
secure: false
});
const set = await driver.manage().getCookies();
console.log('set', set);
it will tell me that no cookies have been set:
set []
I also dug into the sources of selenium-webdriver
and able to use the debugger to find out that the correct cookie string will be constructed and enqueued here.
How do I correctly add a cookie so that it will be used by the driver instance?
For those wondering about async/await
, I have disabled the managed promises using:
webdriver.promise.USE_PROMISE_MANAGER = false;
Share
Improve this question
edited Sep 19, 2017 at 13:56
Gokul
8312 gold badges13 silver badges30 bronze badges
asked Sep 18, 2017 at 14:30
m90m90
11.8k15 gold badges67 silver badges119 bronze badges
1 Answer
Reset to default 4The method addCookie
add a cookie to the current domain, so you'll first have to navigate to the targeted URL to set the domain:
await driver.get('http://test.my.domain');
await driver.manage().addCookie({
name: 'KEY',
value: 'COOKIE-VALUE',
domain: 'test.my.domain',
path: '/',
secure: false
});
https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie