蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实战:本站注册、登录和搜索测试:独立本地浏览器实验

PYTHONWindows/Python3.12/Selenium4.52.0/Edge及驱动156.0.4314.58;GridServer4.52/Temurin21.0.13;独立无头会话selenium-practical.py
selenium-practical.py12 行
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')
本例需在 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-practical.py

预期结果与验证条件观察提示
  • 本章固定输入断言通过,错误/等待/业务结果和退出分别验证
  • 截图对应本站当前测试页;未执行平台或能力不因本轮通过自动升级

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:Selenium

课程关联工程包(登记关联15章) · 含当前章节或实例 · mbskycom202611a531.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习