dina1 commited on
Commit
c2862a2
Β·
verified Β·
1 Parent(s): 56af846

Update playwright_model.py

Browse files
Files changed (1) hide show
  1. playwright_model.py +50 -21
playwright_model.py CHANGED
@@ -17,22 +17,19 @@ 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 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 getLeft() {
29
  const rect = sidebar.getBoundingClientRect();
30
  return rect.left;
31
  }
32
-
33
  let stableCount = 0;
34
  let lastLeft = getLeft();
35
-
36
  await new Promise((resolve) => {
37
  const check = setInterval(() => {
38
  const currentLeft = getLeft();
@@ -53,7 +50,7 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
53
  except Exception as e:
54
  print(f"[WARN] Sidebar stability check failed: {e}")
55
 
56
- # βœ… Inject workflow traversal + sub-screen logic
57
  js_logic = """
58
  (function(){
59
  const menus=[...document.querySelectorAll('.menu-item')];
@@ -73,8 +70,6 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
73
  window.__visitedWorkflows=[];
74
  window.__currentIndex=0;
75
  window.__done=false;
76
-
77
- // helper: get all visible sub-screen tabs dynamically
78
  window.__getSubScreens = function(screen){
79
  const tabs=[...screen.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
80
  const list=[];
@@ -84,7 +79,6 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
84
  }
85
  return list;
86
  };
87
-
88
  window.__captureNext=async function(){
89
  if(window.__done) return false;
90
  if(window.__currentIndex>=window.__ordered.length){window.__done=true;return false;}
@@ -106,8 +100,6 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
106
  const subs = window.__getSubScreens(screen);
107
  return {screenName:wfName, subScreens:subs};
108
  };
109
-
110
- // click sub-screen tab by text
111
  window.__clickSubScreen = async function(name){
112
  const tabs=[...document.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
113
  const t=tabs.find(x=>x.textContent.trim()===name);
@@ -122,7 +114,41 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
122
  screenshots = []
123
  index = 0
124
 
125
- # βœ… Loop through each workflow
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  while True:
127
  result = await page.evaluate("window.__captureNext()")
128
  if not result:
@@ -133,7 +159,7 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
133
  screenshot_path = os.path.join(OUTPUT_DIR, f"{screen_name}.png")
134
  print(f"πŸ“Έ Capturing main screen: {screen_name}")
135
 
136
- # Update visible + browser tab title dynamically
137
  await page.evaluate(
138
  """(name) => {
139
  const titleEl = document.querySelector('.page-title, .header-title, main h2');
@@ -143,17 +169,19 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
143
  screen_name
144
  )
145
 
146
- # βœ… Wait until sidebar (if any) has finished sliding
147
  await wait_for_sidebar_stable()
148
-
149
  await asyncio.sleep(1.2)
 
 
 
150
  await page.screenshot(path=screenshot_path, full_page=True)
 
 
151
  screenshots.append(screenshot_path)
152
 
153
- # βœ… Capture sub-screens dynamically (skip first active one)
154
  first_active_skipped = False
155
  for sub in sub_screens:
156
- # Check if this sub tab is already active
157
  is_active = await page.evaluate(
158
  """(subText) => {
159
  const tabs=[...document.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
@@ -167,19 +195,16 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
167
 
168
  if is_active and not first_active_skipped:
169
  first_active_skipped = True
170
- continue # skip first active sub-screen
171
 
172
  print(f" ↳ Capturing sub-screen: {sub}")
173
  await page.evaluate(f"window.__clickSubScreen('{sub}')")
174
-
175
- # βœ… Wait until sidebar (if any) is stable again
176
  await wait_for_sidebar_stable()
177
  await asyncio.sleep(1.0)
178
 
179
  sub_name_clean = sub.replace(" ", "_").lower()
180
  sub_path = os.path.join(OUTPUT_DIR, f"{screen_name}_{sub_name_clean}.png")
181
 
182
- # Update tab title for sub-screen
183
  await page.evaluate(
184
  """([mainName, subName]) => {
185
  const titleEl = document.querySelector('.page-title, .header-title, main h2');
@@ -190,14 +215,18 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
190
  [screen_name, sub]
191
  )
192
 
 
 
193
  await page.screenshot(path=sub_path, full_page=True)
 
 
194
  screenshots.append(sub_path)
195
 
196
  index += 1
197
 
198
  await browser.close()
199
 
200
- # βœ… Combine all screenshots into PDF
201
  if not screenshots:
202
  raise RuntimeError("No screenshots captured β€” check if .screen elements exist!")
203
 
 
17
  print(f"Opening page: {public_url}")
18
  await page.goto(public_url, wait_until="load")
19
 
20
+ # βœ… Sidebar stability detector (same as before)
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
  function getLeft() {
28
  const rect = sidebar.getBoundingClientRect();
29
  return rect.left;
30
  }
 
31
  let stableCount = 0;
32
  let lastLeft = getLeft();
 
33
  await new Promise((resolve) => {
34
  const check = setInterval(() => {
35
  const currentLeft = getLeft();
 
50
  except Exception as e:
51
  print(f"[WARN] Sidebar stability check failed: {e}")
52
 
53
+ # βœ… JS logic to map workflows + subscreens
54
  js_logic = """
55
  (function(){
56
  const menus=[...document.querySelectorAll('.menu-item')];
 
70
  window.__visitedWorkflows=[];
71
  window.__currentIndex=0;
72
  window.__done=false;
 
 
73
  window.__getSubScreens = function(screen){
74
  const tabs=[...screen.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
75
  const list=[];
 
79
  }
80
  return list;
81
  };
 
82
  window.__captureNext=async function(){
83
  if(window.__done) return false;
84
  if(window.__currentIndex>=window.__ordered.length){window.__done=true;return false;}
 
100
  const subs = window.__getSubScreens(screen);
101
  return {screenName:wfName, subScreens:subs};
102
  };
 
 
103
  window.__clickSubScreen = async function(name){
104
  const tabs=[...document.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
105
  const t=tabs.find(x=>x.textContent.trim()===name);
 
114
  screenshots = []
115
  index = 0
116
 
117
+ # βœ… Helper: Expand layout (simulate your frontend JS logic)
118
+ async def expand_layout_for_capture():
119
+ await page.evaluate("""
120
+ const body = document.body;
121
+ const html = document.documentElement;
122
+ body.dataset.originalOverflow = body.style.overflow;
123
+ html.dataset.originalWidth = html.style.width;
124
+ html.dataset.originalHeight = html.style.height;
125
+
126
+ const fullWidth = Math.max(
127
+ body.scrollWidth,
128
+ html.scrollWidth,
129
+ document.documentElement.offsetWidth
130
+ );
131
+ const fullHeight = Math.max(
132
+ body.scrollHeight,
133
+ html.scrollHeight,
134
+ document.documentElement.offsetHeight
135
+ );
136
+
137
+ body.style.overflow = 'visible';
138
+ html.style.width = fullWidth + 'px';
139
+ html.style.height = fullHeight + 'px';
140
+ """)
141
+
142
+ async def restore_layout_after_capture():
143
+ await page.evaluate("""
144
+ const body = document.body;
145
+ const html = document.documentElement;
146
+ if (body.dataset.originalOverflow) body.style.overflow = body.dataset.originalOverflow;
147
+ if (html.dataset.originalWidth) html.style.width = html.dataset.originalWidth;
148
+ if (html.dataset.originalHeight) html.style.height = html.dataset.originalHeight;
149
+ """)
150
+
151
+ # βœ… Capture all workflows and subscreens
152
  while True:
153
  result = await page.evaluate("window.__captureNext()")
154
  if not result:
 
159
  screenshot_path = os.path.join(OUTPUT_DIR, f"{screen_name}.png")
160
  print(f"πŸ“Έ Capturing main screen: {screen_name}")
161
 
162
+ # Update page title dynamically
163
  await page.evaluate(
164
  """(name) => {
165
  const titleEl = document.querySelector('.page-title, .header-title, main h2');
 
169
  screen_name
170
  )
171
 
 
172
  await wait_for_sidebar_stable()
 
173
  await asyncio.sleep(1.2)
174
+
175
+ # βœ… NEW: Expand layout for correct sidebar alignment
176
+ await expand_layout_for_capture()
177
  await page.screenshot(path=screenshot_path, full_page=True)
178
+ await restore_layout_after_capture()
179
+
180
  screenshots.append(screenshot_path)
181
 
182
+ # βœ… Capture sub-screens dynamically
183
  first_active_skipped = False
184
  for sub in sub_screens:
 
185
  is_active = await page.evaluate(
186
  """(subText) => {
187
  const tabs=[...document.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
 
195
 
196
  if is_active and not first_active_skipped:
197
  first_active_skipped = True
198
+ continue
199
 
200
  print(f" ↳ Capturing sub-screen: {sub}")
201
  await page.evaluate(f"window.__clickSubScreen('{sub}')")
 
 
202
  await wait_for_sidebar_stable()
203
  await asyncio.sleep(1.0)
204
 
205
  sub_name_clean = sub.replace(" ", "_").lower()
206
  sub_path = os.path.join(OUTPUT_DIR, f"{screen_name}_{sub_name_clean}.png")
207
 
 
208
  await page.evaluate(
209
  """([mainName, subName]) => {
210
  const titleEl = document.querySelector('.page-title, .header-title, main h2');
 
215
  [screen_name, sub]
216
  )
217
 
218
+ # βœ… NEW: Apply same layout-expansion logic
219
+ await expand_layout_for_capture()
220
  await page.screenshot(path=sub_path, full_page=True)
221
+ await restore_layout_after_capture()
222
+
223
  screenshots.append(sub_path)
224
 
225
  index += 1
226
 
227
  await browser.close()
228
 
229
+ # βœ… Combine screenshots into PDF
230
  if not screenshots:
231
  raise RuntimeError("No screenshots captured β€” check if .screen elements exist!")
232