selenium之使用unittest测试框架

时间:2022-01-03 19:31:09
 # 测试角色权限管理页面功能
from selenium import webdriver
from login_page import LoginPage
import random, time, unittest class TestRole(unittest.TestCase):
def setUp(self):
self.login_obj = LoginPage()
self.driver = webdriver.Chrome()
driver = self.driver
login_obj = self.login_obj
time.sleep(2)
driver = login_obj.login(driver) # 登录
driver.find_element_by_id('').click() # 进入监控管理
time.sleep(1)
# 进入角色管理
driver.find_element_by_xpath('//a[@url="manager/role/role_list.do"]').click()
time.sleep(2) # 定义变量
random_num_to_str = str(random.randint(1000, 9999)) # 随机数,保证不重复
self.role_name = 'role_' + random_num_to_str # 角色名,限制10个字符
self.spec_context = 'spec_' + random_num_to_str # 备注信息
self.modify_spec_context = self.spec_context + '_modify' # 修改后的备注信息 def tearDown(self):
'''关闭浏览器'''
self.login_obj.quit() def test_role(self):
driver = self.driver def create_role():
'''新增角色'''
driver.find_element_by_id('addRow').click()
driver.find_element_by_id('role_name').send_keys(self.role_name)
driver.find_element_by_id('spec').send_keys(self.spec_context)
time.sleep(3)
driver.find_element_by_id('userSave').click()
time.sleep(3) def query_role_by_name():
'''查询角色'''
driver.find_element_by_id('searchKey').send_keys(self.role_name)
driver.find_element_by_id('search').click()
time.sleep(3) def edit_role():
'''编辑角色'''
driver.find_element_by_xpath("//button[@title='编辑']").click() # 查找title='编辑'的button
driver.find_element_by_id('spec').clear()
driver.find_element_by_id('spec').send_keys(self.modify_spec_context)
time.sleep(2)
driver.find_element_by_id('userSave').click()
time.sleep(3) def bind_menu():
'''菜单绑定'''
driver.find_element_by_xpath("//a[@title='菜单绑定']").click()
driver.find_element_by_xpath("//div[@class='checkBoxUp']/div[1]/i").click() # 全选菜单
time.sleep(3)
driver.find_element_by_id('userSave').click()
time.sleep(3) def del_role():
'''删除角色'''
driver.find_element_by_xpath("//a[@title='删除']").click()
time.sleep(1)
driver.find_element_by_class_name("aui_state_highlight").click()
time.sleep(3) create_role()
query_role_by_name()
edit_role()
bind_menu()
del_role() if __name__ == '__main__':
'''自动执行以'test'开头的测试方法'''
unittest.main()

到这里完成了一条完整的测试用例,但如果有多条测试用例需要一起执行,后续说明。