一、学生信息管理系统测试用例设计
二、使用selenium工具对项目进行自动化测试
2.1 测试登录功能及登录后的查询功能
- 测试输入正确的用户名和密码是否能够正常登录,输入错误的用户名和密码是否登录失败,当登录失败时,对异常进行处理,对错误进行截图,并将弹窗中的错误信息打印在控制台。
- 测试列表页面的查询功能,分别输入姓名、籍贯、邮箱看是否能查出正确的学生信息。
from selenium import webdriver
import unittest
import time
import os
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
class Testlogin(unittest.TestCase):
# 测试固件
def setUp(self):
self.driver = webdriver.Chrome()
self.url = "http://101.42.153.174:8082/login.html"
self.driver.get(self.url)
self.driver.maximize_window()
time.sleep(3)
def tearDown(self):
self.driver.quit()
# 测试用例
# 忽略测试用例的执行
# @unittest.skip("skipping")
def test_login(self):
driver = self.driver
# 登录操作
driver.find_element_by_id("loginname").send_keys("admin")
# 使用tab键将焦点定位到用户名
driver.find_element_by_id("loginname").send_keys(Keys.TAB)
driver.find_element_by_id("password").send_keys("1234578")
# 用enter键直接登录
driver.find_element_by_id("submit").send_keys(Keys.ENTER)
time.sleep(6)
alert = driver.switch_to.alert
time.sleep(3)
# 获取alert的文本值
alert_content = alert.text
alert.accept()
try:
self.assertEqual(alert_content, "恭喜,登录成功!", msg="登录失败")
except:
if not os.path.exists("./image"):
os.makedirs("./image")
now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
driver.get_screenshot_as_file('./image/' + now + '-' + 'error.png')
print(alert_content)
def test_select(self):
driver = self.driver
# 登录操作
driver.find_element_by_id("loginname").send_keys("admin")
# 使用tab键将焦点定位到用户名
driver.find_element_by_id("loginname").send_keys(Keys.TAB)
driver.find_element_by_id("password").send_keys("12345678")
# 用enter键直接登录
driver.find_element_by_id("submit").send_keys(Keys.ENTER)
time.sleep(6)
driver.switch_to.alert.accept()
# 查询操作
driver.find_element_by_id("ipt_name").send_keys("赵亮")
# 点击查询键
time.sleep(1)
driver.find_element_by_id("submit1").send_keys(Keys.ENTER)
time.sleep(3)
driver.find_element_by_id("ipt_name").clear()
driver.find_element_by_id("ipt_address").send_keys("成都")
time.sleep(1)
# 点击查询键
driver.find_element_by_id("submit1").send_keys(Keys.ENTER)
time.sleep(3)
driver.find_element_by_id("ipt_address").clear()
driver.find_element_by_id("ipt_email").send_keys("123123@qq.com")
time.sleep(1)
driver.find_element_by_id("submit1").send_keys(Keys.ENTER)
time.sleep(3)
if __name__ == "__main__":
unittest.main(verbosity=2)
2.2 测试列表页的增加用户和删除用户功能
- 测试添加用户的功能,输入正确的信息是否能够注册成功,当一条信息注册成功后,再次执行该测试用例,看这条信息是否添加失败。
- 测试删除单条用户信息,测试删除刚刚添加的用户功能是否正常,通过对比点击删除后弹窗中的内容来判断是否删除成功。
from selenium import webdriver
import unittest
import time
import os
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
class Testlist(unittest.TestCase):
# 测试固件
def setUp(self):
self.driver = webdriver.Chrome()
self.url = "http://101.42.153.174:8082/login.html"
self.driver.get(self.url)
self.driver.maximize_window()
time.sleep(3)
def tearDown(self):
self.driver.quit()
# 测试用例
# 忽略测试用例的执行
# @unittest.skip("skipping")
def test_add(self):
driver = self.driver
# 登录操作
driver.find_element_by_id("loginname").send_keys("admin")
# 使用tab键将焦点定位到用户名
driver.find_element_by_id("loginname").send_keys(Keys.TAB)
driver.find_element_by_id("password").send_keys("12345678")
# 用enter键直接登录
driver.find_element_by_id("submit").send_keys(Keys.ENTER)
time.sleep(6)
driver.switch_to.alert.accept()
time.sleep(3)
driver.find_element_by_css_selector("body > div > div:nth-child(3) > a:nth-child(1)").send_keys(Keys.ENTER)
time.sleep(3)
driver.find_element_by_id("username").send_keys("周董")
time.sleep(1)
driver.find_element_by_id("loginname").send_keys("东东")
time.sleep(1)
driver.find_element_by_id("password").send_keys("123")
time.sleep(1)
driver.find_element_by_id("password2").send_keys("123")
time.sleep(1)
driver.find_element_by_id("women").click()
time.sleep(1)
driver.find_element_by_id("age").send_keys("18")
time.sleep(1)
driver.find_element_by_id("address").find_element_by_tag_name("option")
time.sleep(1)
driver.find_element_by_id("qq").send_keys("18")
time.sleep(1)
driver.find_element_by_id("email").send_keys("1234@qq.com")
time.sleep(5)
driver.find_element_by_id("btn_sub").click()
time.sleep(6)
alert = driver.switch_to.alert
time.sleep(3)
# 获取alert的文本值
alert_content = alert.text
alert.accept()
try:
self.assertEqual(alert_content, "恭喜,添加成功!", msg="添加失败")
except:
if not os.path.exists("./image"):
os.makedirs("./image")
now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
driver.get_screenshot_as_file('./image/' + now + '-' + 'error.png')
print(alert_content)
def test_del(self):
driver = self.driver
# 登录操作
driver.find_element_by_id("loginname").send_keys("admin")
# 使用tab键将焦点定位到用户名
driver.find_element_by_id("loginname").send_keys(Keys.TAB)
driver.find_element_by_id("password").send_keys("12345678")
# 用enter键直接登录
driver.find_element_by_id("submit").send_keys(Keys.ENTER)
time.sleep(6)
driver.switch_to.alert.accept()
time.sleep(3)
driver.find_element_by_xpath('//*[@id="all"]/li[4]/a').click()
time.sleep(3)
driver.find_element_by_xpath('//*[@id="info"]/tr[2]/th[10]/a[2]').click()
time.sleep(6)
driver.switch_to.alert.accept();
time.sleep(3)
alert = driver.switch_to.alert
time.sleep(3)
# 获取alert的文本值
alert_content = alert.text
alert.accept()
try:
self.assertEqual(alert_content, "恭喜,删除成功!", msg="删除失败")
except:
if not os.path.exists("./image"):
os.makedirs("./image")
now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
driver.get_screenshot_as_file('./image/' + now + '-' + 'error.png')
print(alert_content)
if __name__ == "__main__":
unittest.main(verbosity=2)
2.3 生成HTML报告
我们将该文件夹下的所有测试脚本中的测试用例全部放入测试套件,一次性测试所有的功能,并生成HTML报告,测试报告中包含每个测试脚本中的测试用例的个数、测试通过的个数、失败的个数、错误的个数以及每个测试用例的错误细节。
import HTMLTestRunner
import os
import sys
import time
import unittest
def createsuite():
discovers = unittest.defaultTestLoader.discover("../usermanagerTest", pattern="um_*.py", top_level_dir=None)
print(discovers)
return discovers
if __name__=="__main__":
# 文件夹要创建在哪里
curpath = sys.path[0]
print(sys.path)
print(sys.path[0])
# 1,创建文件夹,创建的这个文件夹干什么
if not os.path.exists(curpath+'/resultreport'):
os.makedirs(curpath+'/resultreport')
# 2,文件夹的命名,不能让名称重复
# 时间 时分秒 ——》名称绝对不会重复
now = time.strftime("%Y-%m-%d-%H %M %S", time.localtime(time.time()))
print(now)
print(time.time())
print(time.localtime(time.time()))
# 文件名
filename = curpath + '/resultreport/' + now + 'resultreport.html'
with open(filename, 'wb') as fp:
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"测试报告",
description=u"用例执行情况", verbosity=2)
suite = createsuite()
runner.run(suite)
文章来源:https://www.toymoban.com/news/detail-780005.html
HTML测试报告:
文章来源地址https://www.toymoban.com/news/detail-780005.html
到了这里,关于学生信息管理系统之测试篇的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!