from selenium_lab import *
@case('selenium-file-operator')
def files(l,d,b,s):
p=l.out/'mbsky-upload.txt';body='蓝色天空网 · mbsky.com\nSelenium真实字节上传\n'.encode();p.write_bytes(body);el=d.find_element(By.ID,'upload');el.send_keys(str(p));l.eq('DOM-files-name',d.execute_script('return arguments[0].files[0].name',el),p.name);l.eq('DOM-files-size',d.execute_script('return arguments[0].files[0].size',el),len(body));click(d,'upload-send');wait(d).until(lambda x:x.find_element(By.ID,'upload-result').get_attribute('data-status')=='done');actual=json.loads(d.find_element(By.ID,'upload-result').text);l.eq('server-received-sha',actual['sha256'],hashlib.sha256(body).hexdigest());l.eq('server-received-length',actual['bytes'],len(body));l.eq('server-independent-record',s['uploads'][-1],actual);l.err('missing-file-not-selected',InvalidArgumentException,lambda:el.send_keys(str(l.out/'no-such-file.txt')));target=l.out/'downloads/mbsky-example.zip';l.check('fresh-download-not-existing',not target.exists());click(d,'download');wait(d,8).until(lambda _:target.is_file()and not list(target.parent.glob('*.crdownload'))and target.read_bytes()==download_bytes());l.eq('download-actual-bytes',hashlib.sha256(target.read_bytes()).hexdigest(),hashlib.sha256(download_bytes()).hexdigest())
with zipfile.ZipFile(target)as z:l.eq('ZIP-CRC',z.testzip(),None);l.eq('ZIP-exact-members',z.namelist(),['README.txt','example.html']);l.check('ZIP-brand','mbsky.com'in z.read('example.html').decode())
l.pending_case('Grid-managed-download-and-native-dialog','Only local Edge native input and actual ZIP download tested; Actual same-host Grid managed downloads separately tested in Grid chapter; cross-machine LocalFileDetector/AutoIT/native picker not executed')
if __name__ == "__main__":
run('selenium-file-operator')
本例需在 Windows/Python3.12/Selenium4.52.0/Edge及驱动156.0.4314.58;GridServer4.52/Temurin21.0.13;独立无头会话 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows/Python3.12/Selenium4.52.0/Edge及驱动156.0.4314.58;GridServer4.52/Temurin21.0.13;独立无头会话
入口文件:projects/selenium-lab/selenium-file-operator.py
预期结果与验证条件观察提示
- 本章固定输入断言通过,错误/等待/业务结果和退出分别验证
- 截图对应本站当前测试页;未执行平台或能力不因本轮通过自动升级
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联15章) · 含当前章节或实例 · mbskycom202611a531.zip