-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-test-utils.js
More file actions
232 lines (199 loc) · 8.13 KB
/
Copy pathai-test-utils.js
File metadata and controls
232 lines (199 loc) · 8.13 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// ai-test-utils.js
/**
* AI-Enhanced Testing Utilities for Playwright
* These utilities leverage Playwright's latest AI features for smarter testing
*/
class AITestUtils {
/**
* Smart locator that uses AI-powered element detection
* @param {import('@playwright/test').Page} page
* @param {string} description - Natural language description of the element
* @returns {import('@playwright/test').Locator}
*/
static smartLocator(page, description) {
const desc = description.toLowerCase();
// Enhanced strategies for form fields
if (desc.includes('username') || desc.includes('user name')) {
return page.locator('[data-test="username"], input[name="username"], input[name="user-name"], input[placeholder*="username" i], input[id*="username" i]').first();
}
if (desc.includes('password')) {
return page.locator('[data-test="password"], input[type="password"], input[name="password"], input[placeholder*="password" i]').first();
}
if (desc.includes('login') && desc.includes('button')) {
return page.locator('[data-test="login-button"], button[type="submit"], input[type="submit"], button:has-text("Login"), button:has-text("Sign in")').first();
}
// Use getByRole with AI-enhanced fallbacks
const strategies = [
() => page.getByRole('button', { name: new RegExp(description, 'i') }),
() => page.getByRole('link', { name: new RegExp(description, 'i') }),
() => page.getByRole('textbox', { name: new RegExp(description, 'i') }),
() => page.getByLabel(new RegExp(description, 'i')),
() => page.getByPlaceholder(new RegExp(description, 'i')),
() => page.getByText(new RegExp(description, 'i')),
() => page.getByTestId(description.toLowerCase().replace(/\s+/g, '-')),
() => page.locator(`[aria-label*="${description}" i]`),
() => page.locator(`[title*="${description}" i]`),
() => page.locator(`[alt*="${description}" i]`)
];
for (const strategy of strategies) {
try {
const locator = strategy();
if (locator) return locator;
} catch (error) {
// Continue to next strategy
}
}
// Fallback to text-based search
return page.locator(`text=${description}`).first();
}
/**
* AI-powered form filling with smart field detection
* @param {import('@playwright/test').Page} page
* @param {Object} formData - Key-value pairs for form fields
*/
static async smartFillForm(page, formData) {
for (const [fieldName, value] of Object.entries(formData)) {
const field = this.smartLocator(page, fieldName);
await field.waitFor({ state: 'visible' });
await field.fill(value);
}
}
/**
* Enhanced wait for page readiness with AI insights
* @param {import('@playwright/test').Page} page
* @param {Object} options - Wait options
*/
static async waitForPageReady(page, options = {}) {
const { timeout = 30000, checkInterval = 100 } = options;
// Wait for network to be idle
await page.waitForLoadState('networkidle');
// Wait for no ongoing animations
await page.waitForFunction(() => {
const animations = document.getAnimations();
return animations.length === 0 || animations.every(anim => anim.playState === 'finished');
}, { timeout });
// Check for loading indicators
await page.waitForFunction(() => {
const loadingElements = document.querySelectorAll(
'[class*="loading"], [class*="spinner"], [aria-label*="loading" i]'
);
return Array.from(loadingElements).every(el =>
getComputedStyle(el).display === 'none' ||
getComputedStyle(el).visibility === 'hidden'
);
}, { timeout });
}
/**
* AI-enhanced screenshot with automatic naming and metadata
* @param {import('@playwright/test').Page} page
* @param {string} testName
* @param {string} step
*/
static async smartScreenshot(page, testName, step) {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `${testName}-${step}-${timestamp}.png`;
await page.screenshot({
path: `tests/screenshots/${filename}`,
fullPage: true,
animations: 'disabled'
});
return filename;
}
/**
* Enhanced error handling with AI-powered debugging hints
* @param {Error} error
* @param {import('@playwright/test').Page} page
* @param {string} context
*/
static async enhancedErrorHandling(error, page, context) {
console.log(`🤖 AI Debug Assistant for: ${context}`);
console.log(`Error: ${error.message}`);
// Capture debug information
const url = page.url();
const title = await page.title().catch(() => 'Unknown');
// Suggest debugging strategies based on error type
const suggestions = this.getDebugSuggestions(error, url);
console.log(`Current URL: ${url}`);
console.log(`Page Title: ${title}`);
console.log(`🔍 Debugging Suggestions:`);
suggestions.forEach((suggestion, index) => {
console.log(` ${index + 1}. ${suggestion}`);
});
// Take screenshot for analysis
await this.smartScreenshot(page, 'error', context);
throw error;
}
/**
* Generate AI-powered debugging suggestions
* @param {Error} error
* @param {string} url
* @returns {string[]}
*/
static getDebugSuggestions(error, url) {
const suggestions = [];
const errorMessage = error.message.toLowerCase();
if (errorMessage.includes('timeout')) {
suggestions.push('Element may be loading slowly - try increasing timeout or wait for specific conditions');
suggestions.push('Check if there are any network requests still pending');
suggestions.push('Verify the element selector is correct and specific enough');
}
if (errorMessage.includes('not found') || errorMessage.includes('not visible')) {
suggestions.push('Element selector might be incorrect - try using data-testid or more specific locators');
suggestions.push('Element might be hidden behind another element or in a different iframe');
suggestions.push('Wait for the element to appear before interacting with it');
}
if (errorMessage.includes('navigation')) {
suggestions.push('Check if the URL is correct and accessible');
suggestions.push('Verify network connectivity and server availability');
suggestions.push('Look for redirects or authentication requirements');
}
if (url.includes('saucedemo')) {
suggestions.push('For SauceDemo: Ensure user credentials are correct for the test user type');
suggestions.push('Check if the user account is locked or has specific limitations');
}
return suggestions;
}
/**
* Smart element interaction with retry logic
* @param {import('@playwright/test').Locator} locator
* @param {string} action
* @param {any} value
* @param {Object} options
*/
static async smartInteract(locator, action, value = null, options = {}) {
const maxRetries = options.retries || 3;
const retryDelay = options.delay || 1000;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
await locator.waitFor({ state: 'visible', timeout: 5000 });
switch (action) {
case 'click':
await locator.click(options);
break;
case 'fill':
await locator.fill(value, options);
break;
case 'select':
await locator.selectOption(value, options);
break;
case 'check':
await locator.check(options);
break;
case 'uncheck':
await locator.uncheck(options);
break;
default:
throw new Error(`Unknown action: ${action}`);
}
return; // Success
} catch (error) {
if (attempt === maxRetries) {
throw new Error(`Failed to ${action} after ${maxRetries} attempts: ${error.message}`);
}
console.log(`Attempt ${attempt} failed, retrying in ${retryDelay}ms...`);
await new Promise(resolve => setTimeout(resolve, retryDelay));
}
}
}
}
module.exports = { AITestUtils };