Cucumber步进定义技能Skill cucumber-step-definitions

这个技能涉及编写Cucumber测试框架中的步进定义代码,用于自动化测试和BDD(行为驱动开发),实现Gherkin场景与测试脚本的绑定。关键词包括Cucumber、步进定义、测试自动化、BDD、Gherkin、JavaScript、Java、Ruby、钩子、World上下文。

测试 0 次安装 0 次浏览 更新于 3/25/2026

name: cucumber-step-definitions user-invocable: false description: 编写有效的步进定义和组织测试代码

Cucumber步进定义

掌握为Cucumber测试编写可维护和可重用的步进定义。

基本步进定义

定义匹配Gherkin语法的步骤:

JavaScript/TypeScript (Cucumber.js)

const { Given, When, Then } = require('@cucumber/cucumber');

Given('I am on the login page', async function() {
  await this.page.goto('/login');
});

When('I enter valid credentials', async function() {
  await this.page.fill('#username', 'testuser');
  await this.page.fill('#password', 'password123');
});

Then('I should be logged in', async function() {
  const welcomeMessage = await this.page.textContent('.welcome');
  expect(welcomeMessage).toContain('Welcome, testuser');
});

Java (Cucumber-JVM)

import io.cucumber.java.en.*;
import static org.junit.Assert.*;

public class LoginSteps {

  @Given("I am on the login page")
  public void i_am_on_login_page() {
    driver.get("http://example.com/login");
  }

  @When("I enter valid credentials")
  public void i_enter_valid_credentials() {
    driver.findElement(By.id("username")).sendKeys("testuser");
    driver.findElement(By.id("password")).sendKeys("password123");
  }

  @Then("I should be logged in")
  public void i_should_be_logged_in() {
    String welcome = driver.findElement(By.className("welcome")).getText();
    assertTrue(welcome.contains("Welcome, testuser"));
  }
}

Ruby

Given('I am on the login page') do
  visit '/login'
end

When('I enter valid credentials') do
  fill_in 'username', with: 'testuser'
  fill_in 'password', with: 'password123'
end

Then('I should be logged in') do
  expect(page).to have_content('Welcome, testuser')
end

参数化步骤

从Gherkin步骤中捕获值:

// 场景: I search for "Cucumber" in the search bar

When('I search for {string} in the search bar', async function(searchTerm) {
  await this.page.fill('#search', searchTerm);
  await this.page.click('#search-button');
});

// 场景: I add 5 items to my cart

When('I add {int} items to my cart', async function(quantity) {
  for (let i = 0; i < quantity; i++) {
    await this.addItemToCart();
  }
});

// 场景: The price should be $99.99

Then('the price should be ${float}', async function(expectedPrice) {
  const actualPrice = await this.page.textContent('.price');
  expect(parseFloat(actualPrice)).toBe(expectedPrice);
});

正则表达式

使用正则表达式进行灵活匹配:

// 匹配: "I wait 5 seconds", "I wait 10 seconds"
When(/^I wait (\d+) seconds?$/, async function(seconds) {
  await this.page.waitForTimeout(seconds * 1000);
});

// 匹配: "I should see a success message", "I should see an error message"
Then(/^I should see (?:a|an) (success|error) message$/, async function(type) {
  const message = await this.page.textContent(`.${type}-message`);
  expect(message).toBeTruthy();
});

数据表

处理步骤中的表格数据:

When('I create a user with the following details:', async function(dataTable) {
  // dataTable.hashes() 转换为对象数组
  const users = dataTable.hashes();

  for (const user of users) {
    await this.api.createUser({
      firstName: user['First Name'],
      lastName: user['Last Name'],
      email: user['Email']
    });
  }
});

// 替代: dataTable.raw() 用于原始二维数组
When('I select the following options:', async function(dataTable) {
  const options = dataTable.raw().flat(); // ['Option1', 'Option2']

  for (const option of options) {
    await this.page.check(`input[value="${option}"]`);
  }
});

文档字符串

处理多行文本:

When('I submit a message:', async function(messageText) {
  await this.page.fill('#message', messageText);
  await this.page.click('#submit');
});

世界上下文

使用World在步骤之间共享状态:

const { setWorldConstructor, World } = require('@cucumber/cucumber');

class CustomWorld extends World {
  constructor(options) {
    super(options);
    this.cart = [];
    this.user = null;
  }

  async login(username, password) {
    this.user = await this.api.login(username, password);
  }

  addToCart(item) {
    this.cart.push(item);
  }
}

setWorldConstructor(CustomWorld);

// 在步骤中使用
Given('I am logged in', async function() {
  await this.login('testuser', 'password');
});

When('I add an item to my cart', async function() {
  this.addToCart({ id: 1, name: 'Product' });
});

钩子

设置和拆除测试状态:

const { Before, After, BeforeAll, AfterAll } = require('@cucumber/cucumber');

BeforeAll(async function() {
  // 在所有场景之前运行一次
  await startTestServer();
});

Before(async function() {
  // 在每个场景之前运行
  this.browser = await launchBrowser();
  this.page = await this.browser.newPage();
});

Before({ tags: '@database' }, async function() {
  // 仅针对带有@database标签的场景运行
  await this.db.clear();
});

After(async function() {
  // 在每个场景之后运行
  await this.browser.close();
});

AfterAll(async function() {
  // 在所有场景之后运行一次
  await stopTestServer();
});

步骤组织

页面对象模式

// pages/LoginPage.js
class LoginPage {
  constructor(page) {
    this.page = page;
  }

  async navigate() {
    await this.page.goto('/login');
  }

  async fillCredentials(username, password) {
    await this.page.fill('#username', username);
    await this.page.fill('#password', password);
  }

  async submit() {
    await this.page.click('#login-button');
  }
}

// step-definitions/login-steps.js
const LoginPage = require('../pages/LoginPage');

Given('I am on the login page', async function() {
  this.loginPage = new LoginPage(this.page);
  await this.loginPage.navigate();
});

When('I enter {string} and {string}', async function(username, password) {
  await this.loginPage.fillCredentials(username, password);
  await this.loginPage.submit();
});

辅助函数

// support/helpers.js
async function waitForElement(page, selector, timeout = 5000) {
  await page.waitForSelector(selector, { timeout });
}

async function takeScreenshot(page, name) {
  await page.screenshot({ path: `screenshots/${name}.png` });
}

module.exports = { waitForElement, takeScreenshot };

// 在步骤中使用
const { waitForElement } = require('../support/helpers');

Then('I should see the dashboard', async function() {
  await waitForElement(this.page, '.dashboard');
});

最佳实践

  1. 保持步骤简单和专注 - 每个步骤一个动作或断言
  2. 重用步骤 - 编写适用于多个场景的通用步骤
  3. 避免实现细节 - 不要在步骤名称中暴露内部结构
  4. 使用World - 通过World共享状态,而不是全局变量
  5. 按域组织 - 将相关步骤分组在一起
  6. 不要重复逻辑 - 将常见功能提取到辅助函数
  7. 使步骤可读 - 步进定义应像文档一样可读
  8. 正确处理异步 - 一致使用async/await

要避免的反模式

不要创建过于具体的步骤:

Given('I am on the login page as a premium user with valid credentials')

创建可组合的步骤:

Given('I am on the login page')
And('I am a premium user')
And('I have valid credentials')

不要在Given/When中放置断言:

When('I click login and see the dashboard')

分离动作和断言:

When('I click login')
Then('I should see the dashboard')

不要将步骤用作函数:

// 不要从步骤内部调用步骤
When('I log in', async function() {
  await this.Given('I am on the login page'); // 错误!
  await this.When('I enter credentials'); // 错误!
});

提取到辅助函数:

// support/auth-helpers.js
async function login(world, username, password) {
  await world.page.goto('/login');
  await world.page.fill('#username', username);
  await world.page.fill('#password', password);
  await world.page.click('#login-button');
}

// 在步骤中使用
When('I log in', async function() {
  await login(this, 'user', 'pass');
});

记住:步进定义是可读场景和自动化代码之间的粘合剂。保持它们清洁、可维护和专注。