0

我必须在 K6 中为一个应用程序编写大约 20 个不同的脚本。而且这些脚本中的大多数都包含常见的功能,如登录、选择一些选项等......

那么有没有更好的方法来编写 K6 脚本而不复制这些常用功能呢?我们可以在某处实现通用方法并在默认函数或类似的函数中执行它吗?

4

2 回答 2

3

您可以编写自己的模块包含常用功能,然后导入它们:

$ cat index.js
import { hello_world } from './modules/module.js';

export default function() {
    hello_world();
}
$ cat module.js
export function hello_world() {
    console.log("Hello world");
}

你可以在这里阅读更多细节。

于 2019-10-09T06:51:16.817 回答
1

是的,您可以将常用方法移动到单独的 JS 文件中,然后将import它们放在需要它们的脚本中:https ://docs.k6.io/docs/modules

于 2019-10-09T06:51:04.183 回答