"""Offline Playwright Chromium lab for jQuery EasyUI Ch 19: Autoplay Tabs."""
import json
from playwright.sync_api import sync_playwright
HTML_DOC = r"""<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>EasyUI Autoplay Tabs Offline Lab</title>
<style>
body { font-family: sans-serif; margin: 12px; }
.easyui-tabs { border: 1px solid #95b8e7; display: flex; flex-direction: column; box-sizing: border-box; }
.tabs-header { display: flex; background: #e0ecff; border-bottom: 1px solid #95b8e7; padding: 4px 4px 0; gap: 3px; }
.tabs-tab { padding: 4px 8px; border: 1px solid #95b8e7; border-bottom: none; background: #f4f8ff; font-size: 12px; cursor: pointer; }
.tabs-tab.tabs-selected { background: #fff; font-weight: 600; }
.tabs-panels { flex: 1; }
.tabs-panel { display: none; padding: 20px; box-sizing: border-box; }
.tabs-panel.active { display: block; }
</style>
</head>
<body>
<div id="tt" class="easyui-tabs" style="width:330px;height:270px;">
<div title="Shirt1" style="padding:20px;"><img alt="Shirt1" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80'%3E%3Crect width='80' height='80' fill='%23dbeafe'/%3E%3Ctext x='14' y='45' font-size='14'%3EShirt1%3C/text%3E%3C/svg%3E"></div>
<div title="Shirt2" style="padding:20px;"><img alt="Shirt2" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80'%3E%3Crect width='80' height='80' fill='%23fce7f3'/%3E%3Ctext x='14' y='45' font-size='14'%3EShirt2%3C/text%3E%3C/svg%3E"></div>
<div title="Shirt3" style="padding:20px;"><img alt="Shirt3" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80'%3E%3Crect width='80' height='80' fill='%23dcfce7'/%3E%3Ctext x='14' y='45' font-size='14'%3EShirt3%3C/text%3E%3C/svg%3E"></div>
<div title="Shirt4" style="padding:20px;"><img alt="Shirt4" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80'%3E%3Crect width='80' height='80' fill='%23fef3c7'/%3E%3Ctext x='14' y='45' font-size='14'%3EShirt4%3C/text%3E%3C/svg%3E"></div>
<div title="Shirt5" style="padding:20px;"><img alt="Shirt5" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80'%3E%3Crect width='80' height='80' fill='%23ede9fe'/%3E%3Ctext x='14' y='45' font-size='14'%3EShirt5%3C/text%3E%3C/svg%3E"></div>
</div>
<script>
(function(){
const container = document.getElementById('tt');
const rawPanels = Array.from(container.children);
container.innerHTML = '<div class="tabs-header"></div><div class="tabs-panels"></div>';
const header = container.querySelector('.tabs-header');
const panelsEl = container.querySelector('.tabs-panels');
const tabs = [];
let selected = null;
function selectTab(title) {
selected = title;
tabs.forEach(item => {
const active = item.options.title === title;
item.tabEl.classList.toggle('tabs-selected', active);
item.panelEl.classList.toggle('active', active);
});
}
rawPanels.forEach(div => {
const title = div.getAttribute('title');
const tabEl = document.createElement('div');
tabEl.className = 'tabs-tab';
tabEl.textContent = title;
tabEl.addEventListener('click', () => selectTab(title));
const panelEl = document.createElement('div');
panelEl.className = 'tabs-panel';
panelEl.dataset.title = title;
panelEl.innerHTML = div.innerHTML;
header.appendChild(tabEl);
panelsEl.appendChild(panelEl);
tabs.push({
options: { title },
tabEl,
panelEl,
panel(cmd) { return cmd === 'options' ? this.options : null; }
});
});
selectTab(tabs[0].options.title);
window.tabsWidget = {
tabs() { return tabs; },
select(title) { selectTab(title); },
getSelected() { return selected; },
visibleTitles() {
return tabs.filter(t => getComputedStyle(t.panelEl).display !== 'none').map(t => t.options.title);
}
};
window.runAutoplay = function(intervalMs, maxTicks) {
return new Promise(resolve => {
let index = 0;
const allTabs = window.tabsWidget.tabs();
const history = [];
const timerId = setInterval(() => {
const title = allTabs[index].panel('options').title;
window.tabsWidget.select(title);
history.push(window.tabsWidget.getSelected());
index++;
if (index >= allTabs.length) {
index = 0;
}
if (history.length >= maxTicks) {
clearInterval(timerId);
window._lastTimerStopped = true;
resolve({ history, finalIndex: index, finalSelected: window.tabsWidget.getSelected() });
}
}, intervalMs);
});
};
})();
</script>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page(viewport={"width": 600, "height": 420})
page.set_content(HTML_DOC)
# Case 1: Initial 5 Shirt tabs and container dimensions
res1 = page.evaluate("""() => {
const box = document.getElementById('tt').getBoundingClientRect();
const titles = window.tabsWidget.tabs().map(t => t.panel('options').title);
const firstPad = getComputedStyle(document.querySelector('.tabs-panel.active')).padding;
const imgsLoaded = Array.from(document.querySelectorAll('.tabs-panel img')).every(img => img.complete && img.naturalWidth === 80);
return {
width: Math.round(box.width),
height: Math.round(box.height),
count: titles.length,
titles,
initialSelected: window.tabsWidget.getSelected(),
firstPanelPadding: firstPad,
imgsLoaded,
};
}""")
exp1 = {
"width": 330,
"height": 270,
"count": 5,
"titles": ["Shirt1", "Shirt2", "Shirt3", "Shirt4", "Shirt5"],
"initialSelected": "Shirt1",
"firstPanelPadding": "20px",
"imgsLoaded": True,
}
cases.append(dict(name="initial_five_shirt_tabs", expected=exp1, actual=res1, passed=(res1 == exp1)))
# Case 2: setInterval autoplay cycles through all 5 tabs and wraps to Shirt1
res2 = page.evaluate("() => window.runAutoplay(15, 7)")
exp2 = {
"history": ["Shirt1", "Shirt2", "Shirt3", "Shirt4", "Shirt5", "Shirt1", "Shirt2"],
"finalIndex": 2,
"finalSelected": "Shirt2",
}
cases.append(dict(name="autoplay_interval_cycle_and_wrap", expected=exp2, actual=res2, passed=(res2 == exp2)))
# Case 3: Single visible panel invariant & stable state after clearInterval
page.wait_for_timeout(60)
res3 = page.evaluate("""() => ({
visibleTitles: window.tabsWidget.visibleTitles(),
selectedAfterWait: window.tabsWidget.getSelected(),
timerStopped: Boolean(window._lastTimerStopped),
})""")
exp3 = {
"visibleTitles": ["Shirt2"],
"selectedAfterWait": "Shirt2",
"timerStopped": True,
}
cases.append(dict(name="active_panel_visibility_and_pause_control", expected=exp3, actual=res3, passed=(res3 == exp3)))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/jeasyui-layout-tabs3-lab/explore-layout-tabs3.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-layout-tabs3.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1005.zip