record-demo.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. import { chromium, Page, Browser, BrowserContext } from 'playwright';
  2. import path from 'path';
  3. import { fileURLToPath } from 'url';
  4. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  5. // Configuration
  6. const CONFIG = {
  7. baseUrl: process.env.DEMO_URL || 'http://localhost:8000',
  8. headless: process.env.HEADLESS === 'true',
  9. slowMo: 50, // Slow down actions for visibility
  10. viewportWidth: 1920,
  11. viewportHeight: 1080,
  12. outputDir: path.join(__dirname, 'output'),
  13. };
  14. // Timing helpers (in ms)
  15. const TIMING = {
  16. pageLoad: 1500, // Wait after page navigation
  17. shortPause: 500, // Brief pause between actions
  18. mediumPause: 1000, // Standard pause for visibility
  19. longPause: 2000, // Longer pause for important features
  20. modalOpen: 800, // Wait for modal animations
  21. scrollPause: 600, // Pause after scrolling
  22. };
  23. async function wait(ms: number): Promise<void> {
  24. return new Promise(resolve => setTimeout(resolve, ms));
  25. }
  26. async function scrollDown(page: Page, pixels: number = 300): Promise<void> {
  27. await page.mouse.wheel(0, pixels);
  28. await wait(TIMING.scrollPause);
  29. }
  30. async function scrollToTop(page: Page): Promise<void> {
  31. await page.evaluate(() => window.scrollTo({ top: 0, behavior: 'smooth' }));
  32. await wait(TIMING.scrollPause);
  33. }
  34. async function hoverElement(page: Page, selector: string): Promise<void> {
  35. const element = page.locator(selector).first();
  36. if (await element.isVisible()) {
  37. await element.hover();
  38. await wait(TIMING.shortPause);
  39. }
  40. }
  41. async function clickIfVisible(page: Page, selector: string): Promise<boolean> {
  42. const element = page.locator(selector).first();
  43. if (await element.isVisible()) {
  44. await element.click();
  45. return true;
  46. }
  47. return false;
  48. }
  49. async function closeModalIfOpen(page: Page): Promise<void> {
  50. // Try to close any open modal by pressing Escape
  51. await page.keyboard.press('Escape');
  52. await wait(TIMING.shortPause);
  53. }
  54. async function blurSensitiveContent(page: Page): Promise<void> {
  55. // Use JavaScript to find and blur email addresses
  56. await page.evaluate(() => {
  57. // Find all spans and check for email patterns
  58. document.querySelectorAll('span').forEach(el => {
  59. const text = el.textContent || '';
  60. // Check if this specific element (not children) contains an email
  61. if (el.childNodes.length === 1 && el.childNodes[0].nodeType === Node.TEXT_NODE) {
  62. if (text.includes('@') && text.includes('.')) {
  63. (el as HTMLElement).style.filter = 'blur(6px)';
  64. (el as HTMLElement).style.userSelect = 'none';
  65. }
  66. }
  67. });
  68. // Also find "Connected as" text and blur the next sibling span
  69. document.querySelectorAll('span').forEach(el => {
  70. if (el.textContent?.includes('Connected as')) {
  71. const emailSpan = el.querySelector('span');
  72. if (emailSpan) {
  73. (emailSpan as HTMLElement).style.filter = 'blur(6px)';
  74. (emailSpan as HTMLElement).style.userSelect = 'none';
  75. }
  76. }
  77. });
  78. });
  79. }
  80. // ============================================================================
  81. // Page Scenarios
  82. // ============================================================================
  83. async function demoPrintersPage(page: Page): Promise<void> {
  84. console.log('📷 Demonstrating Printers page...');
  85. await page.goto(CONFIG.baseUrl);
  86. await wait(TIMING.pageLoad);
  87. // Hover over printer cards to show interactions
  88. const printerCards = page.locator('.group').filter({ has: page.locator('img') });
  89. const cardCount = await printerCards.count();
  90. console.log(` Found ${cardCount} printer cards`);
  91. for (let i = 0; i < Math.min(cardCount, 2); i++) {
  92. const card = printerCards.nth(i);
  93. if (await card.isVisible()) {
  94. await card.hover();
  95. await wait(TIMING.mediumPause);
  96. // Try clicking on card to expand/show details
  97. await card.click();
  98. await wait(TIMING.mediumPause);
  99. }
  100. }
  101. // Look for AMS section and hover over slots
  102. const amsSlots = page.locator('[class*="ams"], [class*="AMS"]').first();
  103. if (await amsSlots.isVisible()) {
  104. await amsSlots.hover();
  105. await wait(TIMING.mediumPause);
  106. }
  107. // Try to open camera modal
  108. const cameraIcon = page.locator('svg[class*="lucide-video"], button:has(svg)').first();
  109. if (await cameraIcon.isVisible()) {
  110. await cameraIcon.click();
  111. await wait(TIMING.longPause);
  112. await page.keyboard.press('Escape');
  113. await wait(TIMING.shortPause);
  114. }
  115. // Try to open MQTT debug modal
  116. const debugButton = page.locator('button:has-text("Debug"), button:has-text("MQTT")').first();
  117. if (await debugButton.isVisible()) {
  118. await debugButton.click();
  119. await wait(TIMING.longPause);
  120. await page.keyboard.press('Escape');
  121. await wait(TIMING.shortPause);
  122. }
  123. // Scroll to show more printers
  124. await scrollDown(page, 400);
  125. await wait(TIMING.mediumPause);
  126. await scrollToTop(page);
  127. }
  128. async function demoArchivesPage(page: Page): Promise<void> {
  129. console.log('📁 Demonstrating Archives page...');
  130. await page.goto(`${CONFIG.baseUrl}/archives`);
  131. await wait(TIMING.pageLoad);
  132. // Show view mode toggle (grid/list/calendar)
  133. const viewToggle = page.locator('button:has(svg[class*="grid"]), button:has(svg[class*="list"])');
  134. if (await viewToggle.first().isVisible()) {
  135. await viewToggle.first().click();
  136. await wait(TIMING.mediumPause);
  137. await viewToggle.first().click(); // Toggle back
  138. await wait(TIMING.shortPause);
  139. }
  140. // Use search
  141. const searchInput = page.locator('input[placeholder*="Search"], input[type="search"]').first();
  142. if (await searchInput.isVisible()) {
  143. await searchInput.click();
  144. await searchInput.fill('engine');
  145. await wait(TIMING.longPause);
  146. await searchInput.clear();
  147. await wait(TIMING.shortPause);
  148. }
  149. // Show filter dropdowns
  150. const filterButtons = page.locator('button:has-text("Printer"), button:has-text("Material"), button:has-text("Filter")');
  151. if (await filterButtons.first().isVisible()) {
  152. await filterButtons.first().click();
  153. await wait(TIMING.mediumPause);
  154. await page.keyboard.press('Escape');
  155. await wait(TIMING.shortPause);
  156. }
  157. // Right-click to show context menu
  158. const archiveCard = page.locator('.group').filter({ has: page.locator('img') }).first();
  159. if (await archiveCard.isVisible()) {
  160. await archiveCard.click({ button: 'right' });
  161. await wait(TIMING.longPause);
  162. await page.keyboard.press('Escape');
  163. await wait(TIMING.shortPause);
  164. }
  165. // Click on archive to open edit modal
  166. if (await archiveCard.isVisible()) {
  167. await archiveCard.dblclick();
  168. await wait(TIMING.longPause);
  169. await page.keyboard.press('Escape');
  170. await wait(TIMING.shortPause);
  171. }
  172. // Scroll to show more archives
  173. await scrollDown(page, 500);
  174. await wait(TIMING.mediumPause);
  175. await scrollToTop(page);
  176. }
  177. async function demoQueuePage(page: Page): Promise<void> {
  178. console.log('📋 Demonstrating Queue page...');
  179. await page.goto(`${CONFIG.baseUrl}/queue`);
  180. await wait(TIMING.pageLoad);
  181. // Show filter dropdowns
  182. const printerFilter = page.locator('button:has-text("Printer"), select').first();
  183. if (await printerFilter.isVisible()) {
  184. await printerFilter.click();
  185. await wait(TIMING.mediumPause);
  186. await page.keyboard.press('Escape');
  187. await wait(TIMING.shortPause);
  188. }
  189. // Show sort controls
  190. const sortButton = page.locator('button:has-text("Sort"), button:has(svg[class*="arrow"])').first();
  191. if (await sortButton.isVisible()) {
  192. await sortButton.click();
  193. await wait(TIMING.mediumPause);
  194. }
  195. // Hover over queue items to show drag handles
  196. const queueItems = page.locator('[draggable="true"], .group').first();
  197. if (await queueItems.isVisible()) {
  198. await queueItems.hover();
  199. await wait(TIMING.mediumPause);
  200. }
  201. // Scroll through queue
  202. await scrollDown(page, 300);
  203. await wait(TIMING.mediumPause);
  204. await scrollToTop(page);
  205. }
  206. async function demoStatsPage(page: Page): Promise<void> {
  207. console.log('📊 Demonstrating Stats page...');
  208. await page.goto(`${CONFIG.baseUrl}/stats`);
  209. await wait(TIMING.pageLoad);
  210. // Let charts animate
  211. await wait(TIMING.longPause);
  212. // Show export dropdown
  213. const exportButton = page.locator('button:has-text("Export"), button:has(svg[class*="download"])').first();
  214. if (await exportButton.isVisible()) {
  215. await exportButton.click();
  216. await wait(TIMING.mediumPause);
  217. await page.keyboard.press('Escape');
  218. await wait(TIMING.shortPause);
  219. }
  220. // Scroll through stats widgets
  221. await scrollDown(page, 400);
  222. await wait(TIMING.mediumPause);
  223. await scrollDown(page, 400);
  224. await wait(TIMING.mediumPause);
  225. await scrollDown(page, 400);
  226. await wait(TIMING.mediumPause);
  227. await scrollToTop(page);
  228. }
  229. async function demoProfilesPage(page: Page): Promise<void> {
  230. console.log('⚙️ Demonstrating Profiles page...');
  231. // Start blur loop BEFORE navigating
  232. let blurring = true;
  233. const blurLoop = async () => {
  234. while (blurring) {
  235. try {
  236. await page.evaluate(() => {
  237. document.querySelectorAll('span').forEach(el => {
  238. if (el.textContent?.includes('Connected as')) {
  239. const emailSpan = el.querySelector('span');
  240. if (emailSpan) {
  241. (emailSpan as HTMLElement).style.filter = 'blur(6px)';
  242. }
  243. }
  244. });
  245. });
  246. } catch { /* page might be navigating */ }
  247. await new Promise(r => setTimeout(r, 30));
  248. }
  249. };
  250. // Start blur loop in background
  251. const blurPromise = blurLoop();
  252. await page.goto(`${CONFIG.baseUrl}/profiles`);
  253. await wait(TIMING.pageLoad);
  254. // Show Cloud Profiles section
  255. await wait(TIMING.mediumPause);
  256. // Click on K-Profiles tab if available
  257. try {
  258. const kProfilesTab = page.locator('button:has-text("K-Profile"), button:has-text("K Profile")').first();
  259. if (await kProfilesTab.isVisible({ timeout: 1000 })) {
  260. await kProfilesTab.click({ timeout: 2000 });
  261. await wait(TIMING.mediumPause);
  262. await scrollDown(page, 300);
  263. await wait(TIMING.shortPause);
  264. await scrollToTop(page);
  265. }
  266. } catch { /* skip */ }
  267. // Click back to Cloud Profiles
  268. try {
  269. const cloudTab = page.locator('button:has-text("Cloud")').first();
  270. if (await cloudTab.isVisible({ timeout: 1000 })) {
  271. await cloudTab.click({ timeout: 2000 });
  272. await wait(TIMING.mediumPause);
  273. }
  274. } catch { /* skip */ }
  275. // Show preset filter types (if visible) - use force to bypass overlays
  276. const presetFilters = page.locator('button:has-text("Filament"), button:has-text("Process"), button:has-text("Machine")');
  277. for (let i = 0; i < 3; i++) {
  278. try {
  279. const filter = presetFilters.nth(i);
  280. if (await filter.isVisible({ timeout: 1000 })) {
  281. await filter.click({ force: true, timeout: 2000 });
  282. await wait(TIMING.shortPause);
  283. }
  284. } catch { /* skip if not visible or blocked */ }
  285. }
  286. await scrollDown(page, 300);
  287. await wait(TIMING.shortPause);
  288. await scrollToTop(page);
  289. // Stop blur loop
  290. blurring = false;
  291. await blurPromise;
  292. }
  293. async function demoMaintenancePage(page: Page): Promise<void> {
  294. console.log('🔧 Demonstrating Maintenance page...');
  295. await page.goto(`${CONFIG.baseUrl}/maintenance`);
  296. await wait(TIMING.pageLoad);
  297. // Show status tab (default)
  298. await wait(TIMING.mediumPause);
  299. // Expand a printer section if available
  300. const expandButton = page.locator('button:has(svg[class*="chevron"])').first();
  301. if (await expandButton.isVisible()) {
  302. await expandButton.click();
  303. await wait(TIMING.mediumPause);
  304. }
  305. // Scroll through status
  306. await scrollDown(page, 300);
  307. await wait(TIMING.shortPause);
  308. await scrollToTop(page);
  309. // Click Settings tab
  310. const settingsTab = page.locator('button:has-text("Settings"), [role="tab"]:has-text("Settings")').first();
  311. if (await settingsTab.isVisible()) {
  312. await settingsTab.click();
  313. await wait(TIMING.mediumPause);
  314. // Scroll through settings
  315. await scrollDown(page, 300);
  316. await wait(TIMING.shortPause);
  317. await scrollToTop(page);
  318. }
  319. // Go back to Status tab
  320. const statusTab = page.locator('button:has-text("Status"), [role="tab"]:has-text("Status")').first();
  321. if (await statusTab.isVisible()) {
  322. await statusTab.click();
  323. await wait(TIMING.shortPause);
  324. }
  325. }
  326. async function demoProjectsPage(page: Page): Promise<void> {
  327. console.log('📂 Demonstrating Projects page...');
  328. await page.goto(`${CONFIG.baseUrl}/projects`);
  329. await wait(TIMING.pageLoad);
  330. // Click through status filter buttons
  331. const statusFilters = ['Active', 'Completed', 'Archived', 'All'];
  332. for (const status of statusFilters) {
  333. const filterBtn = page.locator(`button:has-text("${status}")`).first();
  334. if (await filterBtn.isVisible()) {
  335. await filterBtn.click();
  336. await wait(TIMING.shortPause);
  337. }
  338. }
  339. // Click on a project to go to detail page
  340. const projectCard = page.locator('.group, [class*="project"]').filter({ has: page.locator('h3, h2') }).first();
  341. if (await projectCard.isVisible()) {
  342. await projectCard.click();
  343. await wait(TIMING.pageLoad);
  344. // Scroll through project detail
  345. await scrollDown(page, 300);
  346. await wait(TIMING.mediumPause);
  347. // Look for tabs in project detail (BOM, Attachments, Prints)
  348. const detailTabs = ['BOM', 'Attachments', 'Prints', 'Notes'];
  349. for (const tabName of detailTabs) {
  350. const tab = page.locator(`button:has-text("${tabName}"), [role="tab"]:has-text("${tabName}")`).first();
  351. if (await tab.isVisible()) {
  352. await tab.click();
  353. await wait(TIMING.mediumPause);
  354. }
  355. }
  356. await scrollToTop(page);
  357. }
  358. }
  359. async function demoSettingsPage(page: Page): Promise<void> {
  360. console.log('⚙️ Demonstrating Settings page...');
  361. await page.goto(`${CONFIG.baseUrl}/settings`);
  362. await wait(TIMING.pageLoad);
  363. // Define the 6 tabs to click through
  364. const tabs = ['General', 'Plugs', 'Notifications', 'Filament', 'API', 'Virtual'];
  365. for (const tabName of tabs) {
  366. const tab = page.locator(`button:has-text("${tabName}"), [role="tab"]:has-text("${tabName}")`).first();
  367. if (await tab.isVisible()) {
  368. await tab.click();
  369. await wait(TIMING.mediumPause);
  370. // Scroll through tab content
  371. await scrollDown(page, 300);
  372. await wait(TIMING.shortPause);
  373. await scrollToTop(page);
  374. }
  375. }
  376. // Go back to General tab and show a modal
  377. const generalTab = page.locator('button:has-text("General")').first();
  378. if (await generalTab.isVisible()) {
  379. await generalTab.click();
  380. await wait(TIMING.shortPause);
  381. }
  382. // Try to open backup modal
  383. const backupButton = page.locator('button:has-text("Backup")').first();
  384. if (await backupButton.isVisible()) {
  385. await backupButton.click();
  386. await wait(TIMING.longPause);
  387. await page.keyboard.press('Escape');
  388. await wait(TIMING.shortPause);
  389. }
  390. // Go to Plugs tab and show add modal
  391. const plugsTab = page.locator('button:has-text("Plugs")').first();
  392. if (await plugsTab.isVisible()) {
  393. await plugsTab.click();
  394. await wait(TIMING.shortPause);
  395. const addPlugButton = page.locator('button:has-text("Add"), button:has(svg[class*="plus"])').first();
  396. if (await addPlugButton.isVisible()) {
  397. await addPlugButton.click();
  398. await wait(TIMING.longPause);
  399. await page.keyboard.press('Escape');
  400. await wait(TIMING.shortPause);
  401. }
  402. }
  403. // Go to Notifications tab and show add modal
  404. const notifTab = page.locator('button:has-text("Notifications")').first();
  405. if (await notifTab.isVisible()) {
  406. await notifTab.click();
  407. await wait(TIMING.shortPause);
  408. const addNotifButton = page.locator('button:has-text("Add"), button:has(svg[class*="plus"])').first();
  409. if (await addNotifButton.isVisible()) {
  410. await addNotifButton.click();
  411. await wait(TIMING.longPause);
  412. await page.keyboard.press('Escape');
  413. await wait(TIMING.shortPause);
  414. }
  415. }
  416. await scrollToTop(page);
  417. }
  418. async function demoSystemPage(page: Page): Promise<void> {
  419. console.log('💻 Demonstrating System page...');
  420. await page.goto(`${CONFIG.baseUrl}/system`);
  421. await wait(TIMING.pageLoad);
  422. // Show system info
  423. await wait(TIMING.mediumPause);
  424. await scrollDown(page, 300);
  425. await wait(TIMING.shortPause);
  426. await scrollToTop(page);
  427. }
  428. // ============================================================================
  429. // Main Recording Function
  430. // ============================================================================
  431. async function recordDemo(): Promise<void> {
  432. console.log('🎬 Starting Bambuddy demo recording...');
  433. console.log(` URL: ${CONFIG.baseUrl}`);
  434. console.log(` Resolution: ${CONFIG.viewportWidth}x${CONFIG.viewportHeight}`);
  435. console.log(` Headless: ${CONFIG.headless}`);
  436. console.log('');
  437. const browser: Browser = await chromium.launch({
  438. headless: CONFIG.headless,
  439. slowMo: CONFIG.slowMo,
  440. });
  441. const context: BrowserContext = await browser.newContext({
  442. viewport: {
  443. width: CONFIG.viewportWidth,
  444. height: CONFIG.viewportHeight,
  445. },
  446. recordVideo: {
  447. dir: CONFIG.outputDir,
  448. size: {
  449. width: CONFIG.viewportWidth,
  450. height: CONFIG.viewportHeight,
  451. },
  452. },
  453. });
  454. const page: Page = await context.newPage();
  455. try {
  456. // Run through all page demos
  457. await demoPrintersPage(page);
  458. await demoArchivesPage(page);
  459. await demoQueuePage(page);
  460. await demoStatsPage(page);
  461. await demoProfilesPage(page);
  462. await demoMaintenancePage(page);
  463. await demoProjectsPage(page);
  464. await demoSettingsPage(page);
  465. await demoSystemPage(page);
  466. // Return to home page for closing shot
  467. console.log('🏠 Returning to home page...');
  468. await page.goto(CONFIG.baseUrl);
  469. await wait(TIMING.longPause);
  470. console.log('✅ Demo recording completed!');
  471. } catch (error) {
  472. console.error('❌ Error during recording:', error);
  473. throw error;
  474. } finally {
  475. await page.close();
  476. await context.close();
  477. await browser.close();
  478. }
  479. console.log(`\n📹 Video saved to: ${CONFIG.outputDir}/`);
  480. console.log(' (Playwright saves as .webm, convert with ffmpeg if needed)');
  481. console.log(' Example: ffmpeg -i video.webm -c:v libx264 demo.mp4');
  482. }
  483. // Run the recording
  484. recordDemo().catch(console.error);