程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

第十八:第一個Python+Selenium自動化測試實戰項目(粗糙)

編輯:Python

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

二.項目介紹

1.目的:測試某官方網站登錄功能模塊可以正常使用。

2.用例
2.1.輸入格式正確的用戶名和正確的密碼,驗證是否登錄成功

2.2.輸入格式正確的用戶名和不正確的密碼,驗證是否登錄失敗,並且提示信息正確

2.3.輸入格式正確的用戶名和任意密碼,驗證是否登錄失敗,並且提示信息正確

2.4.用戶名和密碼兩者都為空,驗證是否登錄失敗,並且提示信息正確

2.5.用戶名和密碼兩者之一為空,驗證是否登錄失敗,並且提示信息正確

3.環境
Windows10 +Python3.6+selenium3.13+Pycharm

3.1.環境大多數人都會搭建,有事沒事找百度,一搜一籮筐,哈哈!自己剛學的時候也是各種問題各種百度,好在都解決了,感謝有度娘這麼強大的存在!這裡就不寫環境怎麼搭建了,直接進入主題。

三.腳本設計

1.目的
測試腳本要達到:腳本可移植,腳本模塊化,測試數據分離,輸出測試報告等目的

2.腳本設計模式

3.代碼實現

3.1.項目目錄結構

3.2.注:下面的文件存放在同一個目錄下

login_test.py

# 麥子學院登錄模塊自動化測試用例腳本
import time
from selenium import webdriver
from webinfo import webinfo
from userinfo import userinfo
from log_file import login_log
from pathlib import Path
from pathlib2 import Path
def open_web():
driver = webdriver.Chrome()
driver.maximize_window()
return driver
def load_url(driver,ele_dict):
# 調用實例化ele_dict,獲取返回的指定信息值
driver.get(ele_dict['Turl'])
time.sleep(5)
def find_element(driver,ele_dict):
# 調用實例化ele_dict,獲取返回的指定信息定位值
# find element
driver.find_element_by_class_name(ele_dict['image_id']).click()
if 'text_id' in ele_dict:
driver.find_element_by_link_text(u'登錄').click()
user_id = driver.find_element_by_id(ele_dict['userid'])
pwd_id = driver.find_element_by_id(ele_dict['pwdid'])
login_id = driver.find_element_by_id(ele_dict['loginid'])
return user_id,pwd_id,login_id
def send_val(ele_tuple,arg):
# ele_tuple參數是獲取到的實例化ele_dict的定位信息值
# input userinfo
listkey = ['uname','pwd']
i = 0
for key in listkey:
ele_tuple[i].send_keys('')
ele_tuple[i].clear()
ele_tuple[i].send_keys(arg[key])
i+=1
ele_tuple[2].click()
def check_login(driver,ele_dict,log,userlist):
result = False
time.sleep(3)
try:
err = driver.find_element_by_id(ele_dict['error'])
driver.save_screenshot(err.text+'.png')
log.log_write(U'賬號:%s 密碼:%s 提示信息:%s:failed\n' %(userlist['uname'],userlist['pwd'],err.text))
print('username or password error')
except:
print('login success!')
log.log_write(u'賬號:%s 密碼:%s :passed\n'%(userlist['uname'],userlist['pwd']))
#login_out(driver,ele_dict)
return True
return result
def login_out(driver,ele_dict):
driver.find_element_by_class_name(ele_dict['logout']).click()
''' def screen_shot(err): i = 0 save_path = r'G:\suitUIUnittestDemo' capturename = '\\'+str(i)+'.png' wholepath = save_path+capturename if Path(save_path).is_dir(): pass else: Path(save_path).mkdir() while Path(save_path).exists(): i+=1 capturename = '\\'+str(i)+'.png' wholepath = save_path + capturename err.screenshot(wholepath) '''
def login_test():
# 調用日志文件,並進行類的實例化
log = login_log()
#ele_dict = {'url': 'http://www.maiziedu.com/', 'text_id': '登錄', 'user_id': 'id_account_l', 'pwd_id': 'id_password_l'
#, 'login_id': 'login_btn','image_id':'close-windows-btn7','error_id':'login-form-tips'}
# 調用webinfo文件,並調用webinfo方法,實例化函數方法
ele_dict = webinfo(r'G:\suitUIUnittestDemo\webinfo.txt')
# 調用userinfo文件,並調用userinfo方法,實例化函數方法
#user_list=[{'uname':account,'pwd':pwd}]
user_list = userinfo(r'G:\suitUIUnittestDemo\userinfo.txt')
# 實例化驅動,調用open_web
driver = open_web()
# 調用load_url方法,並從實例化webinfo(ele_dict)獲取返回的ele_dict['Turl']數據
# load url
load_url(driver,ele_dict)
# 調用find_element方法,入參調用時候webinfo(ele_dict),獲取返回的定位信息
# find element
ele_tuple = find_element(driver,ele_dict)
# 調用日志文件,調用log_write方法
# send values
ftitle = time.strftime('%Y-%m-%d', time.gmtime())
log.log_write(u'\t\t\t%s登錄系統測試報告\n' % (ftitle))
#
for userlist in user_list:
# 調用send_val方法,ele_tuple參數是獲取到的實例化ele_dict的定位信息值,參數userlist是調用userinfo文件並實例化函數user_list內的值數據
send_val(ele_tuple,userlist)
# 調用check_login方法
# check login success or failed
result = check_login(driver,ele_dict,log,userlist)
if result:
# 調用login_out方法
login_out(driver,ele_dict)
time.sleep(3)
ele_tuple = find_element(driver,ele_dict)
time.sleep(3)
# 調用日志文件實例化中的check_login方法
log.log_close()
driver.quit()
if __name__ == '__main__':
login_test()

log_file.py

# 測試輸出報告
import time
class login_log(object):
def __init__(self,path='',mode='w'):
filename = path + time.strftime('%Y-%m-%d',time.gmtime())
self.log = open(path+filename+'.txt',mode)
def log_write(self,msg):
self.log.write(msg)
def log_close(self):
self.log.close()
if __name__ == '__main__':
log = login_log()
ftitle = time.strftime('%Y-%m-%d',time.gmtime())
print(ftitle)
log.log_write('xiaochao11520')
log.log_close()

webinfo.txt

Turl = https://mail.126.com
text_id = 登錄
userid = id_account_l
pwdid = id_password_l
loginid = login_btn
error = login-form-tips
logout = sign_out
image_id = close-windows-btn7

webinfo.py

# 從文本文檔中讀取web元素
import codecs
def webinfo(path):
file = codecs.open(path,'r','utf-8')
ele_dict = {
}
for line in file:
result = [ele.strip() for ele in line.split('=')]
ele_dict.update(dict([result]))
return ele_dict
if __name__ == '__main__':
ele_dict = webinfo(r'G:\suitUIUnittestDemo\webinfo.txt')
for key in ele_dict:
print(key,ele_dict[key])

userinfo.txt

uname = linuxxiaochao;pwd=xiaochao11520
uname = 273839363;pwd=xiaochao11520
uname = ;pwd=xiaochao11520
uname = [email protected];pwd=
uname = 2738;pwd=xiaochao

userinfo.py

# 從文本文檔中讀取用戶信息
import codecs
def userinfo(path):
file = codecs.open(path,'r','utf-8')
user_list = []
for line in file:
user_dict = {
}
result = [ele.strip() for ele in line.split(';')]
for sult in result:
re_sult = [ele.strip() for ele in sult.split('=')]
user_dict.update(dict([re_sult]))
user_list.append(user_dict)
return user_list
if __name__ == '__main__':
user_list = userinfo(r'G:\suitUIUnittestDemo\userinfo.txt')
print(user_list)

  1. 上一篇文章:
  2. 下一篇文章:
相關文章
    没有相关文章
Copyright © 程式師世界 All Rights Reserved