Loading...
Loading...
Compare original and translation side by side
Scenario: Add product to cart
Given I navigate to "http://shop.com/products"
When I find the element with CSS ".product[data-id='123']"
And I click the button with class "add-to-cart"
And I wait for the AJAX request to complete
Then the element ".cart-count" should contain "1"Scenario: Add product to cart
Given I am browsing products
When I add "Wireless Headphones" to my cart
Then my cart should contain 1 itemScenario: Add product to cart
Given I navigate to "http://shop.com/products"
When I find the element with CSS ".product[data-id='123']"
And I click the button with class "add-to-cart"
And I wait for the AJAX request to complete
Then the element ".cart-count" should contain "1"Scenario: Add product to cart
Given I am browsing products
When I add "Wireless Headphones" to my cart
Then my cart should contain 1 itemScenario: User registration and login and profile update
Given I register a new account
When I log in
And I update my profile
And I change my password
Then everything should workScenario: Register new account
When I register with valid details
Then I should receive a confirmation email
Scenario: Login with new account
Given I have registered an account
When I log in with my credentials
Then I should see my dashboard
Scenario: Update profile
Given I am logged in
When I update my profile information
Then my changes should be savedScenario: User registration and login and profile update
Given I register a new account
When I log in
And I update my profile
And I change my password
Then everything should workScenario: Register new account
When I register with valid details
Then I should receive a confirmation email
Scenario: Login with new account
Given I have registered an account
When I log in with my credentials
Then I should see my dashboard
Scenario: Update profile
Given I am logged in
When I update my profile information
Then my changes should be savedScenario: Create order
When I create order #12345
Scenario: View order
When I view order #12345 # Depends on previous scenario!Scenario: View order
Given an order exists with ID "12345"
When I view the order details
Then I should see the order informationScenario: Create order
When I create order #12345
Scenario: View order
When I view order #12345 # 依赖前一个场景!Scenario: View order
Given an order exists with ID "12345"
When I view the order details
Then I should see the order informationFeature: Shopping Cart
Background:
Given I am logged in as a customer
Scenario: Add product to cart
When I add a product to my cart
Then my cart should contain 1 item
Scenario: Remove product from cart
Given I have a product in my cart
When I remove the product
Then my cart should be emptyBackground:
Given I am on the homepage
And I click the menu
And I navigate to products
And I filter by category "Electronics"
And I sort by price
# Too much setup! Not all scenarios need all of thisFeature: Shopping Cart
Background:
Given I am logged in as a customer
Scenario: Add product to cart
When I add a product to my cart
Then my cart should contain 1 item
Scenario: Remove product from cart
Given I have a product in my cart
When I remove the product
Then my cart should be emptyBackground:
Given I am on the homepage
And I click the menu
And I navigate to products
And I filter by category "Electronics"
And I sort by price
# 前置条件过多!并非所有场景都需要这些步骤Feature: User Authentication
Scenario: Successful login
...
Scenario: Failed login with wrong password
...
Scenario: Account lockout after multiple failures
...Feature: User Authentication
Scenario: Successful login
...
Scenario: Failed login with wrong password
...
Scenario: Account lockout after multiple failures
...@smoke @critical
Scenario: Login with valid credentials
...
@slow @integration
Scenario: Password reset email workflow
...
@wip
Scenario: OAuth login
# Work in progress
...undefined@smoke @critical
Scenario: Login with valid credentials
...
@slow @integration
Scenario: Password reset email workflow
...
@wip
Scenario: OAuth login
# 开发中
...undefinedundefinedundefinedScenario: POST request to /api/users
When I send a POST to "/api/users" with JSON payload
And the response status is 201Scenario: Register new user
When I register a new user account
Then the user should be created successfullyScenario: POST request to /api/users
When I send a POST to "/api/users" with JSON payload
And the response status is 201Scenario: Register new user
When I register a new user account
Then the user should be created successfullyScenario: Purchase product
Given I am logged in
When I add a product to cart
And I click the element with ID "checkout-btn" # Too detailed!
And I enter credit card "4111111111111111" # Too detailed!
Then I complete the purchaseScenario: Purchase product
Given I am logged in
And I have a product in my cart
When I checkout with a credit card
Then my order should be completed
And I should receive a confirmation emailScenario: Purchase product
Given I am logged in
When I add a product to cart
And I click the element with ID "checkout-btn" # 过于细节!
And I enter credit card "4111111111111111" # 过于细节!
Then I complete the purchaseScenario: Purchase product
Given I am logged in
And I have a product in my cart
When I checkout with a credit card
Then my order should be completed
And I should receive a confirmation emailWhen I log in and add a product to cart and checkoutWhen I log in
And I add a product to my cart
And I proceed to checkoutWhen I log in and add a product to cart and checkoutWhen I log in
And I add a product to my cart
And I proceed to checkoutScenario Outline: Login validation
When I log in with "<username>" and "<password>"
Then I should see "<message>"
Examples:
| username | password | message |
| valid | valid | Welcome |
| invalid | valid | Invalid username |
| valid | invalid | Invalid password |
| empty | empty | Username required |undefinedScenario Outline: Login validation
When I log in with "<username>" and "<password>"
Then I should see "<message>"
Examples:
| username | password | message |
| valid | valid | Welcome |
| invalid | valid | Invalid username |
| valid | invalid | Invalid password |
| empty | empty | Username required |undefinedundefinedundefinedScenario Outline: Discount calculation
Given a customer with "<membership>" status
When they purchase items totaling $<amount>
Then they should receive a $<discount> discount
Examples: Standard discounts
| membership | amount | discount |
| silver | 100 | 5 |
| gold | 100 | 10 |
| platinum | 100 | 15 |
Examples: Minimum purchase thresholds
| membership | amount | discount |
| silver | 49 | 0 |
| silver | 50 | 2.50 |Scenario Outline: Discount calculation
Given a customer with "<membership>" status
When they purchase items totaling $<amount>
Then they should receive a $<discount> discount
Examples: Standard discounts
| membership | amount | discount |
| silver | 100 | 5 |
| gold | 100 | 10 |
| platinum | 100 | 15 |
Examples: Minimum purchase thresholds
| membership | amount | discount |
| silver | 49 | 0 |
| silver | 50 | 2.50 |// Generic, reusable
When('I fill in {string} with {string}', async function(field, value) {
await this.page.fill(`[name="${field}"]`, value);
});
// Used in multiple scenarios:
When('I fill in "email" with "test@example.com"')
When('I fill in "password" with "secure123"')
When('I fill in "search" with "products"')// 通用可复用步骤
When('I fill in {string} with {string}', async function(field, value) {
await this.page.fill(`[name="${field}"]`, value);
});
// 在多个场景中使用:
When('I fill in "email" with "test@example.com"')
When('I fill in "password" with "secure123"')
When('I fill in "search" with "products"')When('I do {string} with {string} and {string}', ...)When('I log in with {string} and {string}', ...)
When('I search for {string} in {string}', ...)When('I do {string} with {string} and {string}', ...)When('I log in with {string} and {string}', ...)
When('I search for {string} in {string}', ...)// support/factories.js
const faker = require('faker');
class UserFactory {
static create(overrides = {}) {
return {
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
email: faker.internet.email(),
password: 'Test123!',
...overrides
};
}
}
// Use in steps
Given('I register a new user', async function() {
const user = UserFactory.create();
this.currentUser = user;
await this.api.register(user);
});// support/factories.js
const faker = require('faker');
class UserFactory {
static create(overrides = {}) {
return {
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
email: faker.internet.email(),
password: 'Test123!',
...overrides
};
}
}
// 在步骤中使用
Given('I register a new user', async function() {
const user = UserFactory.create();
this.currentUser = user;
await this.api.register(user);
});Given user "12345" exists
When I view order "67890"Given a user "john@example.com" exists
When I view my most recent orderGiven user "12345" exists
When I view order "67890"Given a user "john@example.com" exists
When I view my most recent order@happy-path
Scenario: Successful checkout
Given I have items in my cart
When I complete the checkout process
Then my order should be confirmed
@error-handling
Scenario: Checkout with expired card
Given I have items in my cart
When I checkout with an expired credit card
Then I should see an error message
And my order should not be processed
@edge-case
Scenario: Checkout with insufficient inventory
Given I have a product in my cart
But the product is out of stock
When I attempt to checkout
Then I should be notified about stock unavailability@happy-path
Scenario: Successful checkout
Given I have items in my cart
When I complete the checkout process
Then my order should be confirmed
@error-handling
Scenario: Checkout with expired card
Given I have items in my cart
When I checkout with an expired credit card
Then I should see an error message
And my order should not be processed
@edge-case
Scenario: Checkout with insufficient inventory
Given I have a product in my cart
But the product is out of stock
When I attempt to checkout
Then I should be notified about stock unavailability@slow @integration
Scenario: Full order workflow with email notifications
# Takes 30 seconds to run
...@slow @integration
Scenario: Full order workflow with email notifications
# 运行需要30秒
...// cucumber.js
module.exports = {
default: '--parallel 4'
};// cucumber.js
module.exports = {
default: '--parallel 4'
};features/
authentication/
login.feature
registration.feature
shopping/
cart.feature
checkout.feature
admin/
user-management.featurefeatures/
authentication/
login.feature
registration.feature
shopping/
cart.feature
checkout.feature
admin/
user-management.featureThen the database should have 1 record in the users tableThen I should see a red error message in the top right cornerGiven I click the submit button # This is a When, not a Given!When I POST to /api/v1/users with JSON bodyThen the database should have 1 record in the users tableThen I should see a red error message in the top right cornerGiven I click the submit button # 这应该是When步骤,而非Given!When I POST to /api/v1/users with JSON body