-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api-capture.js
More file actions
82 lines (70 loc) · 2.94 KB
/
Copy pathtest-api-capture.js
File metadata and controls
82 lines (70 loc) · 2.94 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
const { chromium } = require('playwright');
(async () => {
console.log('Testing WordPress API Response Capture');
console.log('=========================================');
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext();
const page = await context.newPage();
// Capture ALL network requests
const apiCalls = [];
page.on('response', response => {
const url = response.url();
if (url.includes('/wp-json/') || url.includes('/wp-admin/')) {
apiCalls.push({
url: url,
status: response.status(),
method: response.request().method()
});
}
});
try {
// Login
console.log('1. Logging in...');
await page.goto('http://localhost:8080/wp-login.php');
await page.fill('#user_login', 'admin');
await page.fill('#user_pass', '!3cTXkh)9iDHhV5o*N');
await page.click('#wp-submit');
await page.waitForURL('**/wp-admin/**', { timeout: 10000 });
console.log(' Logged in');
// Create new post
console.log('2. Creating new post...');
await page.goto('http://localhost:8080/wp-admin/post-new.php');
await page.waitForTimeout(3000);
// Fill title
await page.fill('h1[aria-label="Add title"], .editor-post-title__input', 'API Test Post');
// Save with keyboard shortcut
console.log('3. Saving post...');
await page.keyboard.press('Control+s');
await page.waitForTimeout(3000);
// Display all API calls
console.log('4. API Calls Made:');
let postCallCount = 0;
apiCalls.forEach((call) => {
if (call.url.includes('/wp-json/wp/v2/posts')) {
postCallCount++;
const urlPart = call.url.substring(call.url.indexOf('/wp-json/'));
console.log(' ' + postCallCount + '. ' + call.method + ' ' + urlPart + ' - Status: ' + call.status);
}
});
// Check for success message
const savedMessage = await page.locator('.components-snackbar__content, .editor-post-saved-state').textContent().catch(() => '');
if (savedMessage) {
console.log('5. Editor message: ' + savedMessage);
}
// Get the post ID if available
const postId = await page.evaluate(() => {
const editor = window.wp && window.wp.data && window.wp.data.select('core/editor');
return editor ? editor.getCurrentPostId() : null;
}).catch(() => null);
if (postId) {
console.log('SUCCESS - Post created with ID: ' + postId);
} else {
console.log('Could not retrieve post ID from editor');
}
} catch (error) {
console.error('Test error:', error.message);
} finally {
await browser.close();
console.log('=========================================');
}
})();