K6 - Url redirection issue

513 views Asked by At

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();
}
2

There are 2 answers

0
Berkay Kirmizioglu On

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.

await page.waitForSelector('#elementIdOnFinalPage');

You can try to increase navigation timeout

await page.waitForNavigation({timeout: 30000});

And the third thing you may check within this code is Browser Context;

const context = browser.newContext({timeout: '120s'});

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.

0
Thiện Sinh On

If the login redirects you to a SSO page, you shouldn't use sleep method.

waitUntil: 'networkidle' do the trick for me.

export async function Login() {
  const context = browser.newContext();
  const page = context.newPage();

  try {
    await page.goto('https://my-page.example', {
      waitUntil: 'networkidle',
    });

    page.locator('#username').type('admin');
    page.locator('#password').type('admin');

    const submitButton = page.locator('#next');

    await Promise.all([
      page.waitForNavigation({ waitUntil: 'networkidle' }),
      submitButton.click(),
    ]);
  } finally {
    page.close();
  }
}