仅仅是针对CSDN账号密码登录这一小块的自动化测试;测试用例考虑的不是非常周全,仅图一乐
因为用例比较少,所以没有用自己封装的seleniumAPI,这样看着比较方便一些
HTMLTestRunner需要下载,不能用pip安装。参见博客
CSDN登录-账号密码登录的功能
#!/usr/bin/python3
# _*_coding=utf-8 _*_
# @author junwei
# @date 2021/2/28 21:21
# description 测试CSDN的账号密码登录模块
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
# 登录模块
class Login(object):
# 创建浏览器对象
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(2)
self.driver.maximize_window()
def login_csdn(self, userName, pwd):
# 进入使用账号密码登录的页面
url = "https://passport.csdn.net/login?code=public"
self.driver.get(url)
self.driver.implicitly_wait(2)
self.driver.find_element_by_css_selector(
"#app > div > div > div.main > div.main-login > div.main-select > ul > li:nth-child(2) > a").click()
# 输入用户名
self.driver.find_element_by_id("all").send_keys(userName)
# 输入密码
self.driver.implicitly_wait(2)
self.driver.find_element_by_id("password-number").send_keys(pwd)
if self.is_button_enable() is True:
self.driver.find_element_by_css_selector(
"#app > div > div > div.main > div.main-login > div.main-process-login > div > div:nth-child(6) > div > button").click()
else:
return "warning:login button disabled"
# 判断登录按钮是否可有效
def is_button_enable(self):
is_enable = self.driver.find_element_by_css_selector(
"#app > div > div > div.main > div.main-login > div.main-process-login > div > div:nth-child(6) > div > button").is_enabled()
return is_enable
# 判断是否登录成功
def is_login(self):
ActionChains(self.driver).move_to_element(self.driver.find_element_by_css_selector(
"#csdn-toolbar > div > div > div.toolbar-container-right > div > div.toolbar-btn.toolbar-btn-login.csdn-toolbar-fl.toolbar-subMenu-box > a > img")).perform()
self.driver.implicitly_wait(2)
nick_name = self.driver.find_element_by_class_name("csdn-profile-nickName")
if nick_name is not None:
return True
else:
return False
# 获取提示信息
def get_msg(self):
# self.driver.implicitly_wait(2)
time.sleep(2) # 需要强制等待
return self.driver.find_element_by_id("js_err_dom").text # 1.用户名或密码错误 2.长时间未修改密码,请通过"忘记密码"修改密码后登录 3.用户唯一标识不能为空
def __del__(self):
time.sleep(2)
self.driver.close()
if __name__ == '__main__':
login = Login()
login.login_csdn("", "s")
print(login.is_button_enable())
print(login.get_msg())
# print(login.is_login())
6条测试用例&运行unittest
#!/usr/bin/python3
# _*_coding=utf-8 _*_
# @author junwei
# @date 2021/2/28 21:22
# description 测试用例
import unittest
from unittest_selenium.CSDNFunction.login import Login
from unittest_selenium.HTMLTestRunner import HTMLTestRunner
class MyTestCase(unittest.TestCase):
def setUp(self) -> None:
print("当前用例执行开始")
def tearDown(self) -> None:
print("当前用例执行结束")
# 测试用例001:账号密码正确,登录成功
def test_001(self):
login = Login()
login.login_csdn("correctName", "correctPwd")
is_login = login.is_login()
self.assertEqual(True, is_login)
# 测试用例002:不输入用户名和密码
def test_002(self):
login = Login()
login.login_csdn("", "")
is_button_enable = login.is_button_enable()
self.assertEqual(False, is_button_enable)
# 测试用例003:输入用户名,密码为空,登录按钮无法点击
def test_003(self):
login = Login()
login.login_csdn("testUser", "")
is_button_enable = login.is_button_enable()
self.assertEqual(False, is_button_enable)
# 测试用例004:输入密码,用户名为空,点击登录
def test_004(self):
login = Login()
login.login_csdn("", "testPwd")
msg = login.get_msg()
self.assertEqual("用户唯一标识不能为空", msg)
# 测试用例005:输入正确的用户名和错误的密码,点击登录
def test_005(self):
login = Login()
login.login_csdn("correctName", "testPwd")
msg = login.get_msg()
self.assertEqual("用户名或密码错误", msg)
# 测试用例006:假设已知账号A长时间未登录,输入账号名A及密码
def test_006(self):
login = Login()
login.login_csdn("1", "testPwd")
msg = login.get_msg()
self.assertEqual('长时间未修改密码,请通过"忘记密码"修改密码后登录', msg)
if __name__ == '__main__':
# unittest.main()
# # 测试用例列表
# case_list = ['test_001', 'test_002', 'test_003', 'test_004', 'test_005', 'test_006']
# # 创建测试套件
# suit = unittest.TestSuite()
# # 将测试用例添加到测试套件中
# for case in case_list:
# suit.addTest(MyTestCase(case))
# 添加全部测试用例
suit = unittest.makeSuite(MyTestCase)
# 执行测试,verbosity=2指定为每个测试用例生成报告,run中传入要执行的测试套件
unittest.TextTestRunner(verbosity=2).run(suit)
# # 使用HTMLTestRunner生成HTML格式的测试报告
# with open('report.html', 'wb') as f:
# HTMLTestRunner(stream=f, title="测试报告标题", description="测试报告描述信息", verbosity=2).run(suit)
第一条用例失败是因为没有给正确的账号密码