python&selenium自动化测试实战项目——全面完整详细(02)

Posted 程序员 二黑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python&selenium自动化测试实战项目——全面完整详细(02)相关的知识,希望对你有一定的参考价值。

python&selenium自动化测试实战项目——全面、完整、详细

接上篇

会员档案查询用例

 1 '''
  2 Code description:会员档案查询 testcase
  3 Create time:
  4 Developer:
  5 '''
  6 
  7 import random
  8 import time
  9 from selenium.webdriver.common.action_chains import ActionChains
 10 from retail.test_case.models.myunit import MyunitTest
 11 from retail.test_case.page_obj.memeberquery_page import MemberQuery
 12 
 13 class MemberQuery_TC(MyunitTest):
 14 
 15     """会员档案查询模块测试用例"""
 16 
 17     #@unittest.skip('dont run the test case')
 18     def test_member_check_ui(self):
 19         """会员档案查询页面显示正确"""
 20         menu = MemberQuery(self.driver) # 实例化会员查询页面
 21         self.login.loginFunc()
 22         menu.memberQueryMenu()  # 查找会员档案查询菜单
 23         elements = menu.findElements(*menu.uiElements)
 24         ele_list = []
 25         for eles in elements:
 26             if eles.text !='':
 27                 ele_list.append(eles.text)
 28         self.assertEqual('查询条件', ele_list[0])
 29         self.assertEqual('会员信息明细', ele_list[1])
 30         self.assertEqual('积分变化明细', ele_list[2])
 31 
 32 
 33     def test_member_type(self):
 34         """会员类型下拉列表项正确"""
 35         menu = MemberQuery(self.driver)  # 实例化会员查询页面
 36         self.login.loginFunc()# 登录
 37         menu.memberQueryMenu()  # 查找会员档案查询菜单
 38         menu.selectMemberType()
 39         list_type = []
 40         for member_type in menu.memberTypeNum:  # 循环遍历会员类型下拉列表
 41             text, memeber_type_level = menu.memberTypeLevelOption(*member_type)
 42             list_type.append(text)
 43         self.assertEqual('个人会员', list_type[0])
 44         self.assertEqual('企业会员', list_type[1])
 45         self.assertEqual('其它', list_type[2])
 46 
 47     def test_member_level(self):
 48         """会员级别下拉列表项正确"""
 49         menu = MemberQuery(self.driver)  # 实例化会员查询页面
 50         self.login.loginFunc()  # 登录
 51         menu.memberQueryMenu() # 查找会员档案查询菜单
 52         menu.selectMemberLevel()
 53         list_level = []
 54         for member_level in menu.memberLevelNum: # 循环遍历会员级别下拉列表
 55             text, memeber_type_level = menu.memberTypeLevelOption(*member_level)
 56             list_level.append(text)
 57         self.assertEqual('标准会员', list_level[0])
 58         self.assertEqual('黄金会员', list_level[1])
 59         self.assertEqual('铂金会员', list_level[2])
 60         self.assertEqual('钻石会员', list_level[3])
 61 
 62     # ............................................................................................#
 63     # 对页面的条件进行组合后单击查询按钮。这是一个大数据量的操作,因此不对返回数据做校验,只看本次组合的条件在页面是否可正常使用。
 64     # 如果查询失败,系统会有弹出框提示失败原因,这个应该很好理解的。
 65     # 我们抓取这个框是否在一定的时间内出现,如果出现则判定本次查询失败,记录用例结果。
 66     # ............................................................................................#
 67 
 68     def test_member_query_failed(self):
 69         """默认条件查询成功"""
 70         menu = MemberQuery(self.driver)  # 实例化会员档案查询页面
 71         self.login.loginFunc()  # 登录
 72         menu.memberQueryMenu()  # 找到会员查询页面
 73         menu.cQueryResetBtn(*menu.queryResetBtn[0])  # 点击[查询]
 74         flag = menu.isElementExist(menu.qFailerr)  # 断言错误提示框
 75         self.assertFalse(flag, msg='查询失败')  # flag为false时,断言成功, 无提示框,说明默认查询成功
 76 
 77     def test_alone_query_1(self):
 78         """按会员编号,会员姓名,手机号码单一条件查询"""
 79         menu = MemberQuery(self.driver)  # 实例化会员档案查询页面
 80         self.login.loginFunc()  # 登录
 81         menu.memberQueryMenu()  # 找到会员档案查询页面
 82         for num_name_phone in menu.memberNumNamePhone:
 83             menu.reset()  # 重置
 84             for value in menu.valuesList:
 85                 menu.iQueryCondition(num_name_phone,value)
 86                 menu.cQueryResetBtn(*menu.queryResetBtn[0]) # 点击[查询]
 87                 time.sleep(3)
 88                 flag = menu.isElementExist(menu.qFailerr)
 89                 if flag:
 90                     self.assertTrue(flag, '提示框不存在,查询成功')
 91                     menu.accept(*menu.confirmBtn)
 92                 else:
 93                     self.assertFalse(flag, '提示框存在,查询失败')
 94 
 95     def test_alone_query_2(self):
 96         """按会员类型单一查询"""
 97         menu = MemberQuery(self.driver) # 实例化会员档案查询页面
 98         self.login.loginFunc() # 登录
 99         menu.memberQueryMenu() # 找到会员档案查询页面
100         for me_type in menu.memberTypeNum:
101             menu.selectMemberType() # 点击[会员类型]
102             text, member_type_level = menu.memberTypeLevelOption(*me_type) # 遍历每一个下拉选项
103             ActionChains(self.driver).move_to_element(member_type_level).perform() # 鼠标移动到下拉选项上
104             member_type_level.click() # 选中下拉选项
105             menu.cQueryResetBtn(*menu.queryResetBtn[0])  # 点击[查询]
106             time.sleep(3)
107             flag = menu.isElementExist(menu.qFailerr) # 判断查询成功
108             self.assertFalse(flag, '提示框存在,查询失败')
109 
110     def test_alone_query_3(self):
111         """按会员级别单一查询"""
112         menu = MemberQuery(self.driver)  # 实例化会员档案查询页面
113         self.login.loginFunc()  # 登录
114         menu.memberQueryMenu()  # 找到会员档案查询页面
115         for me_level in menu.memberLevelNum:
116             menu.selectMemberLevel() # 点击[会员级别]
117             text, member_level = menu.memberTypeLevelOption(*me_level)  # 遍历每一个下拉选项
118             ActionChains(self.driver).move_to_element(member_level).perform()  # 鼠标移动到下拉选项上
119             member_level.click()  # 选中下拉选项
120             menu.cQueryResetBtn(*menu.queryResetBtn[0])  # 点击[查询]
121             time.sleep(3)
122             flag = menu.isElementExist(menu.qFailerr)  # 判断查询成功
123             self.assertFalse(flag, '提示框存在,查询成功')
124 
125     def test_reset(self):
126         """重置功能校验"""
127         menu = MemberQuery(self.driver) # 实例化会员档案查询页面
128         self.login.loginFunc() # 登录
129         menu.memberQueryMenu() # 找到会员档案查询页面
130         # 3个条件输入框随机输入数据
131         for inputBox in menu.memberNumNamePhone:
132             menu.iQueryCondition(inputBox,random.choice(menu.valuesList))
133         #会员类型下拉列表中随机选择一项
134         menu.selectMemberType()
135         text_type, member_type = menu.memberTypeLevelOption(*(random.choice(menu.memberTypeNum)))
136         ActionChains(self.driver).move_to_element(member_type).perform()  # 鼠标移动到下拉选项上
137         member_type.click()
138 
139         # menu.selectMemberLevel()
140         # text_level, member_level = menu.memberTypeLevelOption(*(random.choice(menu.member_level_num)))
141         # #ActionChains(self.driver).move_to_element(member_level).perform()
142         # member_level.click()
143 
144 
145         # 点击【重置】
146         menu.cQueryResetBtn(*menu.queryResetBtn[1])
147         # 获取前3个输入框的内容
148         text_list = []
149         for input_box in menu.memberNumNamePhone:
150             text = menu.getInputboxValue(*input_box)
151             text_list.append(text)
152         # 获取会员类型
153         type_type_text = menu.getInputboxValue(*menu.memberTypeBtn)
154         text_list.append(type_type_text)
155 
156         # type_level_text = menu.getInputboxValue(*menu.member_level_btn)
157         # text_list.append(type_level_text)
158 
159         # 断言每一个条件框是否为空 为空就通过
160         for get_attr in text_list:
161             self.assertEqual('',get_attr)
162 
163 if __name__ == '__main__':
164     pass

执行测试用例

 1 #! user/bin/python
 2 
 3 '''                                                           
 4 Code description:auto run test case
 5 Create time:
 6 Developer:
 7 '''
 8 
 9 import unittest
10 import time
11 from BeautifulReport import BeautifulReport
12 from retail.config.conf import *
13 from retail.test_case.models.testreport import testreport
14 
15 # TODO : will be use jenkins continuous intergration teachnology manage the auto project
16 if __name__ == '__main__':
17 
18     # currTime = time.strftime('%Y-%m-%d %H_%M_%S')
19     # filename = currTime + '.html'
20     # # 第一种测试报告
21     # test_suite = unittest.defaultTestLoader.discover(tcPath, pattern='*Tc.py')
22     # result = BeautifulReport(test_suite)
23     # result.report(filename= filename, description='test report', log_path=reportPath)
24 
25     # # 第二种测试报告
26     runner, fp, fileName = testreport()
27     test_suite = unittest.defaultTestLoader.discover(tcPath, pattern='LoginTc.py')
28     runner.run(test_suite)
29     fp.close()

from BeautifulReport import BeautifulReport 这个报告需要自己网上找一下(很多类似的测试报告源码,不一定非使用本案例中的报告模板)

报告展示

有付出才有汇报,接下来看下们的成果

1.截图:

创建规则失败时截图

登录成功截图

用例执行日志:

2018-10-12 15:39:17,916 - doconfIni.py:[36] - [INFO] - read excel value [D:\\Petrochina_Retail_Test_Project] successed! 
2018-10-12 15:39:19,119 - testreport.py:[35] - [INFO] - successed to generate test report [D:\\Petrochina_Retail_Test_Project\\retail\\report\\TestReport\\report2018-10-12 15_39_19.html]
2018-10-12 15:39:21,469 - doexcel.py:[31] - [INFO] - initing class ReadExcel
2018-10-12 15:39:21,481 - doexcel.py:[31] - [INFO] - initing class ReadExcel
2018-10-12 15:39:21,493 - doexcel.py:[31] - [INFO] - initing class ReadExcel
2018-10-12 15:39:21,504 - doexcel.py:[31] - [INFO] - initing class ReadExcel
2018-10-12 15:39:21,505 - doexcel.py:[46] - [INFO] - reading value [权限管理] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,538 - doexcel.py:[46] - [INFO] - reading value [会员档案] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,538 - doexcel.py:[46] - [INFO] - reading value [积分消费查询] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,539 - doexcel.py:[46] - [INFO] - reading value [功能演示] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,539 - doexcel.py:[46] - [INFO] - reading value [待办工作] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,539 - doexcel.py:[46] - [INFO] - reading value [报表] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,539 - doexcel.py:[46] - [INFO] - reading value [积分规则/活动查询] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,540 - doexcel.py:[46] - [INFO] - reading value [积分规则/活动申请] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,540 - doexcel.py:[46] - [INFO] - reading value [username] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,540 - doexcel.py:[46] - [INFO] - reading value [password] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,541 - doexcel.py:[46] - [INFO] - reading value [loginSubmitButton] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,541 - doexcel.py:[46] - [INFO] - reading value [checkcookie] from excel file [D:\\Petrochina_Retail_Test_Project\\retail\\data\\TestData\\elementDate.xlsx] completed
2018-10-12 15:39:21,541 - doexcel.py:[46] - [INFO] - reading value [inputTdRight] 以上是关于python&selenium自动化测试实战项目——全面完整详细(02)的主要内容,如果未能解决你的问题,请参考以下文章

python&selenium自动化测试实战项目——全面完整详细(02)

Python&Selenium 关键字驱动测试框架之数据文件解析

MongoDB入门实操《中篇》

selenium+python自动化81-html报告优化(饼图+失败重跑+兼容python2&3)

MongoDB入门实操《下篇》

selenium+python自动化81-html报告优化(饼图+失败重跑+兼容python2&3)转载