python + selenium + unittest 实现网站登录注册自动化测试
2022-07-20 14:22:19 234
Python Selenium 自动化脚本 软件测试
推荐一个在线工具网站:[在线工具大全](https://www.zztdd.cn) 登录封装: ```python from selenium import webdriver import time class Auto(object): def __init__(self, *args): login_url = 'https://snail.zhuozhuo.io/?n=fastCat.fcFrontSnail.NewFront.NewFront.PCLoginPage' register_url = "https://getsuwo.com/?n=fastCat.fcFrontSnail.NewFront.NewFront.PCRegisterPage" InRegisterAuto_url = "https://getsuwo.com/?n=fastCat.fcFrontSnail.NewFront.NewFront.PCInternationalRegisterPage" driver = webdriver.Chrome() self.driver = driver self.login_url = login_url self.register_url = register_url self.InRegisterAuto_url = InRegisterAuto_url self.args = args #登录页面 def loginAuto(self): self.driver.implicitly_wait(5) self.driver.get(self.login_url) self.error('self.driver.find_element_by_name("Username").send_keys(self.args[0])') self.error('self.driver.find_element_by_name("Password").send_keys(self.args[1])') self.driver.find_element_by_css_selector("[type='submit']").click() try: time.sleep(0.5) result = self.driver.find_element_by_class_name("ErrorMsg").text return result except: return finally: self.driver.quit() ``` 登录用例执行: ```python import unittest import warnings from Tool import Auto class ExecuteCase(unittest.TestCase): def setUp(self): warnings.simplefilter('ignore', ResourceWarning) # 处理unittest警告信息 # 测试账号(test_1 -- test_8),登录页面账号未作格式校验 def test_1(self): '''所有输入框为空''' expectResult = "帐号不能为空" login = Auto() actualResult = login.loginAuto() self.assertEqual(expectResult, actualResult) def test_2(self): '''输入12位手机号,密码为空,值为:184883658555''' expectResult = "手机号码格式不正确" login = Auto("184883658555") actualResult = login.loginAuto() self.assertEqual(expectResult, actualResult) def test_3(self): '''输入10位手机号,密码为空,值为:1848365858''' expectResult = "手机号码格式不正确" login = Auto("1848365858") actualResult = login.loginAuto() self.assertEqual(expectResult, actualResult) ``` 等等…… [详细源码,请访问github](https://github.com/ZhongTian5/selenium) 推荐一个免费的在线小工具网站:https://www.zztdd.cn/