Files
qa-test/features/step_definitions/crud.steps.js
2025-06-17 22:58:08 +08:00

94 lines
2.9 KiB
JavaScript

import { Given, When, Then } from '@cucumber/cucumber';
import assert from 'assert';
import axios from 'axios';
const BASE_URL = 'http://localhost:3000';
let response;
let createdRecord;
// Utility to log requests
function logRequest(method, url, data) {
console.log(`\n>>> REQUEST: ${method.toUpperCase()} ${url}`);
if (data) {
console.log('Payload:', JSON.stringify(data, null, 2));
}
}
// Utility to log responses
function logResponse(res) {
console.log(`<<< RESPONSE: Status ${res.status}`);
console.log('Response data:', JSON.stringify(res.data, null, 2));
}
Given('the API is running', async function () {
// Optionally ping /health or just assume running
// Example:
// const res = await axios.get(`${BASE_URL}/health`);
// assert.strictEqual(res.status, 200);
});
When('I create a record with name {string}, age {int}, email {string}', async function (name, age, email) {
const url = `${BASE_URL}/records`;
const payload = { name, age, email };
logRequest('post', url, payload);
response = await axios.post(url, payload);
logResponse(response);
createdRecord = response.data;
});
Then('the response status should be {int}', function (status) {
assert.strictEqual(response.status, status);
});
Then('the response should contain an id', function () {
assert.ok(createdRecord.id, 'Response does not contain an id');
});
Given('I have a created record with name {string}, age {int}, email {string}', async function (name, age, email) {
const url = `${BASE_URL}/records`;
const payload = { name, age, email };
logRequest('post', url, payload);
const res = await axios.post(url, payload);
logResponse(res);
createdRecord = res.data;
});
When('I get the record by ID', async function () {
const url = `${BASE_URL}/records/${createdRecord.id}`;
logRequest('get', url);
response = await axios.get(url);
logResponse(response);
});
Then(
'the response should contain the correct data for name {string}, age {int}, email {string}',
function (name, age, email) {
assert.strictEqual(response.data.name, name);
assert.strictEqual(response.data.age, age);
assert.strictEqual(response.data.email, email);
}
);
When('I update the record with name {string}, age {int}, email {string}', async function (name, age, email) {
const url = `${BASE_URL}/records/${createdRecord.id}`;
const payload = { name, age, email };
logRequest('put', url, payload);
response = await axios.put(url, payload);
logResponse(response);
});
Then('the response should contain updated true', function () {
assert.strictEqual(response.data.updated, true);
});
When('I delete the record by ID', async function () {
const url = `${BASE_URL}/records/${createdRecord.id}`;
logRequest('delete', url);
response = await axios.delete(url);
logResponse(response);
});
Then('the response should contain deleted true', function () {
assert.strictEqual(response.data.deleted, true);
});