Files
n8n/packages/testing/playwright/services/variables-api-helper.ts
alighasami 3d5eaf9445
Some checks failed
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
first commit
2026-03-17 16:22:57 +03:30

80 lines
1.9 KiB
TypeScript

import type { ApiHelpers } from './api-helper';
import { TestError } from '../Types';
interface VariableResponse {
id: string;
key: string;
value: string;
}
interface CreateVariableDto {
key: string;
value: string;
projectId?: string;
}
export class VariablesApiHelper {
constructor(private api: ApiHelpers) {}
/**
* Create a new variable
*/
async createVariable(variable: CreateVariableDto): Promise<VariableResponse> {
const response = await this.api.request.post('/rest/variables', { data: variable });
if (!response.ok()) {
throw new TestError(`Failed to create variable: ${await response.text()}`);
}
const result = await response.json();
return result.data ?? result;
}
/**
* Get all variables
*/
async getAllVariables(): Promise<VariableResponse[]> {
const response = await this.api.request.get('/rest/variables');
if (!response.ok()) {
throw new TestError(`Failed to get variables: ${await response.text()}`);
}
const result = await response.json();
return result.data ?? result;
}
/**
* Delete a variable by ID
*/
async deleteVariable(id: string): Promise<void> {
const response = await this.api.request.delete(`/rest/variables/${id}`);
if (!response.ok()) {
throw new TestError(`Failed to delete variable: ${await response.text()}`);
}
}
/**
* Delete all variables (useful for test cleanup)
*/
async deleteAllVariables(): Promise<void> {
const variables = await this.getAllVariables();
// Delete variables in parallel for better performance
await Promise.all(variables.map((variable) => this.deleteVariable(variable.id)));
}
/**
* Create a test variable with a unique key
*/
async createTestVariable(
keyPrefix: string = 'TEST_VAR',
value: string = 'test_value',
projectId?: string,
): Promise<VariableResponse> {
const key = `${keyPrefix}_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
return await this.createVariable({ key, value, projectId });
}
}