Generate Cucumber step definitions for Playwright test automation following project patterns
This skill generates Cucumber step definitions for Playwright-based test automation following the project's established patterns.
Generates TypeScript step definition files following the project's patterns.
tests/steps/{Category}/{FeatureName}.tsimport { Given, When, Then } from '@cucumber/cucumber';
import Fixture from '@fixtures/Fixture';
import { expect } from '@fixtures/Playwright';
Given('step text here', async function (this: Fixture) {
// Setup code
// Navigate to pages, set initial state, prepare data
});
Example for UI:
Given('I am on the login page', async function (this: Fixture) {
await this.login.navigate();
});
Example for API:
Given('I send GET request to todos endpoint', async function (this: Fixture) {
this.response = await this.request.get('https://jsonplaceholder.typicode.com/todos');
});
When('step text here', async function (this: Fixture) {
// Perform actions
// Click buttons, fill forms, send requests
});
Example for UI:
When('I enter valid credentials', async function (this: Fixture) {
await this.login.login(this.utils.getUsername(), this.utils.getPassword());
await expect(this.secureArea.secureAreaHeader).toBeVisible();
});
Example for API:
When('I should receive a successful response with status code 200', async function (this: Fixture) {
expect(this.response.status()).toBe(200);
});
Then('step text here', async function (this: Fixture) {
// Verify expected outcomes
// Assert conditions, check responses
});
Example for UI:
Then('I should be logged in', async function (this: Fixture) {
await expect(this.page).toHaveURL(/\/secure/);
});
Example for API:
Then('The response should contain a list of todos', async function (this: Fixture) {
const responseBody = await this.response.json();
expect(Array.isArray(responseBody)).toBe(true);
expect(responseBody.length).toBeGreaterThan(0);
});
async function (this: Fixture) to access the fixture contextthis.page - Current page instancethis.request - API request contextthis.response - API response objectthis.login, this.secureArea, etc. - Page objectsthis.utils - Utility functionsexpect from @fixtures/Playwright for Playwright assertionsawait expect() for async assertions (UI)expect() without await for synchronous assertions (API responses)For steps with parameters, use Cucumber expressions:
String Parameters:
Given('I enter {string} as username', async function (this: Fixture, username: string) {
await this.login.enterUsername(username);
});
Number Parameters:
Then('I should see {int} items', async function (this: Fixture, count: number) {
const items = await this.page.locator('.item').count();
expect(items).toBe(count);
});
Table Parameters:
When('I fill the form with:', async function (this: Fixture, dataTable) {
const data = dataTable.rowsHash();
await this.page.fill('#username', data.username);
await this.page.fill('#password', data.password);
});
this.utils for common operations like getting credentialstests/steps/UI/{Feature}.ts - For browser interactionstests/steps/API/{Feature}.ts - For API callsimport { Given, When, Then } from '@cucumber/cucumber';
import Fixture from '@fixtures/Fixture';
import { expect } from '@fixtures/Playwright';
// Preconditions
Given('I am on the products page', async function (this: Fixture) {
await this.page.goto('/products');
});
Given('I have {int} items in my cart', async function (this: Fixture, count: number) {
// Implementation to add items to cart
for (let i = 0; i < count; i++) {
await this.page.click('.add-to-cart');
}
});
// Actions
When('I click on the first product', async function (this: Fixture) {
await this.page.click('.product-item:first-child');
});
When('I add the product to cart', async function (this: Fixture) {
await this.page.click('#add-to-cart-button');
});
// Assertions
Then('I should see the product details', async function (this: Fixture) {
await expect(this.page.locator('.product-details')).toBeVisible();
});
Then('The cart count should be {int}', async function (this: Fixture, expectedCount: number) {
const cartCount = await this.page.locator('.cart-count').textContent();
expect(parseInt(cartCount || '0')).toBe(expectedCount);
});
"Create step definitions for a products feature with steps to navigate to products page, filter by category, and verify product count"
"Create step definitions for a user API feature with steps to create a user, get user details, and verify the response"
"Create a step definition that accepts a username and password as parameters and logs in the user"