Existing browser context must be closed before creating a new one k6 0.46

Hi there,

I’m getting this error Uncaught (in promise) GoError: new page: existing browser context must be closed before creating a new one with the new k6 update 0.46.0 and I performed migration according to this article.

I don’t understand why, here’s the test suite:

I have scenarios.js with:

export let options = {
...
customscenario: {
    executor: 'per-vu-iterations',
    options: {
      browser: {
        type: 'chromium',
      },
    },
...
  },
...
};

and also a set of tests like this:

export async function customScenario() {
  await firstTest();
  await secondTest();
...
}

…and performed migration so the tests look like this now:

...
import { browser } from 'k6/experimental/browser';
...

export async function firstTest() {
  const page = browser.newPage();

  try {
    // test script...
 } finally {
    page.close();
  }
}

export default async function firstTestMethod() {
  await firstTest();
}

So, as you can see I should have it all ready and it worked before migration to 0.46.0, but now it says that the browser context is not closed, that I must close it before opening the secondTest(); and more…

I don’t have anymore browser.close(); in finally but that’s okay since 0.46.0.

I want all my tests under customScenario() above to be executed separately without this error, as before migration. Don’t know what I’m missing, but I did everything according to migration.

Hi @qaengineer,

Unless you’re sharing the same BrowserContext, there shouldn’t be an issue.

Have you seen our multi-scenario example? This example show the correct usage. Each scenario function has its own browser and BrowserContext. They don’t share it between themselves.

More detail on your test script might give us more idea.

Thanks.

Thanks for the reply!

I have one scenarios.js file with multiple scenarios inside, but the tests are written in separate files and just called/imported into scenarios.js file.

Beginning and end are the same for every test file, it is like going to some page and logging then performing tests.

I understand that with every test the browser context is automatically started but why in the end of every test I have browser.close() when it doesn’t really work to close the context and let the next test start new context? I see the error that the browser context is not closed first so can’t start new one.

I checked the multi-scenario example you’ve shared and I have the same in my test suite, just the difference is that I have tests separately in their own .js files and not inside a single scenarios.js file.

Hi, @qaengineer,

I understand that with every test the browser context is automatically started but why in the end of every test I have browser.close() when it doesn’t really work to close the context and let the next test start new context?

You don’t need to have browser.close at the end of every test, it’s automatically closed per iteration. And you don’t have to close BrowserContexts unless you open them yourself.

Please make sure to check our migration guide.

I hope this helps.

Hi @inancgumus
I am sure I followed the migration guide and still the issue is there.
To confirm what’s issue, according to error issue is with browser being still open and not closed while performing another test.

Have no idea how to debug this in order to see if it is related to specific test suite setup or something else, but the code is above… scenarios.js file with options and set of tests. Then the tests in separate .js files. I am sure the test suite worked before migration to 0.46.0. This is something within the new feature of automatically opening and closing browser context.

Hi @qaengineer,

Could you provide us with similar test scripts and HTML pages so we can reproduce the issue?

Thanks.

Of course @inancgumus
Here are the files you can use for local testing. You can ignore k6 cloud and project name vars or set your own.

You just run ./k6 scenarios.js and you will see the error GoError: new page: existing browser context must be closed... that means only the first test was ran and unable to run secondTest.

Download here.

Hi @qaengineer,

Sorry for my late reply. I was out of the office.

The problem is the test script creates concurrent browser contexts, which is not allowed:

export async function firstTest() {
  const page = browser.newPage();
  // ...
}

export async function secondTest(context) {
  const page = context.newPage();
  // ...
}

See the migration guide and documentation for more details:

  1. Migrating to k6 v0.46
  2. newPage([options])
  3. context()

So, the solution is to close the browser context when a test finishes:

export async function firstTest() {
  const page = browser.newPage();

  try {
    // ... same
  } finally {
    page.close();
    browser.context().close(); // <--
  }
}

export async function secondTest() {
  const page = browser.newPage();

  try {
    // ... same
  } finally {
    page.close();
    browser.context().close(); // <--
  }
}

browser.newPage creates a new page in a new context. That’s why we should close the other context before calling newPage. The first test will create a new page in a new context and then close the context when it’s done. Then the second test will run and do the same.

Hope this helps.

2 Likes

Thanks so much! Now it is running normally.

1 Like

Nice to hear! Happy to help :slight_smile:

I saw one solution about adding ‘bypassCSP:true’ to the browser context, I was wondering if we have option to add any config/setting in the browser context before creating the browser instance/initializing it ?
The solution I’m referring here is: Locating elements within an iframe - #6 by minhhd

Hi @yogitalakhi18,

There is no option to set this at the browser initialization step. I don’t know if it will help, but, you might want to pass these options from the command-line:

K6_BROWSER_ARGS="disable-web-security,disable-site-isolation-trials" k6 run script.js

Hope this helps.

@inancgumus Unfortunately this didn’t work for me. :frowning:
But thank you for coming back.