Skip to content

Commit f07150e

Browse files
committed
fix(tests): improve flaky tests
1 parent 5429c08 commit f07150e

2 files changed

Lines changed: 74 additions & 60 deletions

File tree

tests/e2e/store-admin/comprehensive.spec.ts

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,37 @@ test.describe('store-admin comprehensive tests', () => {
2121
// Wait for page to load
2222
await page.waitForTimeout(10000);
2323

24-
// Try to use AI Assistant if available
24+
// Try to use AI Assistant if available, with fallback to manual description
2525
const askAIAssistantButton = page.locator('button:has-text("Ask AI Assistant")');
2626
if (await askAIAssistantButton.isVisible()) {
2727
await askAIAssistantButton.click();
28-
await page.waitForResponse(response =>
29-
response.url().includes('/api/ai/generate/description') && response.status() === 200
30-
);
28+
try {
29+
await page.waitForResponse(
30+
response => response.url().includes('/api/ai/generate/description'),
31+
{ timeout: 30000 }
32+
);
33+
} catch {
34+
console.log('AI Assistant timed out, falling back to manual description');
35+
await page.getByRole('textbox', { name: 'Description' }).fill('Something tasty for the pups');
36+
}
3137
} else {
32-
// Fallback to manual description
38+
console.log('AI Assistant button not found, using manual description');
3339
await page.getByRole('textbox', { name: 'Description' }).fill('Something tasty for the pups');
3440
}
3541

36-
// Handle success dialog
37-
page.once('dialog', dialog => {
38-
console.log(`Dialog message: ${dialog.message()}`);
39-
expect(dialog.message()).toBe('Product saved successfully');
40-
dialog.dismiss().catch(() => { });
41-
});
42+
// Ensure description has a value before saving
43+
const descriptionField = page.getByRole('textbox', { name: 'Description' });
44+
if (await descriptionField.inputValue() === '') {
45+
await descriptionField.fill('Something tasty for the pups');
46+
}
4247

43-
// Save the product
48+
// Save the product and wait for the dialog response
49+
const dialogPromise = page.waitForEvent('dialog');
4450
await page.getByRole('button', { name: 'Save Product' }).click();
51+
const dialog = await dialogPromise;
52+
console.log(`Dialog message: ${dialog.message()}`);
53+
expect(dialog.message()).toBe('Product saved successfully');
54+
await dialog.dismiss();
4555
});
4656

4757
test('can view and manage products list', async ({ page }) => {
@@ -64,7 +74,7 @@ test.describe('store-admin comprehensive tests', () => {
6474
// Verify first product has expected company name
6575
const firstProduct = productRows.first();
6676
const productName = firstProduct.locator('td').nth(1); // Assuming name is in second column
67-
77+
6878
// Check if product contains company name
6979
const productNameText = await productName.textContent();
7080
if (productNameText && productNameText.includes('Bike')) {
@@ -83,10 +93,10 @@ test.describe('store-admin comprehensive tests', () => {
8393
const productLinks = page.locator('a[href*="/product/"]');
8494
if (await productLinks.count() > 0) {
8595
await productLinks.first().click();
86-
96+
8797
// Verify we're on product detail page
8898
await expect(page.url()).toContain('/product/');
89-
99+
90100
// Check for product detail elements
91101
const productTitle = page.locator('h1, h2, .product-title');
92102
if (await productTitle.count() > 0) {
@@ -102,10 +112,10 @@ test.describe('store-admin comprehensive tests', () => {
102112
const ordersLink = page.getByRole('link', { name: /Orders?/ });
103113
if (await ordersLink.isVisible()) {
104114
await ordersLink.click();
105-
115+
106116
// Verify orders page loaded
107117
await expect(page.url()).toContain('order');
108-
118+
109119
// Check for orders table/list elements
110120
const ordersTable = page.locator('table, .orders-list, [data-testid="orders"]');
111121
if (await ordersTable.count() > 0) {
@@ -125,7 +135,7 @@ test.describe('store-admin comprehensive tests', () => {
125135
const saveButton = page.getByRole('button', { name: 'Save Product' });
126136
if (await saveButton.isVisible()) {
127137
await saveButton.click();
128-
138+
129139
// Look for validation messages
130140
const validationMessages = page.locator('.error, .validation-error, [role="alert"]');
131141
if (await validationMessages.count() > 0) {
@@ -142,7 +152,7 @@ test.describe('store-admin comprehensive tests', () => {
142152

143153
// Navigate to Products page and verify branding consistency
144154
await page.getByRole('link', { name: 'Products' }).click();
145-
155+
146156
// Check if page maintains branding
147157
const headerElements = page.locator('h1, h2, .header, .page-title');
148158
if (await headerElements.count() > 0) {
@@ -174,18 +184,18 @@ test.describe('store-admin comprehensive tests', () => {
174184
const askAIButton = page.locator('button:has-text("Ask AI Assistant")');
175185
if (await askAIButton.isVisible()) {
176186
await askAIButton.click();
177-
187+
178188
// Wait for AI response with timeout
179189
try {
180190
await page.waitForResponse(
181191
response => response.url().includes('/api/ai/generate/description') && response.status() === 200,
182192
{ timeout: 30000 }
183193
);
184-
194+
185195
// Verify description field was populated
186196
const descriptionField = page.getByRole('textbox', { name: 'Description' });
187197
await expect(descriptionField).not.toHaveValue('');
188-
198+
189199
console.log('AI Assistant successfully generated product description');
190200
} catch (error) {
191201
console.log('AI Assistant not available or timeout occurred, using manual description');
@@ -204,7 +214,7 @@ test.describe('store-admin comprehensive tests', () => {
204214
// Test mobile viewport
205215
await page.setViewportSize({ width: 375, height: 667 });
206216
await page.getByRole('link', { name: 'Products' }).click();
207-
217+
208218
// Verify basic functionality works on mobile
209219
await expect(page.getByRole('button', { name: 'Add Product' })).toBeVisible();
210220

@@ -226,17 +236,17 @@ test.describe('store-admin comprehensive tests', () => {
226236

227237
// Try to access a non-existent product
228238
await page.goto(`${testConfig.storeAdminUrl}/product/non-existent-id`);
229-
239+
230240
// Should handle error gracefully (either redirect or show error message)
231241
await page.waitForTimeout(3000);
232-
242+
233243
// Check if we're redirected or if error message is shown
234244
const currentUrl = page.url();
235245
const errorElements = page.locator('.error, .not-found, [role="alert"]');
236-
246+
237247
const isRedirected = !currentUrl.includes('non-existent-id');
238248
const hasErrorMessage = await errorElements.count() > 0;
239-
249+
240250
expect(isRedirected || hasErrorMessage).toBe(true);
241251
});
242252
});

0 commit comments

Comments
 (0)