菜单
文档breadcrumb arrow Grafana k6breadcrumb arrow 示例breadcrumb arrow 关联与动态数据
开源

关联与动态数据

关于如何在测试脚本中关联动态数据的脚本示例。当使用 Chrome 扩展程序或 HAR 转换器生成测试脚本时,通常需要进行关联。这是因为这些工具会从您特定的会话中捕获会话 ID、CSRF 令牌、VIEWSTATE、wpnonce 和其他动态值。这些令牌通常很快过期。这是用户在测试网站或 Web 应用程序上的用户旅程时最常需要编写脚本来处理的问题之一。

关联

在负载测试场景中,关联意味着从一个请求的响应中提取一个或多个值,然后在后续请求中重复使用它们。通常,这可能是获取某个令牌或某种 ID,以便完成用户旅程中的一系列步骤。

录制将捕获会话数据,例如 CSRF 令牌、VIEWSTATES、nonce 等。此类数据在您运行测试时很可能无效,这意味着您需要处理从 HTML/表单中提取此数据,以便将其包含在后续请求中。此问题对于任何包含表单的站点都相当常见,并且可以通过一些脚本处理。

从 JSON 响应中提取值/令牌

JavaScript
import http from 'k6/http';
import { check } from 'k6';

export default function () {
  // Make a request that returns some JSON data
  const reqHeaders = {
    Authorization: 'Token abcdef0123456789',
  };
  const res = http.get('https://quickpizza.grafana.com/api/doughs', {
    headers: reqHeaders,
  });

  // Extract data from that JSON data by first parsing it
  // using a call to "json()" and then accessing properties by
  // navigating the JSON data as a JS object with dot notation.
  const dough1 = res.json().doughs[0];
  check(dough1, {
    'dough1 1 has correct name': (s) => s.name === 'Thin',
    'dough1 1 has correct ID': (s) => s.ID === 1,
  });

  // Now we could use the "dough1" variable in subsequent requests...
}

相关 k6 API:

从表单字段中提取值/令牌

在决定如何处理表单提交时,您可以选择两种不同的方法。您可以使用更高级别的 Response.submitForm([params]) API,或者提取必要的隐藏字段等并自行构建请求,然后使用适当的 http.* 系列 API 发送,例如 http.post(url, [body], [params])

提取 .NET ViewStates、CSRF 令牌及其他隐藏输入字段

JavaScript
import http from 'k6/http';
import { sleep } from 'k6';

export default function () {
  // Request the page containing a form and save the response. This gives you access
  //to the response object, `res`.
  const res = http.get('https://test.k6.io/my_messages.php', { responseType: 'text' });

  // Query the HTML for an input field named "redir". We want the value or "redir"
  const elem = res.html().find('input[name=redir]');

  // Get the value of the attribute "value" and save it to a variable
  const val = elem.attr('value');

  // Now you can concatenate this extracted value in subsequent requests that require it.
  // ...
  // console.log() works when executing k6 scripts locally and is handy for debugging purposes
  console.log('The value of the hidden field redir is: ' + val);

  sleep(1);
}

⚠️ 您知道吗?

请注意,如果在脚本的 options 部分中将 discardResponseBodies 设置为 true。如果是,您可以将其设置为 false,或者如示例所示,使用 {"responseType": "text"} 按请求保存响应。

相关 k6 API:

通用值/令牌提取

有时,响应既不是 JSON 也不是 HTML,在这种情况下,上述提取方法不适用。在这种情况下,您可能希望直接对 Response.body 字符串进行操作,使用一个能够从已知位置提取字符串的简单函数。这通常通过查找需要提取值之前(左边界)和之后(右边界)的字符串“边界”来实现。

jslib utils 库包含一个此类函数的示例,即 findBetween。该函数使用 JavaScript 内置的 String.indexOf,因此不依赖于可能耗费性能的正则表达式操作。

使用 findBetween 提取值/令牌

JavaScript
import { findBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
import { check } from 'k6';
import http from 'k6/http';

export default function () {
  // This request returns XML:
  const res = http.get('https://quickpizza.grafana.com/api/xml?color=green');

  // Use findBetween to extract the first <value> tag encountered:
  const color = findBetween(res.body, '<value>', '</value>');

  check(color, {
    'color is correct': (t) => t === 'green',
  });
}

相关 k6 API: