使用unittest单元测试框架,对项目中的登录功能进行了自动化测试。
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:8080/myblog/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("username").send_keys("郝文雪")
# 使用tab键将焦点定位到用户名
driver.find_element_by_id("username").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)
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')
def test_zhuye(self):
driver = self.driver
url = self.url
driver.get(url)
driver.find_element_by_link_text("主页").click()
a = driver.find_element_by_xpath('//*[@id="page"]/a[1]').text
time.sleep(5)
print(a)
try:
self.assertEqual(a, "上一页", 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')
if __name__ == "__main__":
unittest.main(verbosity=2)
assertEqual
这个断言来判断弹窗中的内容是否为“恭喜,登录成功!”,如果是,则说明登录成功,如果不是,则对异常进行处理,并进行异常截图。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 Testlogin2(unittest.TestCase):
# 测试固件
def setUp(self):
self.driver = webdriver.Chrome()
self.url = "http://101.42.153.174:8080/myblog/reg.html"
self.driver.get(self.url)
self.driver.maximize_window()
time.sleep(3)
def tearDown(self):
self.driver.quit()
# 测试用例
# 忽略测试用例的执行
# @unittest.skip("skipping")
def test_reg(self):
driver = self.driver
# 登录操作
driver.find_element_by_id("username").send_keys("赵亮")
# 使用tab键将焦点定位到用户名
driver.find_element_by_id("username").send_keys(Keys.TAB)
driver.find_element_by_id("nickname").send_keys("亮亮")
driver.find_element_by_id("password").send_keys("aaaaaaaa")
driver.find_element_by_id("password2").send_keys("aaaaaaaa")
# 用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')
if __name__ == "__main__":
unittest.main(verbosity=2)
我们可以一个文件夹下的所有测试脚本中的测试用例全部放入测试套件,一次性进行测试,并且生成HTML的测试报告,测试报告中包含每个测试脚本中的测试用例的个数、测试通过的个数、失败的个数、错误的个数以及每个测试用例的错误细节。
import HTMLTestRunner
import os
import sys
import time
import unittest
def createsuite():
discovers = unittest.defaultTestLoader.discover("../myblogTest", pattern="login*.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)