蓝色天空网 · mbsky.com
Grid4:会话分发和真实远程执行
Standalone、Hub/Node与完全分布式模式分开。
固定本地测试页的实际浏览器结果,不是在线Python解释器。下载代码后可在独立环境修改输入并重新运行。
本章Python入口 · 完整实验实现 · 本站测试服务 · 本站页面交互

当前输入实际检查
| 实际检查 | 结果 |
|---|---|
| fixture-initial-no-severe-errors | 通过 |
| actual-single-node | 通过 |
| node-two-slots | 通过 |
| real-concurrent-slots | 通过 |
| managed-download-bytes-0 | 通过 |
| managed-download-deleted-0 | 通过 |
| managed-download-bytes-1 | 通过 |
| managed-download-deleted-1 | 通过 |
| two-distinct-sessions | 通过 |
| remote-search-result | 通过 |
| remote-browser-version | 通过 |
| remote-search-result | 通过 |
| remote-browser-version | 通过 |
| screenshot-saved-actual-browser | 通过 |
| PNG-magic-actual-browser | 通过 |
| PNG-extent-actual-browser | 通过 |
| all-created-sessions-quit | 通过 |
本章完整入口
from selenium_lab import *
@case('selenium-grid')
def grid_case(l,d,b,s):
from grid_runtime import grid,local_json
with grid(l.out)as(remote,status):
l.eq('actual-single-node',len(status['value']['nodes']),1);l.eq('node-two-slots',len(status['value']['nodes'][0]['slots']),2);barrier=threading.Barrier(3)
def worker(index):
with browser(l,name='grid-'+str(index),remote=remote)as rd:
rd.get(b+'/search');session=rd.session_id;barrier.wait(timeout=30);barrier.wait(timeout=30);rows=search(rd,'CSS');rd.get(b);click(rd,'download');wait(rd,8).until(lambda _:'mbsky-example.zip'in rd.get_downloadable_files());local=l.out/'remote-downloads'/str(index);rd.download_file('mbsky-example.zip',str(local));l.eq('managed-download-bytes-'+str(index),hashlib.sha256((local/'mbsky-example.zip').read_bytes()).hexdigest(),hashlib.sha256(download_bytes()).hexdigest());rd.delete_downloadable_files();l.eq('managed-download-deleted-'+str(index),rd.get_downloadable_files(),[]);return {'session':session,'rows':rows,'version':rd.capabilities['browserVersion']}
with ThreadPoolExecutor(max_workers=2)as pool:
futures=[pool.submit(worker,i)for i in range(2)]
try:
barrier.wait(timeout=30);active=local_json(remote+'/status');occupied=[slot['session']for node in active['value']['nodes']for slot in node['slots']if slot['session']is not None];l.eq('real-concurrent-slots',len(occupied),2);l.metadata['grid_active_slots']=occupied;barrier.wait(timeout=30);results=[f.result(timeout=30)for f in futures]
except BaseException:barrier.abort();raise
l.eq('two-distinct-sessions',len({v['session']for v in results}),2)
for v in results:l.eq('remote-search-result',v['rows'],['CSS布局','CSS选择器']);l.eq('remote-browser-version',v['version'],'156.0.4314.58')
l.metadata['grid_results']=results;wait(d,4).until(lambda _:all(slot['session']is None for node in local_json(remote+'/status')['value']['nodes']for slot in node['slots']))
l.pending_case('distributed-other-platforms','Actual local Standalone/2concurrent remote Edge sessions; separate Hub+Node machines, Docker/Kubernetes, other OS/browser and node-failure recovery not executed')
if __name__ == "__main__":
run('selenium-grid')