""" e2e_r3_data.py — 只跑浏览器加数据部分 (form tddHuman628318 已建好) """ import os, time, json os.environ["PLAYWRIGHT_BROWSERS_PATH"] = "/home/ubuntu/.cache/ms-playwright" from playwright.sync_api import sync_playwright OUT_DIR = "/home/ubuntu/ccWorkDir/docs/tdd/e2e-formDesign-2026-06-16-r3" os.makedirs(f"{OUT_DIR}/imgs", exist_ok=True) FORM_ID = "836729931961476293" # tddHuman628318 MENU_URL = "model/tddHuman628318c83ff" PARENT_DIR = "836650888410965189" with sync_playwright() as p: browser = p.chromium.launch(headless=True) ctx = browser.new_context(viewport={"width": 1440, "height": 900}) # 注入 token (用 __dacli_token 走 raw JWT) ctx.add_init_script(""" (function() { const origFetch = window.fetch; window.fetch = function(input, init) { init = init || {}; init.headers = init.headers || {}; const u = (typeof input === 'string') ? input : (input.url || ''); if (!u.includes('/api/oauth/Login') && !init.headers['Authorization'] && !init.headers['authorization']) { try { const dacliTok = localStorage.getItem('__dacli_token'); if (dacliTok) init.headers['Authorization'] = dacliTok; } catch(e) {} } return origFetch.apply(this, arguments); }; })(); """) page = ctx.new_page() # 加载 dacli token 到 localStorage with open("/home/ubuntu/ccWorkDir/.dacli-token/token.json") as f: DACLI_TOK = json.load(f)["token"] page.goto("http://nginx/") page.evaluate(f"localStorage.setItem('__dacli_token', '{DACLI_TOK}')") # ========== 12. 浏览器访问用户端 + 添加数据 ========== print(">>> 12_visit_user", flush=True) # 用户端需要登录 page.goto("http://nginx/jnpf_app_tdd/login") page.wait_for_selector("#form_item_account", timeout=15000) page.fill("#form_item_account", "admin") page.fill("#form_item_password", "123456") page.click("button:has-text('登 录')") for i in range(20): time.sleep(1) if not page.evaluate("!!document.querySelector('#form_item_account')"): print(f" ✓ 用户端登录成功 i={i+1}s", flush=True); break time.sleep(2) # 重写 localStorage 用 fresh token (UI 登录后 jnpf 有自己的 localStorage, 但我们的 inject 优先用 __dacli_token) # 用户端是 SPA, fetch 注入仍生效 user_url = f"http://nginx/jnpf_app_tdd/{MENU_URL}" page.goto(user_url) time.sleep(5) page.screenshot(path=f"{OUT_DIR}/imgs/12_user_list.png") # 检查 list 是否加载 has_table = page.locator(".vxe-table").count() > 0 print(f" user list has vxe-table: {has_table}", flush=True) # 点 新增 按钮 add_btn = page.locator("button:has-text('新增')").first if add_btn.count() == 0: add_btn = page.locator("button:has-text('新 增')").first print(f" add btn count: {add_btn.count()}", flush=True) if add_btn.count() > 0: add_btn.click() time.sleep(2) page.screenshot(path=f"{OUT_DIR}/imgs/12_add_modal.png") # 找 modal 里的所有 label, 看哪个是字段 labels = page.evaluate("""() => Array.from(document.querySelectorAll('.ant-modal:not(.ant-modal-hidden) .ant-form-item-label label')).map(l => l.innerText.trim())""") print(f" modal labels: {labels}", flush=True) # 找 modal 里所有 input, 填第一个 input inputs_in_modal = page.evaluate("""() => Array.from(document.querySelectorAll('.ant-modal:not(.ant-modal-hidden) .ant-form-item input, .ant-modal:not(.ant-modal-hidden) .ant-form-item textarea')).map(i => ({type: i.type, placeholder: i.placeholder, name: i.name}))""") print(f" modal inputs: {inputs_in_modal}", flush=True) # 直接 click 第一个 input 填 try: first_input = page.locator('.ant-modal:not(.ant-modal-hidden) .ant-form-item input, .ant-modal:not(.ant-modal-hidden) .ant-form-item textarea').first first_input.click() time.sleep(0.2) first_input.press_sequentially("tddHuman628318 浏览器添加数据", delay=20) time.sleep(0.3) page.keyboard.press("Tab") time.sleep(0.3) print(f" ✓ 填字段 (第一个 input)", flush=True) except Exception as e: print(f" ⚠ 填字段: {e}", flush=True) page.screenshot(path=f"{OUT_DIR}/imgs/12_filled.png") # 列所有 modal 里的 button, 找 primary 的 btns = page.evaluate("""() => Array.from(document.querySelectorAll('.ant-modal:not(.ant-modal-hidden) button')).map(b => ({text: (b.innerText||'').trim(), primary: (b.className||'').includes('primary'), disabled: b.disabled, type: b.type}))""") print(f" modal btns: {btns}", flush=True) # 找 primary 按钮 try: primary_btn = page.locator('.ant-modal:not(.ant-modal-hidden) button.ant-btn-primary').last print(f" primary btn count: {primary_btn.count()}", flush=True) if primary_btn.count() > 0: primary_btn.click() time.sleep(3) print(f" ✓ 已提交 (primary btn)", flush=True) except Exception as e: print(f" ⚠ 提交: {e}", flush=True) page.screenshot(path=f"{OUT_DIR}/imgs/12_after_submit.png") time.sleep(2) # 验证 list 里有数据 list_data = page.evaluate(f"""async () => {{ const r = await fetch('/api/visualdev/OnlineDev/{FORM_ID}/List', {{ method: 'POST', headers: {{'Content-Type': 'application/json', 'App-Code': 'tdd'}}, body: JSON.stringify({{currentPage: 1, pageSize: 10}}) }}); return await r.json(); }}""") data_count = (list_data.get('data') or {}).get('pagination', {}).get('total', 0) print(f" ★ 列表数据行数: {data_count}", flush=True) page.screenshot(path=f"{OUT_DIR}/imgs/12_list_after.png") browser.close() print(">>> 完成", flush=True)