selenium之使用unittest测试框架
1 # 测试角色权限管理页面功能
2 from selenium import webdriver
3 from login_page import LoginPage
4 import random, time, unittest
5
6
7 class TestRole(unittest.TestCase):
8 def setUp(self):
9 self.login_obj = LoginPage()
10 self.driver = webdriver.Chrome()
11 driver = self.driver
12 login_obj = self.login_obj
13 time.sleep(2)
14 driver = login_obj.login(driver) # 登录
15 driver.find_element_by_id('4').click() # 进入监控管理
16 time.sleep(1)
17 # 进入角色管理
18 driver.find_element_by_xpath('//a[@url="manager/role/role_list.do"]').click()
19 time.sleep(2)
20
21 # 定义变量
22 random_num_to_str = str(random.randint(1000, 9999)) # 随机数,保证不重复
23 self.role_name = 'role_' + random_num_to_str # 角色名,限制10个字符
24 self.spec_context = 'spec_' + random_num_to_str # 备注信息
25 self.modify_spec_context = self.spec_context + '_modify' # 修改后的备注信息
26
27 def tearDown(self):
28 '''关闭浏览器'''
29 self.login_obj.quit()
30
31 def test_role(self):
32 driver = self.driver
33
34 def create_role():
35 '''新增角色'''
36 driver.find_element_by_id('addRow').click()
37 driver.find_element_by_id('role_name').send_keys(self.role_name)
38 driver.find_element_by_id('spec').send_keys(self.spec_context)
39 time.sleep(3)
40 driver.find_element_by_id('userSave').click()
41 time.sleep(3)
42
43 def query_role_by_name():
44 '''查询角色'''
45 driver.find_element_by_id('searchKey').send_keys(self.role_name)
46 driver.find_element_by_id('search').click()
47 time.sleep(3)
48
49 def edit_role():
50 '''编辑角色'''
51 driver.find_element_by_xpath("//button[@title='编辑']").click() # 查找title='编辑'的button
52 driver.find_element_by_id('spec').clear()
53 driver.find_element_by_id('spec').send_keys(self.modify_spec_context)
54 time.sleep(2)
55 driver.find_element_by_id('userSave').click()
56 time.sleep(3)
57
58 def bind_menu():
59 '''菜单绑定'''
60 driver.find_element_by_xpath("//a[@title='菜单绑定']").click()
61 driver.find_element_by_xpath("//div[@class='checkBoxUp']/div[1]/i").click() # 全选菜单
62 time.sleep(3)
63 driver.find_element_by_id('userSave').click()
64 time.sleep(3)
65
66 def del_role():
67 '''删除角色'''
68 driver.find_element_by_xpath("//a[@title='删除']").click()
69 time.sleep(1)
70 driver.find_element_by_class_name("aui_state_highlight").click()
71 time.sleep(3)
72
73 create_role()
74 query_role_by_name()
75 edit_role()
76 bind_menu()
77 del_role()
78
79
80 if __name__ == '__main__':
81 '''自动执行以'test'开头的测试方法'''
82 unittest.main()
到这里完成了一条完整的测试用例,但如果有多条测试用例需要一起执行,后续说明。
转载 http://www.cnblogs.com/gongxr/p/7291812.html