Skip to content

Commit

Permalink
Cookie in Bidi for Firefox 125
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore committed Mar 8, 2024
1 parent 7787402 commit 6780947
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
1 change: 0 additions & 1 deletion lib/extensionserver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class ExtensionServer {
options.requestheader ||
options.block ||
options.basicAuth ||
options.cookie ||
options.clearCacheKeepCookies)
? true
: false;
Expand Down
10 changes: 0 additions & 10 deletions lib/extensionserver/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ function generateURL(port, testUrl, options) {
query.ba = options.basicAuth + '@' + testUrl;
}

if (options.cookie) {
const cookies = toArray(options.cookie);
query.cookie = [];
for (const cookie of cookies) {
const name = cookie.slice(0, Math.max(0, cookie.indexOf('=')));
const value = cookie.slice(Math.max(0, cookie.indexOf('=') + 1));
query.cookie.push(name + '@' + value + '@' + testUrl);
}
}

return format({
protocol: 'http',
hostname: '127.0.0.1',
Expand Down
30 changes: 30 additions & 0 deletions lib/firefox/firefoxBidi.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import intel from 'intel';
import { toArray } from '../support/util.js';
const log = intel.getLogger('browsertime.firefox.bidi');

export class FirefoxBidi {
Expand All @@ -22,4 +23,33 @@ export class FirefoxBidi {
log.error('Could not inject JavaScript:' + error);
}
}

async setCookie(url, cookie) {
const cookies = toArray(cookie);
for (let cookieParts of cookies) {
const parts = new Array(
cookieParts.slice(0, cookieParts.indexOf('=')),
cookieParts.slice(cookieParts.indexOf('=') + 1, cookieParts.length)
);

const params = {
method: 'storage.setCookie',
params: {
cookie: {
name: parts[0],
value: {
type: 'string',
value: parts[1]
},
domain: new URL(url).hostname
}
}
};
try {
await this.bidi.send(params);
} catch (error) {
log.error('Could not set cookie:' + error);
}
}
}
}
8 changes: 7 additions & 1 deletion lib/firefox/webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class Firefox {
/**
* Before each URL/test runs.
*/
async beforeEachURL(runner) {
async beforeEachURL(runner, url) {
await runner.runPrivilegedScript(`
new Promise(async function(resolve) {
await Services.fog.testFlushAllChildren(); // force any data that wasn't recorded yet to be immediately put in the buffers
Expand Down Expand Up @@ -126,6 +126,12 @@ export class Firefox {
await this.geckoProfiler.start();
}

if (this.options.cookie && url) {
await this.bidi.setCookie(url, this.options.cookie);
} else if (this.options.cookie) {
log.info('Could not set cookie because the URL is unknown');
}

if (this.firefoxConfig.perfStats) {
this.perfStats = new PerfStats(runner, this.firefoxConfig);
return this.perfStats.start();
Expand Down

0 comments on commit 6780947

Please sign in to comment.