Getting error 'Unknown Field Options' when doing browser testing

Hi, I am following a sample script for browser testing using k6.

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

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      options: {
        browser: {
          type: 'chromium',
        },
      },
    },
  }
}

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

  try {
    await page.goto('https://test.k6.io/my_messages.php');

    page.locator('input[name="login"]').type('admin');
    page.locator('input[name="password"]').type('123');

    const submitButton = page.locator('input[type="submit"]');

    await Promise.all([
      page.waitForNavigation(),
      submitButton.click(),
    ]);

    check(page, {
      'header': page.locator('h2').textContent() == 'Welcome, admin!',
    });
  } finally {
    page.close();
  }
}

I am using version 0.46. When i run the script, I am getting issue:-
ERRO[0000] could not initialize ‘tests/testk6.js’: could not load JS test ‘file:///C:/Users//.js’: json: unknown field “options”. Any idea about that?

Hi @neha29,

Welcome to the community forum :wave:

The good news is that the script works well on my laptop with v0.46.0. Though I use a Mac and you seem to be on Windows. Is that correct? It could either be a platform/installation issue, or how you run the command.

Can you tell us how you installed and run k6? Are you using the k6 binary or a Docker image? If you can also share the command you run and some more context, we might be able to help you.

Cheers!

I am experiencing the same behaviour using v0.48.0 installed via the Windows MSI file from. I use the k6 binary to run the test and Babel and Webpack to transpile TypeScript files into JavaScript.

Script:
import { browser } from ‘k6/experimental/browser’;

// load test config, used to populate exported options object:
const testConfig = JSON.parse(open(‘./options.json’));
export const options = testConfig;

export default async function () {
const browser = new Browser();
const page = browser.newPage();

try {
    await page.goto('https://test.k6.io/');
    page.screenshot({ path: 'screenshot.png' });
} finally {
    page.close();
}

}

options.json:
{
“scenarios”: {
“ui”: {
“executor”: “shared-iterations”,
“options”: {
“browser”: {
“type”: “chromium”
}
}
}
}
}

webpack
k6 run dist/poc-test.js

Thanks for any guidance!

Hi @thebigdombowsky welcome to the community forum!

For me this two files works with k6 v0.48.0 with the k6 run script.js command.

script.js:

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

// load test config, used to populate exported options object:
const testConfig = JSON.parse(open('./options.json'));
export const options = testConfig;

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

  try {
    await page.goto('https://test.k6.io/');
    page.screenshot({ path: 'screenshot.png' });
  } finally {
    page.close();
  }
}

options.json:

{
  "scenarios": {
    "ui": {
      "executor": "shared-iterations",
      "options": {
        "browser": {
          "type": "chromium"
        }
      }
    }
  }
}

Can you confirm, that this works for you, or still fails?

Hi @bandorko,

Thanks for the quick reply!

Unfortunately, I still receive the error

“ERRO[0000] could not initialize ‘script.js’: could not load JS test ‘file:///C:/Development/performance/k6-multiprotocol/src/script.js’: json: unknown field “options””.

I wasn’t receiving the error before I upgraded to v0.48.0 using the test below

import { browser } from ‘k6/experimental/browser’

const options = {
scenarios: {
pickNewOrders: {
executor: ‘constant-vus’,
options: {
vus: 1,
duration: ‘10s’,
browser: {
type: ‘chromium’,
},
},
}
},
}

export async function pickNewOrders() {
const page = browser.newPage()
const user = Selector(‘#username’)
const password = Selector(‘#password’)
const login = Selector(‘#kc-login’)
const pickMenu = Selector(‘#medportal_pick > span.p-menuitem-text’)
const pickNewOrders = Selector(‘#medportal_new_order > span’)

try {
    await page.goto('https://example.com/'),


        await Promise.all([
            user.fill('systemadmin'),
            password.fill('max'),
            login.click(),
            page.waitForNavigation(),
        ])

    await Promise.all([
        pickMenu.isVisible(),
        pickMenu.click(),
    ])

    await Promise.all([
        pickNewOrders.isVisible(),
        pickNewOrders.click(),
        await page.waitForLoadState({ waitUntil: 'networkidle2' }),

    ])

} catch (error) {
    console.log(error)

} finally {
    await browser.close()
}

}

@thebigdombowsky :
For me there are a couple of issues with this script also, but first of all, you have to export your const options

@bandorko Please disregard the second script, as I pasted it in error. Below is what I am running and receiving the error from.

script.js

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

// load test config, used to populate exported options object:
const testConfig = JSON.parse(open('./options.json'));
export const options = testConfig;

export default async function () {
  const browser = new Browser();
  const page = browser.newPage();

    try {
        await page.goto('https://test.k6.io/');
        page.screenshot({ path: 'screenshot.png' });
    } finally {
        page.close();
    }
}

options.json

{
    "scenarios": {
    "ui": {
        "executor": "shared-iterations",
        "options": {
        "browser": {
            "type": "chromium"
        }
        }
    }
    }
}

Output

ERRO[0000] could not initialize 'script.js': could not load JS test 'file:///C:/Development/performance/k6-multiprotocol/src/script.js': json: unknown field "options"

@thebigdombowsky :
I can’t reproduce your problem. Can you give me more information?

  • what is the exact command you are running?
  • what is the output of the k6 version command?
1 Like

Hi @bandorko

Apparently, k6 0.43.1 was installed using Chocolatey, and I updated it to 0.48.0 using the MSI. I just updated the k6 package using choco install k6 --version 0.48.0, and the script executes perfectly. Thanks for your help!