I am having trouble with the redirection page during a browser test. Despite the sleep(10) and all the page.waitForNavigation() when I finally ask for page.title() the output is not the final page’s title but one of the previous pages. Also, if I remove the sleep(10) the browser is not waiting for the full redirection despite the page.waitForNavigation() and closes. Thanks!!
import { chromium, Frame } from 'k6/experimental/browser';
import { check, sleep } from 'k6';
export const options = {
maxRedirects: 20,
scenarios: {
browser: {
executor: 'shared-iterations',
exec: 'browser',
vus: 1,
maxDuration: '1m',
},
},
};
export async function browser() {
const browser = chromium.launch({ headless: false ,timeout: '120s',slowMo:'500ms'});
const context = browser.newContext();
let page = context.newPage();
await page.goto('http://localhost:5000/', { waitUntil: 'networkidle' });
const cookieButton = await page.locator('//*[@id="cookies-banner"]/div/div[2]/button');
await cookieButton.click();
await page.locator('#company-input').type('Test1');
const continueButton = await page.locator('#company-login-continue-button');
await Promise.all([
page.waitForNavigation(),
continueButton.click(),
]);
sleep(1);
const loginButton = await page.locator('//*[@id="login-method-selection-password-login-button"]/p');
await Promise.all([
page.waitForNavigation(),
loginButton.click(),
]);
sleep(1);
await page.locator('#user-name-input').type('TestUsername');
await page.locator('#password-input').type('TestPassword');
const continueButton2 = await page.locator('//*[@id="password-login-continue-button"]/p');
await Promise.all([
page.waitForNavigation(),
continueButton2.click(),
]);
sleep(10)
console.log(page.title())
await page.close();
await browser.close();
}
I think the issue is much more than redirection; it's related with page timeouts and waits.
So you could try the belows;
Instead of waiting for navigation, wait for a specific element that you know will be present on the final page. This way, you can be sure that the navigation is complete.
You can try to increase navigation timeout
And the third thing you may check within this code is Browser Context;
While you've set the browser timeout, you can also set a timeout for the browser context, ensuring that the context also has an extended timeout.