目录
1. 测试用例设计
2. 自动化测试
对系统的功能性、界面性、兼容性、易用性、安全性、性能进行测试。
使用 Unittes 框架对系统中的登录、添加用户信息、删除用户信息、换页功能、查询功能进行测试,并生成测试报告。
测试报告链接:
测试报告http://localhost:63343/hwhw/usermanager/resultReport/2022-50-06-18%2050%2011-resultReport.html?_ijt=slr84vv528j0gd3j58hslddqal
WebDriver常用的API:
from selenium import webdriver
import unittest
import time
from selenium.common.exceptions import NoAlertPresentException
import os
class testCase1(unittest.TestCase):
# 有setUp的部分,默认执行
def setUp(self):
self.driver = webdriver.Chrome()
self.url = "http://124.222.237.227:8085/login.html"
self.driver.get(self.url)
self.driver.maximize_window()
time.sleep(3)
# tearDown,在test_XXX方法执行完之后默认会调用
def tearDown(self):
self.driver.quit()
# 有test_XXX的部分,默认执行
# @unittest.skip("skipping")
def test_baidu1(self):
driver=self.driver
# 用id来定位,登录
driver.find_element_by_id("loginname").send_keys("admin")
driver.find_element_by_id("password").send_keys("admin")
time.sleep(4)
driver.find_element_by_id("submit").click()
time.sleep(4)
#@unittest.skip("skipping")
def test_baidu2(self):
driver=self.driver
driver.find_element_by_id("loginname").send_keys("admin")
driver.find_element_by_id("password").send_keys("admin")
time.sleep(4)
driver.find_element_by_id("submit").click()
time.sleep(4)
# 判断alert是否存在,可删除
def is_alert_exist(self):
try:
self.driver.switch_to.alert
except NoAlertPresentException as e:
return False
return True
if __name__=="__main__":
unittest.main(verbosity=0)
test case1(单元测试用例):
from selenium import webdriver
import unittest
import time
from selenium.common.exceptions import NoAlertPresentException
import os
class testCase1(unittest.TestCase):
# 有setUp的部分,默认执行
def setUp(self):
self.driver = webdriver.Chrome()
self.url = "http://124.222.237.227:8085/login.html"
self.driver.get(self.url)
self.driver.maximize_window()
time.sleep(3)
# tearDown,在test_XXX方法执行完之后默认会调用
def tearDown(self):
self.driver.quit()
# 有test_XXX的部分,默认执行
# @unittest.skip("skipping")
#测试登录
def test_baidu1(self):
driver=self.driver
driver.find_element_by_id("loginname").send_keys("admin")
driver.find_element_by_id("password").send_keys("admin")
time.sleep(4)
driver.find_element_by_id("submit").click()
time.sleep(4)
#测试添加
# @unittest.skip("skipping")
def test_baidu2(self):
driver=self.driver
driver.find_element_by_id("loginname").send_keys("admin")
driver.find_element_by_id("password").send_keys("admin")
time.sleep(4)
driver.find_element_by_id("submit").click()
time.sleep(4)
driver.find_element_by_css_selector("body > div > div:nth-child(3) > a:nth-child(1)").click()
time.sleep(3)
driver.find_element_by_id("username").send_keys("lisi")
time.sleep(3)
driver.find_element_by_id("loginname").send_keys("lisi")
time.sleep(3)
driver.find_element_by_id("password").send_keys("lisi")
time.sleep(3)
driver.find_element_by_id("password2").send_keys("lisi")
time.sleep(3)
driver.find_element_by_id("women").click()
time.sleep(3)
driver.find_element_by_id("age").send_keys("90")
time.sleep(3)
options = driver.find_element_by_id("address").find_elements_by_tag_name("option")
options[2].click()
time.sleep(3)
driver.find_element_by_id("qq").send_keys("123456789")
time.sleep(3)
driver.find_element_by_id("email").send_keys("[email protected]")
time.sleep(3)
driver.find_element_by_xpath("//*[@id='btn_sub']").click()
time.sleep(3)
driver.switch_to.alert.dismiss() # 弹框确认
time.sleep(3)
driver.switch_to.alert.dismiss() # 弹框取消
time.sleep(3)
driver.find_element_by_xpath("//*[@id='all']/li[4]/a").click()
time.sleep(3)
# 判断alert是否存在,可删除
def is_alert_exist(self):
try:
self.driver.switch_to.alert
except NoAlertPresentException as e:
return False
return True
if __name__=="__main__":
unittest.main(verbosity=0)
test case2(单元测试用例):
from selenium import webdriver
import unittest
import time
from selenium.common.exceptions import NoAlertPresentException
import os
class testCase1(unittest.TestCase):
# 有setUp的部分,默认执行
def setUp(self):
self.driver = webdriver.Chrome()
self.url = "http://124.222.237.227:8085/login.html"
self.driver.get(self.url)
self.driver.maximize_window()
time.sleep(3)
# tearDown,在test_XXX方法执行完之后默认会调用
def tearDown(self):
self.driver.quit()
# 有test_XXX的部分,默认执行
# @unittest.skip("skipping")
#测试修改
def test_baidu1(self):
driver=self.driver
driver.find_element_by_id("loginname").send_keys("admin")
driver.find_element_by_id("password").send_keys("admin")
time.sleep(4)
driver.find_element_by_id("submit").click()
time.sleep(4)
driver.find_element_by_xpath("//*[@id='all']/li[4]/a").click()
time.sleep(3)
# 修改操作
driver.find_element_by_xpath("//*[@id='info']/tr/th[10]/a[1]").click()
time.sleep(3)
driver.find_element_by_id("username").send_keys("s")
time.sleep(3)
driver.find_element_by_id("password").send_keys("Seleniumss")
time.sleep(3)
driver.find_element_by_id("password2").send_keys("Seleniumss")
time.sleep(3)
driver.find_element_by_id("men").click()
time.sleep(3)
options = driver.find_element_by_id("address").find_elements_by_tag_name("option")
options[3].click()
time.sleep(3)
driver.find_element_by_id("btn_sub").click()
time.sleep(3)
driver.switch_to.alert.dismiss() # 点击OK
time.sleep(3)
#测试删除
# @unittest.skip("skipping")
def test_baidu2(self):
driver=self.driver
driver.find_element_by_id("loginname").send_keys("admin")
driver.find_element_by_id("password").send_keys("admin")
time.sleep(4)
driver.find_element_by_id("submit").click()
time.sleep(4)
# 删除功能
# 末页
driver.find_element_by_xpath("//*[@id='all']/li[4]/a").click()
time.sleep(3)
driver.find_element_by_xpath("//*[@id='16']").click()
driver.find_element_by_id("delete").click()
time.sleep(3)
driver.switch_to.alert.accept() # 点击OK
time.sleep(3)
# 判断alert是否存在,可删除
def is_alert_exist(self):
try:
self.driver.switch_to.alert
except NoAlertPresentException as e:
return False
return True
if __name__=="__main__":
unittest.main(verbosity=0)
test case3(单元测试用例):
from selenium import webdriver
import unittest
import time
from selenium.common.exceptions import NoAlertPresentException
import os
class testCase1(unittest.TestCase):
# 有setUp的部分,默认执行
def setUp(self):
self.driver = webdriver.Chrome()
self.url = "http://124.222.237.227:8085/login.html"
self.driver.get(self.url)
self.driver.maximize_window()
time.sleep(3)
# tearDown,在test_XXX方法执行完之后默认会调用
def tearDown(self):
self.driver.quit()
# 有test_XXX的部分,默认执行
# @unittest.skip("skipping")
#测试查找
def test_baidu1(self):
driver=self.driver
driver.find_element_by_id("loginname").send_keys("admin")
driver.find_element_by_id("password").send_keys("admin")
time.sleep(4)
driver.find_element_by_id("submit").click()
time.sleep(4)
driver.find_element_by_id("ipt_name").send_keys("张三")
time.sleep(3)
driver.find_element_by_id("ipt_address").send_keys("成都")
time.sleep(3)
driver.find_element_by_id("ipt_email").send_keys("[email protected]")
time.sleep(3)
driver.find_element_by_id("submit1").click()
time.sleep(3)
# 判断alert是否存在,可删除
def is_alert_exist(self):
try:
self.driver.switch_to.alert
except NoAlertPresentException as e:
return False
return True
if __name__=="__main__":
unittest.main(verbosity=0)
test suite(单元测试用例的集合) && test runner(执行单元测试):
import unittest
from usermanager import test004
from usermanager import test002
from usermanager import test003
def createSuite():
suite = unittest.TestSuite()
#suite.addTest(test001.testCase1("test_baidu1"))
#suite.addTest(test001.testCase1("test_baidu2"))
#suite.addTest(test003.testCase2("test_baidu1"))
#suite.addTest(test003.testCase2("test_baidu2"))
# makeSuite
#suite.addTest(unittest.makeSuite(test001.testCase1))
#suite.addTest(unittest.makeSuite(test003.testCase2))
#TestLoader
#suite1 = unittest.TestLoader.loadTestsFromTestCase(test001.testCase1)
#suite2 = unittest.TestLoader.loadTestsFromTestCase(test003.testCase2)
#suite.addTest([suite1,suite2])
# return suite
#最简便,把指定文件夹下,多有特定个事命名的脚本中类的所有以test开头的py文件
discover = unittest.defaultTestLoader.discover('../usermanager',pattern='test00*.py',top_level_dir=None)
return discover
if __name__=="__main__":
suite = createSuite()
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
test report(生成测试报告 ):
import unittest
import time
from usermanager import test002
from usermanager import test003
from usermanager import test004
import os,sys
import HTMLTestRunner
def createSuite():
discover = unittest.defaultTestLoader.discover('../usermanager',pattern='test00*.py',top_level_dir=None)
print(discover)
return discover
if __name__=="__main__":
# 索取当前文件所在的文件路径
curpath = sys.path[0]
print(curpath)
if not os.path.exists(curpath + "/resultRepost"):
os.mkdir(curpath + "/resultReport")
now = time.strftime("%Y-%M-%d-%H %M %S",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)