說明:本項目采用流程控制思想,未引用unittest&pytest等單元測試框架

  一.項目介紹
  目的
  測試某官方網站登錄功能模塊可以正常使用
  用例
  1.輸入格式正確的用戶名和正確的密碼,驗證是否登錄成功;
  2.輸入格式正確的用戶名和不正確的密碼,驗證是否登錄失敗,并且提示信息正確;
  3.輸入格式正確的用戶名和任意密碼,驗證是否登錄失敗,并且提示信息正確;
  4.用戶名和密碼兩者都為空,驗證是否登錄失敗,并且提示信息正確;
  5.用戶名和密碼兩者之一為空,驗證是否登錄失敗,并且提示信息正確;
  環境
  Windows10 +Python3.6+selenium3.13+Pycharm
  環境我想大多數人都會搭建,有事沒事找百度,一搜一籮筐,哈哈!我自己剛學的時候也是各種問題各種百度,好在都解決了,感謝有度娘這么強大的存在!這里就不寫環境怎么搭建了,直接進入主題
  二.腳本設計
  目的
  我們的測試腳本需要達到:腳本可移植,腳本模塊化,測試數據分離,輸出測試報告 等目的
  腳本設計模式
?
  代碼實現
  項目目錄結構
?
  注:下面的文件存放在同一個目錄下
? ?1 #! user/bin/python
  2 '''
  3 代碼說明:麥子學院登錄模塊自動化測試用例腳本
  4 編寫日期:
  5 設置者:linux超
  6 '''
  7
  8 import time
  9 from selenium import webdriver
  10 from webinfo import webinfo
  11 from userinfo import userinfo
  12 from log_fiile import login_log
  13 from pathlib import Path
  14
  15 def open_web():
  16? ? ?driver = webdriver.Firefox()
  17? ? ?driver.maximize_window()
  18? ? ?return driver
  19
  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 element
  26? ? ?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_id
  34
  35 def send_val(ele_tuple,arg):
  36? ? ?# input userinfo
  37? ? ?listkey = ['uname','pwd']
  38? ? ?i = 0
  39? ? ?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+=1
  44? ? ?ele_tuple[2].click()
  45 def check_login(driver,ele_dict,log,userlist):
  46? ? ?result = False
  47? ? ?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 True
  58? ? ?return result
  59 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 = 0
  64? ? ?save_path = r'D:\pythondcode\capture'
  65? ? ?capturename = '\\'+str(i)+'.png'
  66? ? ?wholepath = save_path+capturename
  67? ? ?if Path(save_path).is_dir():
  68? ? ? ? ?pass
  69? ? ?else:
  70? ? ? ? ?Path(save_path).mkdir()
  71? ? ?while Path(save_path).exists():
  72? ? ? ? ?i+=1
  73? ? ? ? ?capturename = '\\'+str(i)+'.png'
  74? ? ? ? ?wholepath = save_path + capturename
  75? ? ?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 url
  86? ? ?load_url(driver,ele_dict)
  87? ? ?#find element
  88? ? ?ele_tuple = find_element(driver,ele_dict)
  89? ? ?# send values
  90? ? ?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 failed
  95? ? ? ? ?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/python
  2 '''
  3 代碼說明:從文本文檔中讀取用戶信息
  4 編寫日期:
  5 設置者:linux超
  6 '''
  7
  8 import codecs
  9
  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/python
  2 '''
  3 代碼說明:從文本文檔中讀取web元素
  4 編寫日期:
  5 設置者:linux超
  6 '''
  7
  8 import codecs
  9
  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/python
  2 '''
  3 代碼說明:測試輸出報告
  4 編寫日期:
  5 設置者:linux超
  6 '''
  7
  8 import time
  9
  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 [email protected];pwd=xiaochao11520
  2 uname=273839363;pwd=xiaochao11520
  3 uname= ;pwd=xiaochao11520
  4 [email protected];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