Hello community,
I need to execute log in on my webpage using k6 browser and get access_token and refresh_token as response from “…/oauth2/v2.0/token” endpoint.
Is it possible to listen to all HTTP request / response while k6 Browser works (like in Network tab of Chrome Dev mode) and output data to console without interrupting the browser flow?
In the next step, I will use access_token and refresh_token in k6 normal testing script (not the browser).
The problem is, I can’t recreate authentication process using normal testing script in k6, that’s why I had idea to use k6 browser to get access_token and refresh_token.
import { browser } from 'k6/experimental/browser';
import { sleep } from 'k6';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
let tokens = {};
export default async function () {
const page = browser.newPage();
try {
console.log('Navigating to main page');
await page.goto('https://website.com/');
console.log('Navigation main page successful');
await sleep(0.5);
await page.mouse.click(380, 650);
console.log('Performed click at coordinates');
await page.waitForNavigation({ waitUntil: 'load' });
console.log('Navigation to the next page completed');
await sleep(0.5);
await page.locator('input[name="email"]').type('www');
await sleep(0.5);
await page.locator('input[name="password"]').type('eee');
await sleep(0.5);
await page.mouse.click(385, 340);
console.log('Performed click at coordinates');
//I want to get list of all response headers in console
//Need to get access_token and refresh_token from response "token"
await page.waitForNavigation({ waitUntil: 'load' });
console.log('Navigation main page successful and page fully loaded');
// Check if tokens are available
if (tokens.access_token && tokens.refresh_token) {
console.log('Tokens successfully retrieved');
} else {
console.error('Failed to retrieve tokens');
}
await sleep(20);
} catch (error) {
console.error('Error during navigation:', error.message, error.stack);
} finally {
// Always close the page after operations are done
await page.close();
console.log('Browser closed');
}
}