File size: 1,407 Bytes
1dbc34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Page, Locator } from '@playwright/test';

/**
 * Sanitize a string for use in data-testid selectors.
 * This mirrors the sanitizeForTestId function in apps/ui/src/lib/utils.ts
 * to ensure tests use the same sanitization logic as the component.
 *
 * @param name - The string to sanitize (e.g., project name)
 * @returns A sanitized string safe for CSS selectors
 */
export function sanitizeForTestId(name: string): string {
  return name
    .toLowerCase()
    .replace(/\s+/g, '-')
    .replace(/[^a-z0-9-]/g, '')
    .replace(/-+/g, '-')
    .replace(/^-|-$/g, '');
}

/**
 * Get an element by its data-testid attribute
 */
export async function getByTestId(page: Page, testId: string): Promise<Locator> {
  return page.locator(`[data-testid="${testId}"]`);
}

/**
 * Get a button by its text content
 */
export async function getButtonByText(page: Page, text: string): Promise<Locator> {
  return page.locator(`button:has-text("${text}")`);
}

/**
 * Get the category autocomplete input element
 */
export async function getCategoryAutocompleteInput(
  page: Page,
  testId: string = 'feature-category-input'
): Promise<Locator> {
  return page.locator(`[data-testid="${testId}"]`);
}

/**
 * Get the category autocomplete dropdown list
 */
export async function getCategoryAutocompleteList(page: Page): Promise<Locator> {
  return page.locator('[data-testid="category-autocomplete-list"]');
}