You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Gherkin的Scenario Outline中实现多参数的import_child_log

解决Gherkin Scenario Outline单步骤校验多值的问题

针对你在import_child_log参数中使用多个占位符的需求,有几种可行的实现方式,以下是具体方案:

方案1:拆分参数到Examples表格

将需要校验的多个值拆分为独立的参数列,在步骤中组合或分别校验:

修改后的Gherkin代码

Background:
  Given I am "user"
  And I am logged in
  And I am on the "content-management" page

Scenario Outline: As a User, I want to be able to upload, import and export an environment containing unsupported entities
        When I upload files:
            | Name        |
            | <file_name> |
        Then I should see record with parameters:
            | Name   | Content Description   | Source Environment | Source Version
            | <Name> | <Content Description> | <Source Env>       | <Source Ver> |
        And I can find parent log message "<upload_log_message>"
        When I import environment with properties:
            | Property  | Value |
            | Activate  | Yes   |
            | Overwrite | No    |
        And I can confirm import
        Then I should see notification "Successfully imported the environment"
        And I can find parent log message "Env '<envID>/<envVer>': Import process completed in"
        And I can find child log message parts: "<prefix>", "<def_val>", "<ghi_val>"
**EXAMPLES:**
 | file_name | Name  | Content Description |upload_log_message | prefix | def_val | ghi_val
 | ABC       | ABC   | ABC                 | ABC               | ABC    | DEF     | GHI

对应Playwright步骤定义(TypeScript示例)

import { Then } from '@cucumber/cucumber';
import { expect } from '@playwright/test';

Then('I can find child log message parts: {string}, {string}, {string}', async (prefix, defVal, ghiVal) => {
  const childLog = page.locator('.child-log-element'); // 替换为实际日志元素定位器
  // 方式1:校验完整日志内容
  await expect(childLog).toContainText(`${prefix} ${defVal} ${ghiVal}`);
  // 方式2:分别校验每个值存在
  await expect(childLog).toContainText(prefix);
  await expect(childLog).toContainText(defVal);
  await expect(childLog).toContainText(ghiVal);
});

方案2:用JSON格式传递多值

将多个值打包成JSON字符串,在步骤中解析为数组后逐个校验:

修改后的Gherkin代码

// ... 其他步骤保持不变
And I can find child log message "<import_child_log>"
**EXAMPLES:**
 | file_name | Name  | Content Description |upload_log_message | import_child_log
 | ABC       | ABC   | ABC                 | ABC               | ["ABC", "DEF", "GHI"]

对应Playwright步骤定义(TypeScript示例)

import { Then } from '@cucumber/cucumber';
import { expect } from '@playwright/test';

Then('I can find child log message {string}', async (logValuesJson) => {
  const logValues = JSON.parse(logValuesJson);
  const childLog = page.locator('.child-log-element');
  for (const value of logValues) {
    await expect(childLog).toContainText(value);
  }
});

方案3:自定义参数类型(进阶)

通过Cucumber的自定义参数类型,将分隔符分隔的字符串自动转换为数组:

第一步:定义参数类型

import { defineParameterType } from '@cucumber/cucumber';

// 定义用竖线|分隔的多值参数类型
defineParameterType({
  name: 'logValueList',
  regexp: /([^|]+(?:\|[^|]+)*)/,
  transformer: (value: string) => value.split('|')
});

修改后的Gherkin代码

// ... 其他步骤保持不变
And I can find child log message <import_child_log>
**EXAMPLES:**
 | file_name | Name  | Content Description |upload_log_message | import_child_log
 | ABC       | ABC   | ABC                 | ABC               | ABC|DEF|GHI

对应Playwright步骤定义

import { Then } from '@cucumber/cucumber';
import { expect } from '@playwright/test';

Then('I can find child log message {logValueList}', async (logValues: string[]) => {
  const childLog = page.locator('.child-log-element');
  for (const value of logValues) {
    await expect(childLog).toContainText(value);
  }
});

这三种方案都能满足你在单步骤中校验多值的需求,其中方案1最适合新手,逻辑清晰易维护;方案2和3更灵活,适合需要传递更多值的场景。

内容的提问来源于stack exchange,提问作者Uros Matijasevic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 06:20:59