test-v3.tsโข3.47 kB
/**
* Test script for v3 architecture
* Tests the new Microsoft-based snapshot system with ref identifiers
*/
import { PlaywrightClient } from './dist/client/playwright-client.js';
async function test() {
const client = new PlaywrightClient('http://localhost:3103');
try {
console.log('๐งช Testing v3 Architecture\n');
console.log('='.repeat(50));
// Test 1: Create page
console.log('\n๐ Test 1: Create Page');
const { pageId, snapshot } = await client.createPage(
'test-page',
'Test page for v3',
'https://example.com'
);
console.log(`โ
Page created: ${pageId}`);
console.log(`๐ URL: ${snapshot.url}`);
console.log(`๐ Title: ${snapshot.title}`);
// Test 2: Check snapshot format
console.log('\n๐ Test 2: Snapshot Format');
console.log('Snapshot preview (first 500 chars):');
console.log(snapshot.snapshot.substring(0, 500));
// Test 3: Parse refs from snapshot
console.log('\n๐ Test 3: Ref Parsing');
const refMatches = snapshot.snapshot.match(/\[ref=([^\]]+)\]/g);
if (refMatches) {
console.log(`โ
Found ${refMatches.length} refs:`);
refMatches.slice(0, 5).forEach(ref => {
console.log(` ${ref}`);
});
} else {
console.log('โ No refs found in snapshot');
}
// Test 4: Click using ref (if found)
if (refMatches && refMatches.length > 0) {
console.log('\n๐ Test 4: Click Action');
// Extract ref ID from first match
const firstRef = refMatches[0].match(/\[ref=([^\]]+)\]/)?.[1];
if (firstRef) {
console.log(`๐ฑ๏ธ Attempting to click ref: ${firstRef}`);
try {
const clickResult = await client.click(pageId, firstRef, 'First element');
console.log(`โ
Click successful`);
console.log(`๐ New URL: ${clickResult.url}`);
} catch (error: any) {
console.log(`โ ๏ธ Click failed: ${error.message}`);
}
}
}
// Test 5: Navigate
console.log('\n๐ Test 5: Navigation');
const navResult = await client.navigate(pageId, 'https://www.google.com');
console.log(`โ
Navigated to: ${navResult.url}`);
console.log(`๐ New title: ${navResult.title}`);
// Test 6: Get fresh snapshot
console.log('\n๐ Test 6: Fresh Snapshot');
const freshSnapshot = await client.getSnapshot(pageId);
const freshRefs = freshSnapshot.snapshot.match(/\[ref=([^\]]+)\]/g);
if (freshRefs) {
console.log(`โ
Fresh snapshot has ${freshRefs.length} refs`);
}
// Test 7: Screenshot
console.log('\n๐ Test 7: Screenshot');
const screenshot = await client.screenshot(pageId);
console.log(`โ
Screenshot captured (${screenshot.length} bytes base64)`);
// Test 8: List pages
console.log('\n๐ Test 8: List Pages');
const pages = await client.listPages();
console.log(`โ
Found ${pages.length} page(s):`);
pages.forEach(p => {
console.log(` - ${p.name}: ${p.url}`);
});
// Test 9: Close page
console.log('\n๐ Test 9: Close Page');
await client.closePage(pageId);
console.log('โ
Page closed');
console.log('\n' + '='.repeat(50));
console.log('โ
All tests completed successfully!');
} catch (error: any) {
console.error('\nโ Test failed:', error.message);
process.exit(1);
}
}
// Run tests
test().catch(console.error);