说明:本项目采用流程控制思想,未引用unittest&pytest等单元测试框架
一.项目介绍
目的
测试某官方网站登录功能模块可以正常使用
用例
1.输入格式正确的用户名和正确的密码,验证是否登录成功;
2.输入格式正确的用户名和不正确的密码,验证是否登录失败,并且提示信息正确;
3.输入格式正确的用户名和任意密码,验证是否登录失败,并且提示信息正确;
4.用户名和密码两者都为空,验证是否登录失败,并且提示信息正确;
5.用户名和密码两者之一为空,验证是否登录失败,并且提示信息正确;
环境
Windows10 +Python3.6+selenium3.13+Pycharm
环境我想大多数人都会搭建,有事没事找百度,一搜一箩筐,哈哈!我自己刚学的时候也是各种问题各种百度,好在都解决了,感谢有度娘这么强大的存在!这里就不写环境怎么搭建了,直接进入主题
二.脚本设计
目的
我们的测试脚本需要达到:脚本可移植,脚本模块化,测试数据分离,输出测试报告 等目的
脚本设计模式
代码实现
项目目录结构
注:下面的文件存放在同一个目录下
1 #! user/bin/python2 '''3 代码说明:麦子学院登录模块自动化测试用例脚本4 编写日期:5 设置者:linux超6 '''7 8 import time9 from selenium import webdriver10 from webinfo import webinfo11 from userinfo import userinfo12 from log_fiile import login_log13 from pathlib import Path14 15 def open_web():16 driver = webdriver.Firefox()17 driver.maximize_window()18 return driver19 20 def load_url(driver,ele_dict):21 driver.get(ele_dict['Turl'])22 time.sleep(5)23 24 def find_element(driver,ele_dict):25 # find element26 driver.find_element_by_class_name(ele_dict['image_id']).click()27 if 'text_id' in ele_dict:28 driver.find_element_by_link_text('登录').click()29 30 user_id = driver.find_element_by_id(ele_dict['userid'])31 pwd_id = driver.find_element_by_id(ele_dict['pwdid'])32 login_id = driver.find_element_by_id(ele_dict['loginid'])33 return user_id,pwd_id,login_id34 35 def send_val(ele_tuple,arg):36 # input userinfo37 listkey = ['uname','pwd']38 i = 039 for key in listkey:40 ele_tuple[i].send_keys('')41 ele_tuple[i].clear()42 ele_tuple[i].send_keys(arg[key])43 i+=144 ele_tuple[2].click()45 def check_login(driver,ele_dict,log,userlist):46 result = False47 time.sleep(3)48 try:49 err = driver.find_element_by_id(ele_dict['error'])50 driver.save_screenshot(err.text+'.png')51 log.log_write('账号:%s 密码:%s 提示信息:%s:failed\n' %(userlist['uname'],userlist['pwd'],err.text))52 print('username or password error')53 except:54 print('login success!')55 log.log_write('账号:%s 密码:%s :passed\n'%(userlist['uname'],userlist['pwd']))56 #login_out(driver,ele_dict)57 return True58 return result59 def login_out(driver,ele_dict):60 driver.find_element_by_class_name(ele_dict['logout']).click()61 '''62 def screen_shot(err):63 i = 064 save_path = r'D:\pythondcode\capture'65 capturename = '\\'+str(i)+'.png'66 wholepath = save_path+capturename67 if Path(save_path).is_dir():68 pass69 else:70 Path(save_path).mkdir()71 while Path(save_path).exists():72 i+=173 capturename = '\\'+str(i)+'.png'74 wholepath = save_path + capturename75 err.screenshot(wholepath)76 '''77 def login_test():78 log = login_log()79 #ele_dict = {'url': 'http://www.maiziedu.com/', 'text_id': '登录', 'user_id': 'id_account_l', 'pwd_id': 'id_password_l'80 #, 'login_id': 'login_btn','image_id':'close-windows-btn7','error_id':'login-form-tips'}81 ele_dict = webinfo(r'D:\pythoncode\webinfo.txt')82 #user_list=[{'uname':account,'pwd':pwd}]83 user_list = userinfo(r'D:\pythoncode\userinfo.txt')84 driver = open_web()85 # load url86 load_url(driver,ele_dict)87 #find element88 ele_tuple = find_element(driver,ele_dict)89 # send values90 ftitle = time.strftime('%Y-%m-%d', time.gmtime())91 log.log_write('\t\t\t%s登录系统测试报告\n' % (ftitle))92 for userlist in user_list:93 send_val(ele_tuple,userlist)94 # check login success or failed95 result = check_login(driver,ele_dict,log,userlist)96 if result:97 login_out(driver,ele_dict)98 time.sleep(3)99 ele_tuple = find_element(driver,ele_dict)
100 time.sleep(3)
101 log.log_close()
102 driver.quit()
103
104 if __name__ == '__main__':
105 login_test()
1 #! user/bin/python2 '''3 代码说明:从文本文档中读取用户信息4 编写日期:5 设置者:linux超6 '''7 8 import codecs9
10 def userinfo(path):
11 file = codecs.open(path,'r','utf-8')
12 user_list = []
13 for line in file:
14 user_dict = {}
15 result = [ele.strip() for ele in line.split(';')]
16 for sult in result:
17 re_sult = [ele.strip() for ele in sult.split('=')]
18 user_dict.update(dict([re_sult]))
19 user_list.append(user_dict)
20 return user_list
21
22 if __name__ == '__main__':
23 user_list = userinfo(r'D:\pythoncode\userinfo.txt')
24 print(user_list)
1 #! user/bin/python2 '''3 代码说明:从文本文档中读取web元素4 编写日期:5 设置者:linux超6 '''7 8 import codecs9
10 def webinfo(path):
11 file = codecs.open(path,'r','gbk')
12 ele_dict = {}
13 for line in file:
14 result = [ele.strip() for ele in line.split('=')]
15 ele_dict.update(dict([result]))
16 return ele_dict
17
18 if __name__ == '__main__':
19 ele_dict = webinfo(r'D:\pythoncode\webinfo.txt')
20 for key in ele_dict:
21 print(key,ele_dict[key])
1 #! user/bin/python2 '''3 代码说明:测试输出报告4 编写日期:5 设置者:linux超6 '''7 8 import time9
10 class login_log(object):
11 def __init__(self,path='',mode='w'):
12 filename = path + time.strftime('%Y-%m-%d',time.gmtime())
13 self.log = open(path+filename+'.txt',mode)
14 def log_write(self,msg):
15 self.log.write(msg)
16 def log_close(self):
17 self.log.close()
18 if __name__ == '__main__':
19 log=login_log()
20 ftitle = time.strftime('%Y-%m-%d',time.gmtime())
21 log.log_write('xiaochao11520')
22 log.log_close()
1 uname=273839363@qq.com;pwd=xiaochao11520
2 uname=273839363;pwd=xiaochao11520
3 uname= ;pwd=xiaochao11520
4 uname=273839363@qq.com;pwd=
5 uname=2738;pwd=xiaochao
1 Turl=http://www.maiziedu.com/
2 text_id=登录
3 userid=id_account_l
4 pwdid=id_password_l
5 loginid=login_btn
6 error=login-form-tips
7 logout=sign_out
8 image_id=close-windows-btn7
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!