add cookie with --preScript
I'm have try several times to run a script that close pop-ups or add cookies to avoid pop-ups before run the test.
The browsertime metrics change, so it takes effect, but I want to record the video and get visualmetrics and in the screenshots I'm still viewing the pop-ups in the screen.
Script (runs with no errors):
'use strict'; module.exports = { run(context) { return context.runWithDriver((driver) => {
return driver.get('https://www.bebe.nestle.fr')
.then(() => {
const webdriver = context.webdriver;
const until = webdriver.until;
const By = webdriver.By;
driver.manage().addCookie({name: '_gasd', value: '...'});
driver.manage().addCookie({name: '_gasdasd', value: '...'});
(... and more ...)
return true;
});
})
} };
command:
$ docker run --privileged --shm-size=1g --rm -v "$(pwd)":/sitespeed.io sitespeedio/sitespeed.io --preScrit cookie.js --video --speedIndex https://www.myurl.com -n 1
I want test the load performance of a page as a user that have submit pop-ups for this reason I'm trying to add cookies in a pre script.
Could you help me?
Thanks!
Hi @L4ika hmm not sure, could see that you should chain your addCookies (you got a promise back, even though it's not chained in the docs http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_Options.html) so that the method returns when everything is finished.
Best Peter
Hello guys, I used this, and works fine for me:
module.exports = {
run(context) {
return context.runWithDriver((driver) => {
return driver.get(context.url)
.then(() => {
driver.manage().addCookie({
name: 'foo',
value: 'bar'
})
.then(function () {
driver.manage().getCookie('foo')
.then(function (cookie) {
console.log(cookie);
});
});
});
})
}
};
Or, using variable for cookie domain captured from window in URL source (from variable ou other custom script).
module.exports = {
run(context) {
return context.runWithDriver((driver) => {
return driver.get(context.url)
.then(() => {
driver.executeScript('return window.myCookieDomain')
.then(function(cookieDomain){
driver.manage().addCookie({
name: 'foo',
value: 'bar',
domain: cookieDomain
})
.then(function () {
driver.manage().getCookie('foo')
.then(function (cookie) {
console.log(cookie);
});
});
})
});
})
}
};
@soulgalore , did you can put this example in documentation ?(https://www.sitespeed.io/documentation/sitespeed.io/configuration/) It will be really cool for help who looking for hours and hours a solution to this 👍
Thank you @jeffdrumgod for sharing! I'll do that.
but this have a problem: Using the code in my prev comment, the test is cached and results it's not real. I'm testing alternatives...
The solution for me in this moment, is if I use get for a page without any static file.
Similar this example: https://www.sitespeed.io/documentation/sitespeed.io/prepostscript/#test-a-page-with-primed-cache
but, an empty page with status code 200.
...
driver.get(context.url + '/emptypage')
...
.. But still not good. This still affects the "server time" metric and slightly other metrics related to DNS and Server. :(
jeffdrumgod - In chrome to prevent the page under test being cached while setting cookies you can pass an argument to chrome that stops the browser caching, as per below
{
"browsertime": {
"browser": "chrome",
"chrome.args": "disk-cache-dir=/dev/null",
},
"utc": true
}
I haven't got the time yet to update the docs yet, will do it this weekend.
Closing, nothing more 2do here.