您的位置:软件测试 > 开源软件测试 > 开源功能测试工具 > Selenium
Selenium+Python?实现基本自动化测试
作者:网络转载 发布时间:[ 2016/7/22 14:11:42 ] 推荐标签:自动化测试 功能测试

  5、对下拉列表的定位采用select标签
  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、模块化代码
  需要对自动化重复编写的脚本进行重构(refactor),将重复的脚本抽取出来,放到指定的代码文件中,作为共用的功能模块。使用模块化代码注意需倒入该代码。
1 #模块化代码后引用,需导入代码模块
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     # 构造方法
  3     def __init__(self, driver):
  4         self.driver = driver
  7、自定义函数运行的先后顺序:完整的单元测试很少只执行一个测试用例,开发人员通常都需要编写多个测试用例才能对某一软件功能进行比较完整的测试,这些相关的测试用例称为一个测试用例集,在PyUnit中是用TestSuite类来表示,采用unittest.TestSuite()。
  PyUnit使用TestRunner类作为测试用例的基本执行环境,来驱动整个单元测试过程。Python开发人员在进行单元测试时一般不直接使用TestRunner类,而是使用其子类TextTestRunner来完成测试。
  详情请查看:http://www.ibm.com/developerworks/cn/linux/l-pyunit/
  1 # 构造测试集
  2 suite = unittest.TestSuite()
  3 suite.addTest(RanzhiTest("test_login"))
  4 suite.addTest(RanzhiTest("test_ranzhi"))
  5
  6 # 执行测试
  7 runner = unittest.TextTestRunner()
  8 runner.run(suite)
  以下代码为登录“然之系统”,进入添加用户,循环添加用户并检测添加成功,再退出的过程。以下程序分别为主程序,模块化程序,执行程序,CSV文件
1 import csv
2 import unittest
3 from time import sleep
4
5 from selenium import webdriver
6 # 模块化代码后引用需导入代码模块
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     # 主函数
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                              '登录跳转失败')
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             # 进入嵌套
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     # 构造方法
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

上一页12下一页
软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd