Using External Functions

i’m using k6 for to use it on load and smoke test on my app
i firstly started by coding the essential code in one file
now, i haved clean et refracto all my code
so, i create different folders and files
my main file k6.js launch and start the all script
i have a folder for my different scenarios, just the scenarios with her executor, duration, etc…
and i have a folder with some functions i coded before named helpers.js
for example, i haved a function called loginToken which just found the right token to complete connection on my website
in this function, i used the result from a constant like loginToken (see images)
so my problem is, when i start k6 script, it read my file helpers.js and send me an error 'TypeError: Cannot read property 'html' of undefined or null'
how can i overpass it ? because in my case, i don’t want to write it 10 times

way how i import functions:

import {
} from '../utils/helpers.js';

what is logintToken and how i export it:

export function loginToken(page) {
    var tokenToPut = "";
    var inputChildCount = page.html().find('input').size();
    if (typeof (inputChildCount) === "number") {
        let i = 0;
        while (tokenToPut === "" && i < inputChildCount) {
            if (page.html().find('input').get(i) != (null && undefined) && page.html().find('input').get(i).hasAttributes()) {
                if (page.html().find('input').get(i).getAttribute('name') === '__RequestVerificationToken') {
                    tokenToPut = page

        if (i <= 9) {
            console.log('##[command]TOKEN     [0' + i + '] # ' + tokenToPut);
        } else {
            console.log('##[command]TOKEN     [' + i + '] # ' + tokenToPut);
        return tokenToPut;

exports.loginToken = loginToken();

where i used loginToken function
you can notify i use resultGetPage as argument
it replace ‘page’ in the function

const resultGetPage = http
        .get(config.uri + '/connexion');

const formData = {
        ReturnUrl: '',
        UserName: config.USERNAME,
        Password: config.PASSWORD,
        __RequestVerificationToken: loginToken(resultGetPage)

error display:

thanks all

Hi @daftmob ,

exports.loginToken = loginToken();

is exporting the result of calling loginToken with no arguments loginToken() as loginToken not the function.

If you look at the stacktrace (and are using k6 v0.36.0) you should see that the exception happens on helpers.js line 3 which was called from line 54.

1 Like

good point, i write wrong exportation
now i just have at my end-file with no errors

exports.loginToken = loginToken;

thnaks for your fast reply

1 Like