Browser testing with k6

Hi everyone!! I’m new to k6. I just tried implementing the below code but I am getting ERRO[0000] GoError: To run browser tests set env var K6_BROWSER_ENABLED=true
at go.k6.io/k6/js.(*InitContext).Require-fm (native)
at file:///C:/k6-learning/script.js:1:0(12) hint=“script exception”.
How to solve this, what am I missing???

Hi @Sreyaa,

The environment variable should be set when running the k6 command. Please see the following steps to solve the issue.

  1. Please remove these lines from your script:

    const res = `${____ENV.K6_BROWSER_ENABLED}`;
    res = true;
    
  2. Then run the k6 command with the K6_BROWSER_ENABLED environment variable set:

    K6_BROWSER_ENABLED=true k6 run script.js
    

Please let us know how it goes.

Thanks.

PS: If you’re on a Windows machine, you might also want to try the following:

set "K6_BROWSER_ENABLED=true" && k6 run script.js

No, It is showing K6_BROWSER_ENABLED=true : The term ‘K6_BROWSER_ENABLED=true’ is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1

But, it is working fine after I remove these lines(const res = ${____ENV.K6_BROWSER_ENABLED};
res = true;) and run it with this command
k6 run --env K6_BROWSER_ENABLED=true script.js

my code not launching the browser and getting error - ERRO[0030] Uncaught (in promise) GoError: filling “//input[@id=‘usernameUserInput’]” with “ship-builder2@ship-builder.org”: timed out after 30s
ui [at github.com/grafana/xk6-browser/common.(*Locator).Fill-fm (native)
at file:///C:/K6-Scripts/specs/browserTestExample.js:31:22(26) executor=shared-iterations scenario=ui

my program as below -
import { browser } from ‘k6/experimental/browser’;
import { check, sleep } from ‘k6’;
//import { htmlReport } from “K6 Load Test: <%= title %>”;

export const options = {
scenarios: {
ui: {
executor: ‘shared-iterations’,
options: {
browser: {
type: ‘chromium’,
},
},
},
},
thresholds: {
checks: [‘rate==1.0’],
},
};
export default async function () {

const page = browser.newPage();

try {
    await page.goto('https://stage-lcm.ehs.edison.gehealthcare.com/');

    const username = page.locator("//input[@id='usernameUserInput']");
    username.click();
    username.fill('ship-builder2@ship-builder.org');

    const conbutton=page.locator("//button[@data-cy='Continue']");
    await conbutton.click();
    sleep(10)
    
    const passwordButton=page.locator("//input[@id='password']");
    //await passwordButton.clear();
    await passwordButton.fill('ShipBuilder@123');

    const submitbtn=page.locator("//button[@type='submit']")
     await submitbtn.click();

     sleep(5)


   await page.screenshot({ path: 'screenshots/screenshot.png' });
    
} finally {
    page.close();
}

}
// export function handleSummary(data) {
// return {
// “summary.html”: htmlReport(data),
// };
//}

running with below command-
PS C:\K6-Scripts\specs> k6 run --env K6_BROWSER_ENABLED=true .\browserTestExample.js

I got the solution - now working by help of below command -
$env:K6_BROWSER_HEADLESS=“false” ; $env:K6_BROWSER_TIMEOUT=‘60s’ ; k6 run .\browserTestExample.js

@dsingh4it

Using sleep in a production test script is not a good idea, because it is not reliable. Sometimes the event may not happen during 10 seconds. Sometimes you wait unnecessary long for the event.
I think you should use the page.waitFor… functions instead.

1 Like