Python結(jié)合MySQLdb,urllib等庫主要進(jìn)行偏后臺數(shù)據(jù)庫的自動化測試翁狐,同時頁面元素涉及處理表格元素,并將獲取的表格內(nèi)容進(jìn)行處理。
u 如何遍歷一個表格并獲取表格中需要的內(nèi)容
思路是先獲得表格行(tr),再通過行遍歷所有的列(td),從而獲得每一行的內(nèi)容放入一個列表详幽,通過對列表的下標(biāo)處理獲得需要的內(nèi)容。
""""""
table_tr_list=browser.find_element_by_xpath('//[@id="tab_con"]/div[1]/div[2]/table').find_elements_by_tag_name('tr')#******取得所有行*** table_list=[] #*****定義表格總內(nèi)容列表* for tr in table_tr_list: table_td_list=tr.find_elements_by_tag_name('td')#****通過每一行尋找這一行中每一列的所有元素** row_list=[]
for td in table_td_list: row_list.append(td.text)#****將獲得的每一個元素內(nèi)容追加進(jìn)****row_list****列表 table_list.append(row_list)#******將獲得的每一行的列表放入******table_list******列表中*****combination_name=[]
for i in table_list:
if i==[]:
pass
else: k=i[0]
combination_name.append(k)#****取得需要的內(nèi)容*
u 數(shù)據(jù)庫數(shù)據(jù)獲得
當(dāng)處理的數(shù)據(jù)涉及多個庫時浸锨,只需要連接數(shù)據(jù)庫中某一個庫即可對數(shù)據(jù)庫中所有表格進(jìn)行操作唇聘,但是此時一些存儲過程,表明等都需要加上databasename,比如表明用database_name.table_name來表示具體例子如下:
from selenium import webdriver
from time import sleep
import MySQLdb
import json
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )#****使****python****可以認(rèn)識非****ASC****編碼柱搜,例如中文 browser=webdriver.Firefox()
browser.get("http:\master-test.simuwang.com")#****進(jìn)入官網(wǎng)browser.maximize_window()
sleep(5) browser.find_element_by_xpath(".//[@id='input_nick_lg']").send_keys(username)#****輸入賬號* browser.find_element_by_xpath("http://[@id='input_pwd_lg']").send_keys(passwd)#****輸入密碼* browser.find_element_by_xpath(".//[@id='login_lgx']").click()#****點擊登錄* sleep(5)
browser.find_element_by_class_name('ignore').click()#****點擊按鈕關(guān)閉彈窗 js="var q=document.documentElement.scrollTop=800"
browser.execute_script(js)
table_tr_list=browser.find_element_by_xpath('//[@id="tab_con"]/div[3]/div[2]/table').find_elements_by_tag_name('tr')
table_list=[]
for tr in table_tr_list:
table_td_list=tr.find_elements_by_tag_name('td')
row_list=[]
for td in table_td_list:
row_list.append(td.text)
table_list.append(row_list)
attention_company_name=[]#****取得頁面表格中所有數(shù)據(jù)* for i in table_list:
if i==[]:
pass
else:
attention_company_name.append(i[0])
connect=MySQLdb.connect(host='211.154.153.13',user='rz_cm_master',passwd='TbLuENLK',db='rz_combination_master',charset='gbk')
cursor=connect.cursor()
cursor.execute("SELECT company_short_name FROM rz_data_pool.company_information
WHERE company_id IN ( SELECT company_id FROM rz_combination_master.cm_user_attention_company WHERE userid=(SELECT userid FROM rz_combination_master.cm_user WHERE cellphone=rz_combination_master.func_change_number('15818650780',1,1)))")#****從數(shù)據(jù)庫中獲取用戶關(guān)注機(jī)構(gòu)名字
此處只使用一個****connect****即實現(xiàn)了連接多個庫的效果 result=cursor.fetchall()
list=[]
for i in result:
x=i[0]
y=x.split(',')[0]
list.append(y)
match_attention_company_name=0
for i in attention_company_name:
if i in list:
match_attention_company_name+=1 else:
print "用戶關(guān)注機(jī)構(gòu)與數(shù)據(jù)庫不匹配" break
if match_attention_company_name==len(list):
print "用戶關(guān)注機(jī)構(gòu)與數(shù)據(jù)庫完全匹配"
u 涉及文件下載的處理
進(jìn)行自動化過程中有些用例需要用到文件下載迟郎,但是下載過程中瀏覽器總是彈出窗口,但是這個窗口又屬于系統(tǒng)窗口無法定位聪蘸,所有只能用非常之法處理宪肖,因為我用的是火狐瀏覽器,所有一下火狐瀏覽器處理會清晰一些健爬,谷歌瀏覽器只提供思路控乾。
首先通過以下代碼是沒用的:
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.dir','d:\')
profile.set_preference('browser.download.folderList',2)
profile.set_preference('browser.download.manager.showWhenStarting',False)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk','application/zip')
幾經(jīng)周折,最后找到的方法是重新建立一個firefox配置文件娜遵,使用配置文件打開瀏覽器并手動進(jìn)行一次下載蜕衡,下載后進(jìn)去瀏覽器設(shè)置,改變下載路徑和對應(yīng)需下載文件類型的下載不提示設(shè)置设拟,完了關(guān)閉慨仿。
建立配置文檔流程如下:
開始à運行à輸入firefox.exe –ProfileManagerà創(chuàng)建配置文件à設(shè)置配置文件路徑(這里需要將配置文件路徑記下來鸽扁,最好手動輸入吧- -!,頁面整個路徑就是配置文檔的完整路徑镶骗,后面需要用到)à保存à使用配置文件打開瀏覽器à下載與需要自動化驗證下載的文件類型à點開瀏覽器設(shè)置à設(shè)置路徑并查看對應(yīng)文件類型是否已經(jīng)默認(rèn)不提示下載à關(guān)閉瀏覽器
編輯test:
profile = webdriver.FirefoxProfile('C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\se3w4tfi.downloadd')#****配置文件路徑 browser=webdriver.Firefox(profile)#****使用配置文件路徑打開瀏覽器 browser.get("http://master-test.simuwang.com")#****進(jìn)入官網(wǎng)****
browser.maximize_window()
需要注意的是桶现,如果腳本中存在有登錄過程,賬號密碼可能已經(jīng)被配置文件記住鼎姊,所以會直接跳過登錄過程骡和,因此腳本中也需要刪除登錄代碼。
Google瀏覽器處理方式:
from selenium import webdriver
from time import sleep
options = webdriver.ChromeOptions()
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'd:\'} #download.default.dorectory****:設(shè)置下載路徑
#profile.default content settings.popups:****設(shè)置為****0****禁止彈窗 options.add_experimental_option('prefs', prefs)#****將設(shè)置導(dǎo)入新打開的瀏覽器 driver = webdriver.Chrome(executable_path='D:\chromedriver.exe', chrome_options=options)
driver.get('http://master-test.simuwang.com')
就完事了
u 數(shù)據(jù)格式處理
在用例編寫過程中相寇,很多時候需要進(jìn)行斷言慰于,一般我是使用的self.assertEqual()方法。此間涉及很多數(shù)據(jù)格式處理唤衫,因為表面相同的數(shù)據(jù)當(dāng)格式不同時婆赠,也會比較成為不同。
在進(jìn)行數(shù)據(jù)處理之前可以對取得數(shù)據(jù)使用print type()查看數(shù)據(jù)類型佳励。
實例一:
在寫腳本過程中我需要進(jìn)行文件下載驗證休里,但是下載路徑是中文名字
---C:\用戶中心\下載--- 文件名字是從頁面取得的unicode類型
在腳本書寫時涉及到兩個部分的處理:1、因為系統(tǒng)只認(rèn)識gbk編碼的中文赃承,所以需要將路徑中的中文目錄進(jìn)行g(shù)bk轉(zhuǎn)碼妙黍,2、文件名是從頁面取得瞧剖,也需要轉(zhuǎn)碼為gbk拭嫁,具體代碼如下:
import os.path
file_name="****基金組合****"#****默認(rèn)導(dǎo)出文件名稱 file='D:\'+u'****用戶目錄****'.encode('gbk')+'\'+u'****下載****'.encode('gbk')+'\'+file_name.decode('utf-8').encode('gbk')+'.csv'#****對中文文件名進(jìn)行轉(zhuǎn)碼 result=os.path.exists(file)
self.assertEqual(result,True,'****導(dǎo)出文件失敗****')#****斷言導(dǎo)出文件存在
Unicode轉(zhuǎn)GBK----str.encode(‘gbk’)
Str格式轉(zhuǎn)換GBK---str.decode(‘utf-8’).encode(‘gbk’)
實例二:
在寫腳本過程中我經(jīng)常會需要驗證數(shù)據(jù)庫中數(shù)據(jù)與頁面表格中數(shù)據(jù)的一致性,但是數(shù)據(jù)庫中數(shù)據(jù)直接用MysqlDb取得后格式是(‘data’,)抓于,無法用于數(shù)據(jù)比對做粤,另外存在一些Decimal數(shù)據(jù),格式為decimal(‘data’,)捉撮,為處理這些數(shù)據(jù)怕品,使之成為可以比對的整型或浮點型,有一下兩個方法
方法1(主要用于取得一些字符串?dāng)?shù)據(jù)):
def Obtain_database_data(sql_string):
import MySQLdb
connect=MySQLdb.connect(host='****數(shù)據(jù)庫地址****',user='username',passwd='passwd',db='databasename',charset='gbk')
cursor=connect.cursor()
cursor.execute(sql_string)#****從數(shù)據(jù)庫中獲取用戶自定義組合名字 result=cursor.fetchall()
list=[]
for i in result:
x=i[0]
y=x.split(',')[0]
list.append(y)
return list
方法2(主要用于處理decimal類型數(shù)據(jù)):
def Get_decimal_data(sql_string):#****獲得數(shù)據(jù)庫中****decimal****數(shù)據(jù)類型并進(jìn)行轉(zhuǎn)化 import MySQLdb
connect=MySQLdb.connect(host='****數(shù)據(jù)庫地址****',user='username',passwd='passwd',db='databasename',charset='gbk')
cursor=connect.cursor()
cursor.execute(sql_string)
result=cursor.fetchall()
list=[]
for i in result:
for j in i:
if len(str(j))==4:
list.append('None')#****數(shù)據(jù)庫中為空時的處理 else:
m=float(str(float(j))[:-2])#****數(shù)據(jù)庫中不為空時處理 list.append(m)
return list