菜单
文档breadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow k6/browserbreadcrumb arrow Pagebreadcrumb arrow setInputFiles(selector, file[, options])
开源

setInputFiles(selector, file[, options])

将文件输入元素的值设置为指定的文件。

要使用文件系统上的本地文件,请使用实验性的 fs 模块来加载和读取文件内容。

参数类型默认值描述
selectorstring''用于查找元素的 selector。如果存在多个满足 selector 的元素,将使用第一个。
fileobjectnull这是必填参数。
file.namestring''文件的名称。例如,file.txt
file.mimeTypestring''文件内容的类型。例如,text/plain
file.bufferArrayBuffer[]文件的 Base64 编码内容。
optionsobjectnull这是可选参数。
options.noWaitAfterbooleanfalse如果设置为 true 且执行此操作后发生导航,则不会等待导航完成。
options.timeoutnumber30000最大时间(毫秒)。传入 0 禁用超时。默认值会被 BrowserContextPage 上的 setDefaultTimeout 选项覆盖。

返回值

类型描述
Promise<void>一个 Promise,当文件设置完成后会 fulfil。

内联文件示例

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>`);

    // The file is set to the input element with the id "upload".
    await page.setInputFiles('input[id="upload"]', {
      name: 'file.txt',
      mimetype: 'text/plain',
      buffer: encoding.b64encode('hello world'),
    });
  } finally {
    await page.close();
  }
}

本地文件示例

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.
    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>`);

    // 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 page.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;
}