dina1 commited on
Commit
c0cbe94
Β·
verified Β·
1 Parent(s): d4fc30c

Update playwright_model.py

Browse files
Files changed (1) hide show
  1. playwright_model.py +50 -35
playwright_model.py CHANGED
@@ -16,7 +16,7 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
16
  print(f"Opening page: {public_url}")
17
  await page.goto(public_url, wait_until="load")
18
 
19
- # βœ… Inject workflow traversal logic
20
  js_logic = """
21
  (function(){
22
  const menus=[...document.querySelectorAll('.menu-item')];
@@ -36,6 +36,18 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
36
  window.__visitedWorkflows=[];
37
  window.__currentIndex=0;
38
  window.__done=false;
 
 
 
 
 
 
 
 
 
 
 
 
39
  window.__captureNext=async function(){
40
  if(window.__done) return false;
41
  if(window.__currentIndex>=window.__ordered.length){window.__done=true;return false;}
@@ -43,7 +55,10 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
43
  const {menu,screen}=pair;
44
  if(!screen){window.__done=true;return false;}
45
  const wfName=screen.id || screen.getAttribute('data-name') || ('screen_'+window.__currentIndex);
46
- if(window.__visitedWorkflows.includes(wfName)){window.__done=true;return false;}
 
 
 
47
  window.__visitedWorkflows.push(wfName);
48
  document.querySelectorAll('.screen').forEach(s=>s.classList.remove('active'));
49
  document.querySelectorAll('.menu-item').forEach(m=>m.classList.remove('active'));
@@ -51,7 +66,16 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
51
  if(menu) menu.classList.add('active');
52
  screen.scrollIntoView({behavior:'smooth',block:'center'});
53
  window.__currentIndex++;
54
- return {screenName:wfName};
 
 
 
 
 
 
 
 
 
55
  };
56
  })();
57
  """
@@ -68,10 +92,11 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
68
  break
69
 
70
  screen_name = result.get("screenName", f"screen_{index}")
 
71
  screenshot_path = os.path.join(OUTPUT_DIR, f"{screen_name}.png")
72
  print(f"πŸ“Έ Capturing main screen: {screen_name}")
73
 
74
- # βœ… Update visible title and browser tab
75
  await page.evaluate(
76
  """(name) => {
77
  const titleEl = document.querySelector('.page-title, .header-title, main h2');
@@ -85,41 +110,32 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
85
  await page.screenshot(path=screenshot_path, full_page=True)
86
  screenshots.append(screenshot_path)
87
 
88
- # βœ… Capture sub-screens if any
89
- sub_screens = await page.evaluate(
90
- """() => {
91
- const subs = [...document.querySelectorAll('.tab-button, .sub-screen-tab, [data-subscreen]')];
92
- return subs.map(s => s.textContent.trim()).filter(Boolean);
93
- }"""
94
- )
95
-
96
- # Detect active sub-screen (default one)
97
- default_sub = await page.evaluate(
98
- """() => {
99
- const active = document.querySelector('.tab-button.active, .sub-screen-tab.active, [data-subscreen].active');
100
- return active ? active.textContent.trim() : null;
101
- }"""
102
- )
103
-
104
  for sub in sub_screens:
105
- # 🧠 Skip default sub-screen (already captured above)
106
- if sub == default_sub:
107
- print(f"⏩ Skipping duplicate default sub-screen: {sub}")
108
- continue
109
-
110
- print(f" ↳ Capturing sub-screen: {sub}")
111
- await page.evaluate(
112
- """(name) => {
113
- const btns = [...document.querySelectorAll('.tab-button, .sub-screen-tab, [data-subscreen]')];
114
- const btn = btns.find(b => b.textContent.trim() === name);
115
- if (btn) btn.click();
116
  }""",
117
  sub
118
  )
119
 
120
- await asyncio.sleep(1.2)
 
 
 
 
 
 
 
 
121
 
122
- # Update title for sub-screen
123
  await page.evaluate(
124
  """([mainName, subName]) => {
125
  const titleEl = document.querySelector('.page-title, .header-title, main h2');
@@ -130,7 +146,6 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
130
  [screen_name, sub]
131
  )
132
 
133
- sub_path = os.path.join(OUTPUT_DIR, f"{screen_name}_{sub}.png")
134
  await page.screenshot(path=sub_path, full_page=True)
135
  screenshots.append(sub_path)
136
 
@@ -138,7 +153,7 @@ async def capture_workflows(public_url: str, pdf_filename: str = "workflow_scree
138
 
139
  await browser.close()
140
 
141
- # βœ… Combine into PDF
142
  if not screenshots:
143
  raise RuntimeError("No screenshots captured β€” check if .screen elements exist!")
144
 
 
16
  print(f"Opening page: {public_url}")
17
  await page.goto(public_url, wait_until="load")
18
 
19
+ # βœ… Inject workflow traversal + sub-screen logic
20
  js_logic = """
21
  (function(){
22
  const menus=[...document.querySelectorAll('.menu-item')];
 
36
  window.__visitedWorkflows=[];
37
  window.__currentIndex=0;
38
  window.__done=false;
39
+
40
+ // helper: get all visible sub-screen tabs dynamically
41
+ window.__getSubScreens = function(screen){
42
+ const tabs=[...screen.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
43
+ const list=[];
44
+ for(const t of tabs){
45
+ const sub=t.textContent?.trim();
46
+ if(sub && !list.includes(sub)) list.push(sub);
47
+ }
48
+ return list;
49
+ };
50
+
51
  window.__captureNext=async function(){
52
  if(window.__done) return false;
53
  if(window.__currentIndex>=window.__ordered.length){window.__done=true;return false;}
 
55
  const {menu,screen}=pair;
56
  if(!screen){window.__done=true;return false;}
57
  const wfName=screen.id || screen.getAttribute('data-name') || ('screen_'+window.__currentIndex);
58
+ if(window.__visitedWorkflows.includes(wfName)){
59
+ window.__currentIndex++;
60
+ return window.__captureNext();
61
+ }
62
  window.__visitedWorkflows.push(wfName);
63
  document.querySelectorAll('.screen').forEach(s=>s.classList.remove('active'));
64
  document.querySelectorAll('.menu-item').forEach(m=>m.classList.remove('active'));
 
66
  if(menu) menu.classList.add('active');
67
  screen.scrollIntoView({behavior:'smooth',block:'center'});
68
  window.__currentIndex++;
69
+ const subs = window.__getSubScreens(screen);
70
+ return {screenName:wfName, subScreens:subs};
71
+ };
72
+
73
+ // click sub-screen tab by text
74
+ window.__clickSubScreen = async function(name){
75
+ const tabs=[...document.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
76
+ const t=tabs.find(x=>x.textContent.trim()===name);
77
+ if(t){t.click(); return true;}
78
+ return false;
79
  };
80
  })();
81
  """
 
92
  break
93
 
94
  screen_name = result.get("screenName", f"screen_{index}")
95
+ sub_screens = result.get("subScreens", [])
96
  screenshot_path = os.path.join(OUTPUT_DIR, f"{screen_name}.png")
97
  print(f"πŸ“Έ Capturing main screen: {screen_name}")
98
 
99
+ # Update visible + browser tab title dynamically
100
  await page.evaluate(
101
  """(name) => {
102
  const titleEl = document.querySelector('.page-title, .header-title, main h2');
 
110
  await page.screenshot(path=screenshot_path, full_page=True)
111
  screenshots.append(screenshot_path)
112
 
113
+ # βœ… Capture sub-screens dynamically (skip first active one)
114
+ first_active_skipped = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  for sub in sub_screens:
116
+ # Check if this sub tab is already active
117
+ is_active = await page.evaluate(
118
+ """(subText) => {
119
+ const tabs=[...document.querySelectorAll('.tab, .nav-link, [role="tab"], [data-tab], .sub-tab, .tab-item')];
120
+ const t=tabs.find(x=>x.textContent.trim()===subText);
121
+ if(!t) return false;
122
+ const cls=t.getAttribute('class')||'';
123
+ return cls.includes('active');
 
 
 
124
  }""",
125
  sub
126
  )
127
 
128
+ if is_active and not first_active_skipped:
129
+ first_active_skipped = True
130
+ continue # skip first active sub-screen
131
+
132
+ print(f" ↳ Capturing sub-screen: {sub}")
133
+ await page.evaluate(f"window.__clickSubScreen('{sub}')")
134
+ await asyncio.sleep(1.0)
135
+ sub_name_clean = sub.replace(" ", "_").lower()
136
+ sub_path = os.path.join(OUTPUT_DIR, f"{screen_name}_{sub_name_clean}.png")
137
 
138
+ # Update tab title for sub-screen
139
  await page.evaluate(
140
  """([mainName, subName]) => {
141
  const titleEl = document.querySelector('.page-title, .header-title, main h2');
 
146
  [screen_name, sub]
147
  )
148
 
 
149
  await page.screenshot(path=sub_path, full_page=True)
150
  screenshots.append(sub_path)
151
 
 
153
 
154
  await browser.close()
155
 
156
+ # βœ… Combine all screenshots into PDF
157
  if not screenshots:
158
  raise RuntimeError("No screenshots captured β€” check if .screen elements exist!")
159