dina1 commited on
Commit
82eb828
·
verified ·
1 Parent(s): db6a6b5

Update playwright_model.py

Browse files
Files changed (1) hide show
  1. playwright_model.py +36 -43
playwright_model.py CHANGED
@@ -17,22 +17,45 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
17
  print(f"Opening page: {public_url}")
18
  await page.goto(public_url, wait_until="load")
19
 
20
- # ✅ Improved sidebar + layout stability detector
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  async def wait_for_sidebar_stable():
22
  try:
23
  await page.evaluate("""
24
  (async () => {
25
  const sidebar = document.querySelector('aside, .sidebar, nav');
26
  if (!sidebar) return;
27
-
28
  function getMetrics() {
29
  const rect = sidebar.getBoundingClientRect();
30
  return { left: rect.left, width: rect.width };
31
  }
32
-
33
  let stableCount = 0;
34
  let last = getMetrics();
35
-
36
  await new Promise((resolve) => {
37
  const check = setInterval(() => {
38
  const now = getMetrics();
@@ -43,13 +66,13 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
43
  stableCount++;
44
  if (stableCount >= 5) {
45
  clearInterval(check);
46
- setTimeout(resolve, 400); // safety delay
47
  }
48
  } else {
49
  stableCount = 0;
50
  last = now;
51
  }
52
- }, 120);
53
  });
54
  })();
55
  """)
@@ -79,7 +102,7 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
79
  })();
80
  """)
81
 
82
- # Inject traversal logic
83
  js_logic = """
84
  (function(){
85
  const menus=[...document.querySelectorAll('.menu-item')];
@@ -146,7 +169,6 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
146
  screenshots = []
147
  index = 0
148
 
149
- # ✅ Loop through workflows
150
  while True:
151
  result = await page.evaluate("window.__captureNext()")
152
  if not result:
@@ -168,27 +190,13 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
168
 
169
  await wait_for_sidebar_stable()
170
  await wait_for_layout_stable()
 
 
171
 
172
- # ✅ Fix sidebar height mismatch before capture
173
- await page.evaluate("""
174
- const sidebar = document.querySelector('aside, .sidebar, nav');
175
- const main = document.querySelector('main, .main-content, .app-body, .content');
176
- if (sidebar) {
177
- const fullHeight = Math.max(
178
- document.body.scrollHeight,
179
- document.documentElement.scrollHeight,
180
- main ? main.scrollHeight : 0
181
- );
182
- sidebar.style.minHeight = fullHeight + 'px';
183
- sidebar.style.height = fullHeight + 'px';
184
- }
185
- """)
186
-
187
- await asyncio.sleep(1.0)
188
  await page.screenshot(path=screenshot_path, full_page=True)
189
  screenshots.append(screenshot_path)
190
 
191
- # Capture sub-screens
192
  first_active_skipped = False
193
  for sub in sub_screens:
194
  is_active = await page.evaluate(
@@ -209,23 +217,8 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
209
  await page.evaluate(f"window.__clickSubScreen('{sub}')")
210
  await wait_for_sidebar_stable()
211
  await wait_for_layout_stable()
212
-
213
- # ✅ Sidebar height fix again for sub-screens
214
- await page.evaluate("""
215
- const sidebar = document.querySelector('aside, .sidebar, nav');
216
- const main = document.querySelector('main, .main-content, .app-body, .content');
217
- if (sidebar) {
218
- const fullHeight = Math.max(
219
- document.body.scrollHeight,
220
- document.documentElement.scrollHeight,
221
- main ? main.scrollHeight : 0
222
- );
223
- sidebar.style.minHeight = fullHeight + 'px';
224
- sidebar.style.height = fullHeight + 'px';
225
- }
226
- """)
227
-
228
- await asyncio.sleep(1.0)
229
 
230
  sub_name_clean = sub.replace(" ", "_").lower()
231
  sub_path = os.path.join(OUTPUT_DIR, f"{screen_name}_{sub_name_clean}.png")
@@ -267,4 +260,4 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
267
  return pdf_path
268
 
269
 
270
- generate_ui_report = capture_workflows
 
17
  print(f"Opening page: {public_url}")
18
  await page.goto(public_url, wait_until="load")
19
 
20
+ # ✅ Detect and freeze sidebar layout to fixed position
21
+ async def fix_sidebar_position():
22
+ try:
23
+ await page.evaluate("""
24
+ const sidebar = document.querySelector('aside, .sidebar, nav');
25
+ if (sidebar) {
26
+ // Lock the sidebar in a consistent fixed position
27
+ const rect = sidebar.getBoundingClientRect();
28
+ sidebar.style.position = 'fixed';
29
+ sidebar.style.top = '0px';
30
+ sidebar.style.left = rect.left + 'px';
31
+ sidebar.style.width = rect.width + 'px';
32
+ sidebar.style.height = '100vh';
33
+ sidebar.style.zIndex = '1000';
34
+ sidebar.style.boxShadow = '2px 0 6px rgba(0,0,0,0.05)';
35
+ sidebar.style.overflow = 'hidden';
36
+ }
37
+
38
+ // Shift main content to align with fixed sidebar
39
+ const main = document.querySelector('main, .main-content, .app-body, .content');
40
+ if (main && sidebar) {
41
+ main.style.marginLeft = sidebar.offsetWidth + 'px';
42
+ }
43
+ """)
44
+ except Exception as e:
45
+ print(f"[WARN] Failed to fix sidebar: {e}")
46
+
47
  async def wait_for_sidebar_stable():
48
  try:
49
  await page.evaluate("""
50
  (async () => {
51
  const sidebar = document.querySelector('aside, .sidebar, nav');
52
  if (!sidebar) return;
 
53
  function getMetrics() {
54
  const rect = sidebar.getBoundingClientRect();
55
  return { left: rect.left, width: rect.width };
56
  }
 
57
  let stableCount = 0;
58
  let last = getMetrics();
 
59
  await new Promise((resolve) => {
60
  const check = setInterval(() => {
61
  const now = getMetrics();
 
66
  stableCount++;
67
  if (stableCount >= 5) {
68
  clearInterval(check);
69
+ setTimeout(resolve, 300);
70
  }
71
  } else {
72
  stableCount = 0;
73
  last = now;
74
  }
75
+ }, 100);
76
  });
77
  })();
78
  """)
 
102
  })();
103
  """)
104
 
105
+ # Inject traversal logic
106
  js_logic = """
107
  (function(){
108
  const menus=[...document.querySelectorAll('.menu-item')];
 
169
  screenshots = []
170
  index = 0
171
 
 
172
  while True:
173
  result = await page.evaluate("window.__captureNext()")
174
  if not result:
 
190
 
191
  await wait_for_sidebar_stable()
192
  await wait_for_layout_stable()
193
+ await fix_sidebar_position()
194
+ await asyncio.sleep(0.8)
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  await page.screenshot(path=screenshot_path, full_page=True)
197
  screenshots.append(screenshot_path)
198
 
199
+ # Capture sub-screens
200
  first_active_skipped = False
201
  for sub in sub_screens:
202
  is_active = await page.evaluate(
 
217
  await page.evaluate(f"window.__clickSubScreen('{sub}')")
218
  await wait_for_sidebar_stable()
219
  await wait_for_layout_stable()
220
+ await fix_sidebar_position()
221
+ await asyncio.sleep(0.8)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
 
223
  sub_name_clean = sub.replace(" ", "_").lower()
224
  sub_path = os.path.join(OUTPUT_DIR, f"{screen_name}_{sub_name_clean}.png")
 
260
  return pdf_path
261
 
262
 
263
+ generate_ui_report = capture_workflows