🔍 提取器 
提取器是 Ryze 框架中的重要组件,用于从取样器或处理器的响应结果中提取数据,并将提取的值存储为变量供后续使用。
🔍 基本概念 
作用原理 
提取器在取样器或处理器执行完成后自动运行,从响应结果中提取指定的数据,并将其存储为变量,供后续的测试步骤使用。
配置位置 
提取器可以配置在:
- 取样器中:直接从取样器的响应中提取数据
- 后置处理器中:作为后置处理的一部分
- 测试集合中:在集合级别进行数据提取
🛠️ 内置提取器 
📊 JSON 提取器 
用于从 JSON 格式的响应数据中提取指定值。
配置参数:
| 参数 | 类型 | 说明 | 
|---|---|---|
| testclass | String | 提取器类型,可选值: json、JSONExtractor、json_extractor | 
| field | String | JSONPath 表达式(必填) | 
| ref_name | String | 变量名称(必填) | 
使用示例:
yaml
# 方式1:完整配置
- testclass: json
  field: '$.data.user.id'
  ref_name: user_id
# 方式2:简化配置
- { testclass: json, field: '$.status', ref_name: api_status }JSONPath 表达式示例:
json
{
  "status": "success",
  "data": {
    "user": {
      "id": 123,
      "name": "张三",
      "roles": [
        "admin",
        "user"
      ]
    },
    "pagination": {
      "total": 100,
      "page": 1
    }
  }
}yaml
extractors:
  - { testclass: json, field: '$.status', ref_name: status }  # 提取: "success"
  - { testclass: json, field: '$.data.user.id', ref_name: user_id }  # 提取: 123
  - { testclass: json, field: '$.data.user.roles[0]', ref_name: first_role }  # 提取: "admin"
  - { testclass: json, field: '$.data.pagination.total', ref_name: total_count }  # 提取: 100🔍 正则表达式提取器 
用于从 HTML、XML 或其他文本格式的响应中通过正则表达式提取数据。
配置参数:
| 参数 | 类型 | 默认值 | 说明 | 
|---|---|---|---|
| testclass | String | 无 | 提取器类型,可选值: regex、RegexExtractor、regex_extractor | 
| field | String | 无 | 正则表达式模式(必填) | 
| match_num | Integer | 0 | 匹配组索引(0表示第一个匹配) | 
| ref_name | String | 无 | 变量名称(必填) | 
使用示例:
yaml
# 从 HTML 中提取链接
extractors:
  - testclass: regex
    field: '<a\\s+href="([^"]+)"'
    match_num: 0
    ref_name: first_link
  # 提取数字
  - testclass: regex
    field: '\\d+'
    match_num: 1  # 第二个匹配的数字
    ref_name: second_number
  # 简化配置
  - { testclass: regex, field: 'error_code:(\\d+)', match_num: 0, ref_name: error_code }常用正则模式:
- 提取数字:\\d+
- 提取邮箱:[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}
- 提取 URL:https?://[^\\s]+
- 提取 HTML 标签内容:<tag[^>]*>([^<]*)</tag>
📋 结果提取器 
将整个响应结果作为变量值保存。
配置参数:
| 参数 | 类型 | 说明 | 
|---|---|---|
| testclass | String | 提取器类型,可选值: result、ResultExtractor、result_extractor | 
| ref_name | String | 变量名称(必填) | 
使用示例:
yaml
extractors:
  # 保存完整响应
  - testclass: result
    ref_name: full_response
  # 简化配置
  - { testclass: result, ref_name: api_response }🌐 HTTP Header 提取器 
从 HTTP 响应头中提取指定的头信息。
配置参数:
| 参数 | 类型 | 默认值 | 说明 | 
|---|---|---|---|
| testclass | String | 无 | 提取器类型,可选值: http、http_header、HTTPHeader、HTTPHeaderExtractor | 
| field | String | 无 | 响应头名称(区分大小写,必填) | 
| match_num | Integer | 0 | 匹配索引(当有多个同名头时) | 
| ref_name | String | 无 | 变量名称(必填) | 
使用示例:
yaml
extractors:
  # 提取 Content-Type
  - testclass: http
    field: 'Content-Type'
    match_num: 0
    ref_name: content_type
  # 提取自定义头
  - testclass: http_header
    field: 'X-Request-ID'
    ref_name: request_id
  # 简化配置
  - { testclass: http, field: 'Set-Cookie', match_num: 0, ref_name: session_cookie }常用 HTTP 头:
- Content-Type:响应内容类型
- Content-Length:响应内容长度
- Set-Cookie:Cookie 设置
- Location:重定向地址
- X-*:自定义头
🔧 使用场景 
📊 数据传递场景 
在多步骤测试中,使用提取器将前一步的结果传递给后一步:
yaml
# 步骤1:登录获取 token
- title: "用户登录"
  testclass: http
  config:
    method: POST
    url: https://api.example.com/login
    body:
      username: admin
      password: password123
  extractors:
    # 提取登录返回的 token
    - { testclass: json, field: '$.data.token', ref_name: auth_token }
    # 提取用户ID
    - { testclass: json, field: '$.data.user.id', ref_name: user_id }
# 步骤2:使用 token 获取用户信息
- title: "获取用户信息"
  testclass: http
  config:
    method: GET
    url: https://api.example.com/user/${user_id}
    headers:
      Authorization: Bearer ${auth_token}
  extractors:
    - { testclass: json, field: '$.data.profile', ref_name: user_profile }🔗 链式数据处理 
组合多个提取器处理复杂数据:
yaml
testclass: http
config:
  method: GET
  url: https://api.example.com/products
extractors:
  # 提取商品列表
  - { testclass: json, field: '$.data.products', ref_name: product_list }
  # 提取第一个商品ID
  - { testclass: json, field: '$.data.products[0].id', ref_name: first_product_id }
  # 提取分页信息
  - { testclass: json, field: '$.pagination.total', ref_name: total_products }
  # 从响应头提取缓存信息
  - { testclass: http, field: 'Cache-Control', ref_name: cache_policy }🔍 条件数据提取 
根据响应内容的不同情况提取不同数据:
yaml
testclass: http
config:
  method: GET
  url: https://api.example.com/status
extractors:
  # 提取状态码
  - { testclass: json, field: '$.status', ref_name: api_status }
  # 根据状态提取不同信息
  - testclass: json
    field: '$.data.success_message'  # 成功时的消息
    ref_name: success_msg
  - testclass: json
    field: '$.error.message'  # 失败时的错误信息
    ref_name: error_msg🔢 批量数据处理 
处理数组类型的响应数据:
yaml
testclass: http
config:
  method: GET
  url: https://api.example.com/orders
extractors:
  # 提取所有订单ID
  - { testclass: json, field: '$.data[*].id', ref_name: all_order_ids }
  # 提取最新订单
  - { testclass: json, field: '$.data[0]', ref_name: latest_order }
  # 提取特定状态的订单
  - { testclass: json, field: '$.data[?(@.status=="pending")].id', ref_name: pending_orders }💡 扩展功能 
当内置提取器无法满足特定需求时,Ryze 框架支持自定义提取器扩展。详细的开发指南请参考:
💡 提示:提取器是实现数据驱动测试的核心工具,熟练掌握各种提取器的使用可以大大提高测试的灵活性和可维护性!