蓝色天空网 · mbsky.com
实战:本站注册、登录和搜索测试
用完整本地服务验证正反两条业务路径。
固定本地测试页的实际浏览器结果,不是在线Python解释器。下载代码后可在独立环境修改输入并重新运行。
本章Python入口 · 完整实验实现 · 本站测试服务 · 本站页面交互

当前输入实际检查
| 实际检查 | 结果 |
|---|---|
| fixture-initial-no-severe-errors | 通过 |
| register-mismatch | 通过 |
| register-positive | 通过 |
| duplicate-user | 通过 |
| one-real-fixture-user | 通过 |
| login-wrong-password | 通过 |
| current-user-visible | 通过 |
| actual-session-cookie | 通过 |
| logout-clears-current-user | 通过 |
| logout-cookie-removed | 通过 |
| server-session-removed | 通过 |
| practical-Enter-search | 通过 |
| empty-search-visible | 通过 |
| screenshot-saved-actual-browser | 通过 |
| PNG-magic-actual-browser | 通过 |
| PNG-extent-actual-browser | 通过 |
| all-created-sessions-quit | 通过 |
本章完整入口
from selenium_lab import *
@case('selenium-practical')
def practical(l,d,b,s):
def fill(values):
for id,value in values.items():el=d.find_element(By.ID,id);el.clear();el.send_keys(value)
def status():return d.find_element(By.ID,'auth-result').get_attribute('data-status')
d.get(b+'/register');fill({'username':'mbsky_learner','password':'local-learning-only','email':'learner@example.test','confirm-password':'different'});click(d,'register-button');wait(d).until(lambda _:status()=='error');l.eq('register-mismatch',d.find_element(By.ID,'auth-result').text,'两次密码不一致');fill({'confirm-password':'local-learning-only'});click(d,'register-button');wait(d).until(lambda _:status()=='success');l.eq('register-positive',d.find_element(By.ID,'auth-result').text,'注册成功');click(d,'register-button');wait(d).until(lambda _:status()=='error');l.eq('duplicate-user',d.find_element(By.ID,'auth-result').text,'用户已存在');l.eq('one-real-fixture-user',list(s['users']),['mbsky_learner']);d.get(b+'/login');fill({'username':'mbsky_learner','password':'wrong'});click(d,'login-button');wait(d).until(lambda _:status()=='error');l.eq('login-wrong-password',d.find_element(By.ID,'auth-result').text,'用户名或密码错误');fill({'password':'local-learning-only'});click(d,'login-button');wait(d).until(lambda _:status()=='success');l.eq('current-user-visible',d.find_element(By.ID,'current-user').text,'mbsky_learner');l.check('actual-session-cookie',d.get_cookie('mbsky_test_session')['httpOnly']);click(d,'logout');wait(d).until(lambda _:status()=='loggedout');l.eq('logout-clears-current-user',d.find_element(By.ID,'current-user').text,'');l.eq('logout-cookie-removed',d.get_cookie('mbsky_test_session'),None);l.eq('server-session-removed',s['cookies'],{});d.get(b+'/search');l.eq('practical-Enter-search',search(d,'CSS'),['CSS布局','CSS选择器']);box=d.find_element(By.ID,'query');box.clear();box.send_keys('not-present');click(d,'search-submit');wait(d).until(lambda x:x.find_element(By.ID,'results').get_attribute('data-status')=='done');l.eq('empty-search-visible',d.find_element(By.ID,'result-status').text,'没有匹配结果');l.pending_case('production-registration','Only isolated in-memory fictional test users; production membership, email/SMS, billing, security/performance acceptance not covered')
if __name__ == "__main__":
run('selenium-practical')