蓝色天空网 · mbsky.com

文件上传下载:路径、响应与字节

选中文件和服务器收到文件需要分别验证。

固定本地测试页的实际浏览器结果,不是在线Python解释器。下载代码后可在独立环境修改输入并重新运行。

本章Python入口 · 完整实验实现 · 本站测试服务 · 本站页面交互

文件上传下载:路径、响应与字节实际运行截图
本站独立实验页面的实际浏览器截图。

当前输入实际检查

实际检查结果
fixture-initial-no-severe-errors通过
DOM-files-name通过
DOM-files-size通过
server-received-sha通过
server-received-length通过
server-independent-record通过
missing-file-not-selected通过
fresh-download-not-existing通过
download-actual-bytes通过
ZIP-CRC通过
ZIP-exact-members通过
ZIP-brand通过
screenshot-saved-actual-browser通过
PNG-magic-actual-browser通过
PNG-extent-actual-browser通过
all-created-sessions-quit通过

本章完整入口

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')