setInputFiles(file[, options])
This is documentation for the next version of K6. For the latest stable release, go to the latest version.
Open source
setInputFiles(file[, options])
Sets the file input element’s value to the specified files.
To work with local files on the file system, use the experimental fs module to load and read the file contents.
Parameter | Type | Default | Description |
---|---|---|---|
file | object | null | This is a required parameter. |
file.name | string | '' | The name of the file. For example, file.txt . |
file.mimeType | string | '' | The type of the file content. For example, text/plain . |
file.buffer | ArrayBuffer | [] | Base64 encoded content of the file. |
options | object | null | This is an optional parameter. |
options.noWaitAfter | boolean | false | If set to true and a navigation occurs from performing this action, it will not wait for it to complete. |
options.timeout | number | 30000 | Maximum time in milliseconds. Pass 0 to disable the timeout. Default is overridden by the setDefaultTimeout option on BrowserContext or Page. |
Returns
Type | Description |
---|---|
Promise<void> | A Promise that fulfills when the file is set. |
Inline File Example
JavaScript
import { browser } from 'k6/browser';
import encoding from 'k6/encoding';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
try {
// In this example we create a simple web page with an upload input field.
// Usually, you would use page.goto to navigate to a page with a file input field.
await page.setContent(`
<html>
<head></head>
<body>
<!-- Simple file upload form -->
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="file" id="upload" multiple />
<input type="submit" value="Send" />
</form>
</body>
</html>`);
const eh = await page.$('input[id="upload"]');
// The file is set to the input element with the id "upload".
await eh.setInputFiles({
name: 'file.txt',
mimetype: 'text/plain',
buffer: encoding.b64encode('hello world'),
});
} finally {
await page.close();
}
}
Local File Example
JavaScript
import { browser } from 'k6/browser';
import encoding from 'k6/encoding';
import { open } from 'k6/experimental/fs';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
// Declare the location of the file on the local filesystem.
let file;
(async function () {
file = await open('/abs/path/to/file.txt');
})();
export default async function () {
const page = await browser.newPage();
try {
// In this example we create a simple web page with an upload input field.
// Usually, you would use page.goto to navigate to a page with a file input field.
await page.setContent(`
<html>
<head></head>
<body>
<!-- Simple file upload form -->
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="file" id="upload" multiple />
<input type="submit" value="Send" />
</form>
</body>
</html>`);
const eh = await page.$('input[id="upload"]');
// Read the whole file content into a buffer.
const buffer = await readAll(file);
// The file is set to the input element with the id "upload".
await eh.setInputFiles({
name: 'file.txt',
mimetype: 'text/plain',
buffer: encoding.b64encode(buffer),
});
} finally {
await page.close();
}
}
// readAll will read the whole of the file from the local filesystem into a
// buffer.
async function readAll(file) {
const fileInfo = await file.stat();
const buffer = new Uint8Array(fileInfo.size);
const bytesRead = await file.read(buffer);
if (bytesRead !== fileInfo.size) {
throw new Error('unexpected number of bytes read');
}
return buffer;
}
Was this page helpful?
Related documentation
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
24 Apr

Getting started with Grafana dashboard design
In this webinar, you'll learn how to design stylish and easily accessible Grafana dashboards that tell a story.
08 May

Grafana 12 deep dive
It wouldn’t be GrafanaCON without a new release. Join us as we dive into the latest features of Grafana 12, including dashboards as code, Git-based configs, dynamic dashboards, and more.
08 May

Monitoring EA App’s 200+ core error metrics with a scalable, color-coded Grafana dashboard
Learn how engineers at Electronic Arts built a Grafana dashboard that client teams can use to monitor 200+ core error metrics.