測試驅動之excel文件在自動化中的使用(十二)

一般性的,數據存儲在excel中,也是一種選擇,但是必須安裝對應的庫,要不python是無法操作excel文件的,安裝的第三方庫爲爲xlrd,安裝命令爲:

pipinstall xlrd

安裝過程見截圖:

Excel文件的後綴格式爲.xlsx,實例中excel的數據爲:

所以,我們需要讀取excel中的數據,首先需要import xlrd,然後纔可以讀取excel文件中的數據。在excel文件中,cell是單元格,sheet是工作表,一個工作表由N個單元格來組成。下面來實現讀取excel文件中的數據,見如下的代碼:

def getExcel(rowValue,colValue,file_name='d:\\test.xlsx'):

"""

:paramrowValue:表格的行

:paramcolValue: 表格的列

:paramfile_name: excel文件

:return:

"""

#創建book

book=xlrd.open_workbook(file_name)

#獲取sheet對象

sheet=book.sheet_by_index(0)

#獲取sheet對象中的數據

returnsheet.cell_value(rowValue,colValue)

代碼截圖:

我把讀取excel中的數據寫成一個函數,先導入xlrd的庫,然後創建book,以及獲取sheet對象,依次獲取sheet對象中的數據,在如上的excel數據中,如果我想獲取“請你填寫密碼”,那麼直接調用該函數,並且傳對應的參數分別爲(0,1),見執行的代碼截圖:

如果讀取excel一個sheet對象的所有數據,修改後的代碼爲:

defgetExcels(file_name='d:\\test.xlsx'):

rows=[]

book=xlrd.open_workbook(file_name)

sheet=book.sheet_by_index(0)

forrow in range(1,sheet.nrows):

rows.append(list(sheet.row_values(row,0,sheet.ncols)))

returnrows

我們已百度登錄爲實例,來說明excel文件在自動化中的引用,測試點分別爲:

1、輸入百度賬號,未輸入百度密碼,點擊登陸,驗證返回的錯誤信息;

2、輸入錯誤的百度賬號密碼,點擊登錄,驗證返回的錯誤信息;

我們讀excel文件的函數,登錄百度的函數寫在location.py的模塊中,見location.py模塊的代碼:

#!/usr/bin/env python

#coding:utf-8

import csv,xlrd

from selenium import webdriver

import time as t

def getCsv(file_name):

rows=[]

withopen(file_name,'rb') as f:

readers=csv.reader(f,delimiter=',',quotechar='|')

next(readers,None)

forrow in readers:

rows.append(row)

returnrows

defgetExcel(rowValue,colValue,file_name='d:\\test.xlsx'):

"""

:paramrowValue:表格的行

:paramcolValue: 表格的列

:paramfile_name: excel文件

:return:

"""

book=xlrd.open_workbook(file_name)

sheet=book.sheet_by_index(0)

returnsheet.cell_value(rowValue,colValue)

def clickButton(driver):

driver.find_element_by_xpath(".//*[@id='u1']/a[6]").click()

t.sleep(2)

def clickLogin(driver,username,password):

name=driver.find_element_by_id('TANGRAM__PSP_8__userName')

name.clear()

name.send_keys(username)

t.sleep(2)

passwd=driver.find_element_by_id('TANGRAM__PSP_8__password')

passwd.clear()

passwd.send_keys(password)

t.sleep(2)

driver.find_element_by_id('TANGRAM__PSP_8__submit').click()

t.sleep(2)

#獲取返回的錯誤信息

def getText(driver):

returndriver.find_element_by_xpath(".//*[@id='TANGRAM__PSP_8__error']").text

把測試代碼寫在baiduTest.py的模塊中,見該模塊的測試代碼:

#!/usr/bin/python

#coding:utf-8

from selenium import webdriver

import time as t

import location

import unittest,sys

reload(sys)

sys.setdefaultencoding('utf-8')

class BaiduTest(unittest.TestCase):

defsetUp(self):

self.driver=webdriver.Firefox()

self.driver.maximize_window()

self.driver.implicitly_wait(30)

self.driver.get('http://www.baidu.com/')

deftestCase_01(self):

"""

驗證只輸入百度賬號密碼,點擊登錄返回的錯誤信息

:return:

"""

driver=self.driver

location.clickButton(driver)

location.clickLogin(driver,location.getExcel(0,0),location.getExcel(1,0))

self.assertEqual(location.getText(driver),location.getExcel(1,1))

deftestCase_02(self):

"""

驗證只輸入百度賬號,未輸入密碼,點擊登錄返回的錯誤信息

:return:

"""

driver=self.driver

location.clickButton(driver)

location.clickLogin(driver,location.getExcel(0,0),location.getExcel(2,0))

self.assertEqual(location.getText(driver),location.getExcel(0,1))

deftearDown(self):

self.driver.quit()

if __name__=='__main__':

suite=unittest.TestLoader().loadTestsFromTestCase(BaiduTest)

unittest.TextTestRunner(verbosity=2).run(suite)

這樣,我們就實現了把測試中使用到的數據,存儲在excel中,然後利用xlrd模塊來讀取excel中的數據,達到測試代碼與數據的分離。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章