5、對下拉列表的定位采用select標(biāo)簽
1 from selenium.webdriver.support.select import Select
2 # 選擇部門
3 dp =self.driver.find_element_by_id('dept')
4 Select(dp).select_by_index(user['dept'])
5 # 選擇角色
6 Select(self.driver.find_element_by_id('role')).select_by_index(user['role'])
6、模塊化代碼
需要對自動化重復(fù)編寫的腳本進(jìn)行重構(gòu)(refactor),將重復(fù)的腳本抽取出來,放到指定的代碼文件中,作為共用的功能模塊。使用模塊化代碼注意需倒入該代碼。
1 #模塊化代碼后引用,需導(dǎo)入代碼模塊
2 from ranzhi_lib import RanzhiLib
3 self.lib = RanzhiLib(self.driver)
4 # 點擊后臺管理
5 self.lib.click_admin_app()
6 sleep(2)
7 # 點擊添加用戶
8 self.lib.click_add_user()
9 # 添加用戶
10 self.lib.add_user(user_to_add)
11 sleep(1)
12 # 退出
13 self.lib.logn_out()
14 sleep(2)
1 class RanzhiLib():
2 # 構(gòu)造方法
3 def __init__(self, driver):
4 self.driver = driver
7、自定義函數(shù)運行的先后順序:完整的單元測試很少只執(zhí)行一個測試用例,開發(fā)人員通常都需要編寫多個測試用例才能對某一軟件功能進(jìn)行比較完整的測試,這些相關(guān)的測試用例稱為一個測試用例集,在PyUnit中是用TestSuite類來表示,采用unittest.TestSuite()。
PyUnit使用TestRunner類作為測試用例的基本執(zhí)行環(huán)境,來驅(qū)動整個單元測試過程。Python開發(fā)人員在進(jìn)行單元測試時一般不直接使用TestRunner類,而是使用其子類TextTestRunner來完成測試。
詳情請查看:
http://www.ibm.com/developerworks/cn/linux/l-pyunit/
1 # 構(gòu)造測試集
2 suite = unittest.TestSuite()
3 suite.addTest(RanzhiTest("test_login"))
4 suite.addTest(RanzhiTest("test_ranzhi"))
5
6 # 執(zhí)行測試
7 runner = unittest.TextTestRunner()
8 runner.run(suite)
以下代碼為登錄“然之系統(tǒng)”,進(jìn)入添加用戶,循環(huán)添加用戶并檢測添加成功,再退出的過程。以下程序分別為主程序,模塊化程序,執(zhí)行程序,CSV文件
1 import csv
2 import unittest
3 from time import sleep
4
5 from selenium import webdriver
6 # 模塊化代碼后引用需導(dǎo)入代碼模塊
7 from ranzhi_lib import RanzhiLib
8
9
10 class Ranzhi(unittest.TestCase):
11 def setUp(self):
12 self.driver = webdriver.Firefox()
13 self.lib = RanzhiLib(self.driver)
14
15 # 主函數(shù)
16 def test_ranzhi(self):
17 # 讀取CSV文件到user_list字典類型變量中
18 user_list = csv.reader(open("list_to_user.csv", "r"))
19 # 遍歷整個user_list
20 for user in user_list:
21 sleep(2)
22 self.lib.logn_in('admin', 'admin')
23 sleep(2)
24 # 斷言
25 self.assertEqual("
http://localhost:8080/ranzhi/www/sys/index.html",
26 self.driver.current_url,
27 '登錄跳轉(zhuǎn)失敗')
28 # 讀取一行csv,并分別賦值到user_to_add 中
29 user_to_add = {'account': user[0],
30 'realname': user[1],
31 'gender': user[2],
32 'dept': user[3],
33 'role': user[4],
34 'password': user[5],
35 'email': user[0] + user[6]}
36 # 點擊后臺管理
37 self.lib.click_admin_app()
38 # 進(jìn)入嵌套
39 self.lib.driver.switch_to.frame('iframe-superadmin')
40 sleep(2)
41 # 點擊添加用戶
42 self.lib.click_add_user()
43 # 添加用戶
44 self.lib.add_user(user_to_add)
45 # 退出嵌套
46 self.driver.switch_to.default_content()
47 sleep(1)
48 # 退出
49 self.lib.logn_out()
50 sleep(2)
51 # 用新賬號登錄
52 self.lib.logn_in(user_to_add['account'], user_to_add['password'])
53 sleep(2)
54 self.lib.logn_out()
55 sleep(2)
56
57 def tearDown(self):
58 self.driver.quit()
1 from time import sleep
2
3 from selenium.webdriver.support.select import Select
4
5
6 class RanzhiLib():
7 # 構(gòu)造方法
8 def __init__(self, driver):
9 self.driver = driver
10
11 # 模塊化添加用戶
12 def add_user(self, user):
13 driver = self.driver
14 # 添加用戶名
15 ac = driver.find_element_by_id('account')
16 ac.send_keys(user['account'])
17 # 真實姓名
18 rn = driver.find_element_by_id('realname')
19 rn.clear()
20 rn.send_keys(user['realname'])
21 # 選擇性別
22 if user['gender'] == 'm':
23 driver.find_element_by_id('gender2').click()
24 elif user['gender'] == 'f':
25 driver.find_element_by_id('gender1').click()
26 # 選擇部門
27 dp = driver.find_element_by_id('dept')
28 Select(dp).select_by_index(user['dept'])
29 # 選擇角色
30 role = driver.find_element_by_id('role')
31 Select(role).select_by_index(user['role'])
32 # 輸入密碼
33 pwd1 = driver.find_element_by_id('password1')
34 pwd1.clear()
35 pwd1.send_keys(user['password'])
36
37 pwd2 = driver.find_element_by_id('password2')
38 pwd2.send_keys(user['password'])
39 # 輸入郵箱
40 em = driver.find_element_by_id('email')
41 em.send_keys(user['email'])
42 # 點擊保存
43 driver.find_element_by_id('submit').click()
44 sleep(2)
45
46 # 登錄賬號
47 def logn_in(self, name, password):
48 driver = self.driver
49 driver.get('http://localhost:8080/ranzhi/www')
50 sleep(2)
51
52 driver.find_element_by_id('account').clear()
53 driver.find_element_by_id('account').send_keys(name)
54 driver.find_element_by_id('password').clear()
55 driver.find_element_by_id('password').send_keys(password)
56 driver.find_element_by_id('submit').click()
57 sleep(2)
58
59 # 退出賬號
60 def logn_out(self):
61 self.driver.find_element_by_id('start').click()
62 sleep(4)
63 self.driver.find_element_by_link_text(u'退出').click()
64 sleep(3)
65
66 # 點擊后臺管理
67 def click_admin_app(self):
68 self.driver.find_element_by_xpath('//*[@id="s-menu-superadmin"]/button').click()
69 sleep(1)
70
71 def click_add_user(self):
72 self.driver.find_element_by_xpath('//*[@id="shortcutBox"]/div/div[1]/div/a/h3').click()
73 sleep(3)
1 import unittest
2
3 from ranzhi import Ranzhi
4
5
6 class RanzhiTestRunner():
7 def run_tests(self):
8 suite = unittest.TestSuite()
9 suite.addTest(Ranzhi('test_ranzhi'))
10 runner = unittest.TextTestRunner()
11 runner.run(suite)
12
13
14 if __name__ == "__main__":
15 ranzhi_test_runner = RanzhiTestRunner()
16 ranzhi_test_runner.run_tests()
1 melody109,melody101,m,1,3,123456,@qq.com
2 melody106,melody101,f,2,5,123456,@qq.com
3 melody107,melody101,m,3,2,123456,@qq.com