Hello,
I have an issue with a script I am working on. The beginning of the script is an API request, then I switch to using the browser. When I run the script unit-wise, I never encounter the error. However, when I run 25 iterations on 5 virtual users, I often get errors like this:
panic: runtime error: index out of range [5] with length 0 [recovered]
running panic: runtime error: index out of range [5] with length 0
ui [==============>-----------------------] 5 VUs 00m44.9s/10m0s 10/25 shared iters
goroutine 27702 [running]:
github.com/dop251/goja.(*vm).handleThrow(0xc00055e000, {0x1a07c60, 0xc0096e6870})
github.com/dop251/goja@v0.0.0-20240610225006-393f6d42497b/vm.go:788 +0x405
github.com/dop251/goja.(*vm).runTryInner.func1()
github.com/dop251/goja@v0.0.0-20240610225006-393f6d42497b/vm.go:830 +0x3f
panic({0x1a07c60?, 0xc0096e6870?})
runtime/panic.go:770 +0x132
Do you have any ideas? I’ve tried a lot of things today. Here’s my script:
import http from 'k6/http';
import { browser } from 'k6/browser';
import { check, sleep } from 'k6';
import { Trend } from 'k6/metrics';
import { Counter } from 'k6/metrics';
import { Rate } from "k6/metrics";
import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js';
// Configuration of execution mode
function getOptions(mode) {
switch (mode) {
case 'shared-iterations':
return {
scenarios: {
ui: {
executor: 'shared-iterations',
exec: 'callEasySignO2S',
vus: 5,
iterations: 25,
options: {
browser: {
type: 'chromium',
headless: "new"
}
}
}
}
};
case 'dev':
return {
scenarios: {
browser: {
executor: 'shared-iterations',
exec: 'callEasySignO2S',
vus: 1,
iterations: 1,
options: {
browser: {
type: 'chromium',
headless: false
}
}
}
}
};
default:
throw new Error("Invalid mode. Please pass the EXECUTOR_MODE option: <name>");
}
}
const mode = __ENV.EXECUTOR_MODE || 'shared-iterations';
export const options = getOptions(mode);
export async function script() {
const page = await browser.newPage();
try {
await page.goto(signatureURL, { waitUntil: 'load' });
await page.setViewportSize({ width: 1920, height: 1080 });
await page.waitForLoadState('load');
await page.waitForLoadState('domcontentloaded');
await page.click('button#js--zoom-out-btn');
await page.evaluate(() => window.performance.mark('debut'));
await page.click('button#js--zoom-out-btn');
await page.click('button#js--zoom-out-btn');
await page.click('button#js--zoom-out-btn');
await page.click('button#js--zoom-out-btn');
await page.click('button#js--zoom-in-btn');
await page.click('button#js--zoom-in-btn');
await page.click('button#js--zoom-in-btn');
await page.waitForFunction(() => {
const button = document.querySelector('button.js-btn-sign');
return button && !button.hasAttribute('disabled');
}, { timeout: 30000 });
await page.click('.btn.btn-primary.js-btn-sign.mx-auto.btn-large.js-btn');
await page.waitForLoadState('load');
await page.waitForLoadState('domcontentloaded');
await page.click('input#agreementCheckbox-contraliaCGU');
await page.click('input#agreementCheckbox-certinomisCGU');
await page.click('input#agreementCheckbox-checkbox1');
await page.click('input#agreementCheckbox-checkbox2');
await page.click('button#js-summaryNextBtn-1');
await page.waitForSelector('#js-summaryNextBtn-1');
await page.waitForLoadState('load');
await page.waitForLoadState('domcontentloaded');
const maxAttempts = 3;
for (let attemptIndex = 0; attemptIndex < maxAttempts; attemptIndex++) {
const buttonEnabled = await page.evaluate(() => {
const button = document.querySelector('button.js-otp-sign-button');
return button && button.getAttribute('aria-disabled') === 'false';
});
if (buttonEnabled) {
console.log("The button is enabled, you can continue.");
break;
} else {
console.log(`The button is disabled, attempt ${attemptIndex + 1}...`);
await page.fill('input#js-otp-input-1', '000000');
}
}
await page.click('.js-otp-sign-button');
await page.evaluate(() => window.performance.mark('fin'));
await page.evaluate(() => window.performance.measure('tempsSignature', 'debut', 'fin'));
const signature = await page.evaluate(() =>
JSON.parse(
JSON.stringify(
window.performance.getEntriesByName("tempsSignature")
)
)[0].duration
);
signatureFront.add(signature);
await page.waitForLoadState();
} catch (error) {
errorSignature.add(1);
console.error('Error: An exception has occurred');
console.error('Error:', error.message);
console.error('Stack Trace:', error.stack);
} finally {
console.log('PAGE CLOSED');
await page.close();
}
}
Thank you in advance for your help.