Files
cms-SiteForge/scripts/studio_nested_browser_v823.py
T

34 lines
3.0 KiB
Python
Raw Normal View History

"""Offline Chromium test of nested component canvas events in srcdoc sandbox."""
from pathlib import Path
from playwright.sync_api import sync_playwright
ROOT=Path(__file__).resolve().parents[1]
BRIDGE=(ROOT/'app/static/studio-canvas-bridge.js').read_text(encoding='utf-8')
HTML='''<!doctype html><html><head><style>body{font-family:Arial}.sf-section{position:relative;background:#f4f7f9;padding:20px;margin:10px}.sf-layout-grid{display:grid;grid-template-columns:var(--sf-layout-cols);gap:10px}.sf-layout-column{background:#dce8ed;min-height:170px}</style></head><body>
<section class="sf-section" data-block="parent_1000"><div class="sf-container"><div class="sf-layout-grid" data-layout="parent_1000" style="--sf-layout-cols:1fr 1fr"><div class="sf-layout-column" data-col="1"><section class="sf-section" data-block="child_0001"><div class="sf-container"><h2>Child one</h2><p>Alpha</p></div></section><section class="sf-section" data-block="child_0002"><div class="sf-container"><h2>Child two</h2><p>Beta</p></div></section></div><div class="sf-layout-column" data-col="2"><section class="sf-section" data-block="child_0003"><div class="sf-container"><h2>Child three</h2></div></section></div></div></div></section>
</body></html>'''
def run():
with sync_playwright() as p:
browser=p.chromium.launch(headless=True,executable_path='/usr/bin/chromium',args=['--no-sandbox'])
page=browser.new_page(viewport={'width':1200,'height':900})
page.set_content('<html><body><iframe id="preview" sandbox="allow-scripts" style="width:980px;height:820px"></iframe></body></html>')
page.evaluate('''content=>{window.events=[];window.addEventListener('message',e=>{if(e.source===document.querySelector('iframe').contentWindow)events.push(e.data)});document.querySelector('iframe').srcdoc=content;}''',HTML.replace('</body>','<script>'+BRIDGE+'</script></body>'))
frame=page.frame_locator('#preview')
node=frame.locator('.sf-layout-column .sf-section[data-block="child_0001"]')
node.wait_for(timeout=8000)
node.click(position={'x':80,'y':70})
assert page.evaluate('events.some(e=>e.action==="child-select"&&e.blockId==="parent_1000"&&e.childId==="child_0001")')
node.hover()
node.locator('.sf-nested-toolbar button[aria-label="复制子组件"]').click()
assert page.evaluate('events.some(e=>e.action==="child-duplicate"&&e.childId==="child_0001")')
node.locator('h2').dblclick()
frame.locator('[contenteditable="true"]').fill('Canvas changed headline')
frame.locator('[contenteditable="true"]').press('Enter')
page.wait_for_timeout(100)
assert page.evaluate('events.some(e=>e.action==="child-inline-text"&&e.childId==="child_0001"&&e.value==="Canvas changed headline")')
assert frame.locator('.sf-canvas-resize').count()==1
assert not page.evaluate('events.some(e=>e.action==="execute")')
browser.close()
print('PASS: nested select/duplicate/inline text + sandbox message transport')
if __name__=='__main__':run()