一、测试场景
在利用unittest框架实现自动化的过程中,发现每次访问需登录状态才能访问的页面时,都要重新登录一次才行,不能实现只登录一次,就可以访问其他需登录状态才能看到的页面,为了解决这个问题,可以参考如下操作思路,以一个完整的unittest自动化框架流程举例。
二、实现思路
首先通过selenium模拟账号密码登录,同时获取cookie,保存在.txt文件 ------- 读取txt文件中的cookie,通过driver.add_cookie添加cookie,访问登录后才能访问的接口(页面)。
(为了思路更清晰明了,部分页面使用API进行访问,没有使用定位元素定位。)
实践演示:
笔者拿自己自动化测试过程的一小部分内容举例如下,test_login1.py实现登录操作,并且将cookie保存至文件,test_depart.py读取cookie文件实现免登陆的操作,通过运行runtest1.py文件来运行前面构造的测试文件。
代码解析:
①登录文件:test_login1.py
"""
@author: June
@file:login1.py
@time:2020/10/09
"""
from selenium import webdriver
import time
import json
import unittest
class Testlogin(unittest.TestCase):
#添加驱动
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
#登录
def test_login(self):
driver = self.driver
driver.get("http://10.xxx.xxx.xxx/#/login")
driver.find_element_by_name("username").send_keys("xxx")
driver.find_element_by_name("password").send_keys("xxx")
driver.find_element_by_name("code").send_keys("xxx")
time.sleep(5)
driver.find_element_by_tag_name("button").click()
time.sleep(2)
#获取登录后的cookies。可以根据获取的cookies自定义,选取cookies中的关键信息,笔者的获取的是第三个位置cookie1[2]
cookie1 = driver.get_cookies()
print("登录后的cookies",cookie1[2])
cookie = cookie1[2]
#将cookie值保存到cookie.txt文件,以字符串形式保存
with open("cookie.txt","w+") as f:
f.write(json.dumps(cookie))
return cookie
if __name__ == "__main__":
unittest.main()
②、添加部门、删除部门测试用例文件,是关于需要登录状态才能访问的页面,test_depart.py
import time
from selenium import webdriver
import json
import unittest
class Deport(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.base_url = "http://10.xxx.xxx.xxx/#/"
#在selenium自动化中,,添加cookie前,需要先访问你要绕过登录页访问的那个url,否则会报错
cls.driver.get(cls.base_url)
time.sleep(2)
with open("./cookie.txt", "r+") as fr:
cookie = json.loads(fr.read())
cls.driver.add_cookie(cookie)
@classmethod
def tearDownClass(cls):
cls.driver.quit()
#添加部门用例
def test_add_depart(self):
self.driver.get(self.base_url+"user/company")
time.sleep(5)
self.driver.find_element_by_id("tab-section").click()
time.sleep(2)
self.driver.find_element_by_xpath('//*[@id="pane-section"]/button/span').click()
self.driver.find_element_by_class_name("input-common").send_keys("Testing_department ")
self.driver.find_element_by_xpath('//*[@id="personal"]/div/div[1]//button[2]').click()
time.sleep(3)
#删除部门用例
def test_del_depart(self):
self.driver.get( self.base_url+"user/personal/company")
self.driver.find_element_by_xpath('//*[@id="pane-section"]/table/div/span[2]').click()
time.sleep(3)
self.driver.find_element_by_xpath('//*[@id="personal"]/div/div[3]/div[3]/div/button[2]').click()
time. sleep(2)
if __name__ == "__main__":
unittest.main()
③、unittest运行脚本,runtest1.py
from HTMLTestRunner2 import HTMLTestRunner
import os, time, unittest
# 设置报告文件保存路径
report_path = os.getcwd() + '/result/'
# 获取系统当前时间
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
# 设置报告名称格式
filename = report_path + now + "_report.html"
fp = open(filename, "wb")
if __name__ == '__main__':
# 构造测试集
runner = HTMLTestRunner(stream=fp, title="增删部门自动化测试报告", description="运行环境:Windows10,Chrome浏览器", tester="June")
suite1 = unittest.defaultTestLoader.discover('testcase/', pattern='test_login1.py')
suite2 = unittest.defaultTestLoader.discover('testcase/', pattern='test_depart.py')
suite = unittest.TestSuite([suite1,suite2])
runner.run(suite)
执行runtest1.py,测试结果如下: