python selenium框架_基于python+selenium的框架思路
設(shè)想:
1、使用excel編寫用例第一個(gè)sheet頁為用例概要格式如下:
后面的sheet頁為具體的用例步驟:
實(shí)現(xiàn)所有定位信息都與測(cè)試代碼分離
2、讀取該excel文件取出關(guān)鍵字等信息,作為關(guān)鍵字的參數(shù),通過反射機(jī)制傳遞給關(guān)鍵字方法去執(zhí)行。
關(guān)鍵字模塊如下:ObjectMap.py
# coding:utf-8
from selenium.webdriver.support.ui import WebDriverWait
#獲取單個(gè)頁面元素對(duì)象
def get_element(driver, locationType, locatorExpression):
try:
element = WebDriverWait(driver, 30).until(lambda x:x.find_element(by=locationType,value = locatorExpression))
return element
except Exception, e:
raise e
def get_elements(driver , locationType, locatorExpression):
try:
elements = WebDriverWait(driver, 30).until(lambda x:x.find_elements(by=locationType,value=locatorExpression))
return elements
except Exception, e:
raise e
# 由于關(guān)鍵字函數(shù)的參數(shù)個(gè)數(shù)不一樣,所以通過傳遞動(dòng)態(tài)參數(shù)*args實(shí)現(xiàn)傳參,關(guān)鍵字方法
# 最多需要(driver , locationType, locatorExpression, operationValue)四個(gè)參數(shù)
def open_browser(driver, *args):
driver.get(args[2])
def input_string(driver, *args):
WebDriverWait(driver, 30).until(lambda x: x.find_element(by=args[0], value=args[1])).send_keys(args[2])
def click(driver, *args):
WebDriverWait(driver, 30).until(lambda x: x.find_element(by=args[0], value=args[1])).click()
測(cè)試執(zhí)行代碼如下:
# coding:utf-8
from util import ObjectMap, ExcelUtil
import xlrd, xlwt
import time
from xlutils.copy import copy
def baidu_search():
#初始化操作,創(chuàng)建driver
from selenium import webdriver
start_time = time.time()
# print start_time
driver = webdriver.Chrome()
#讀取excel中的關(guān)鍵字的值,定位方式的值,定位表達(dá)式,和操作值等參數(shù)值。然后將參數(shù)值傳到對(duì)應(yīng)關(guān)鍵字方法中
excelFile = xlrd.open_workbook(r"D:\KeyWordsFrameWork\testScripts\search.xlsx", formatting_info=True)
sheet = excelFile.sheet_by_index(1)
maxRows = sheet.nrows
# print maxRows
for row in range(1, maxRows-1):
keyword = sheet.row_values(row)[2]
locationType = sheet.row_values(row)[3]
locatorExpression = sheet.row_values(row)[4]
operationValue = sheet.row_values(row)[5]
# dir(ObjectMap)獲取該模塊的所有方法和變量
# print dir(ObjectMap)
for i in dir(ObjectMap):
if keyword == i:
# print i
# 要用到反射機(jī)制,通過函數(shù)名字符串調(diào)用對(duì)應(yīng)方法:http://www.liujiangblog.com/course/python/48
if hasattr(ObjectMap, keyword):
# print ‘有這個(gè)方法‘
func = getattr(ObjectMap, keyword)
func(driver, locationType, locatorExpression, operationValue)
end_time = time.time()
take_time = end_time-start_time
print take_time
excleFileCopy = copy(excelFile)
case_sheet = excleFileCopy.get_sheet(0)
case_sheet.write(1,5,take_time)
excleFileCopy.save(r"D:\KeyWordsFrameWork\testScripts\search.xlsx")
if __name__ == ‘__main__‘:
baidu_search()
總結(jié)
以上是生活随笔為你收集整理的python selenium框架_基于python+selenium的框架思路的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python控制电机_树莓派Python
- 下一篇: pythontkinter在一块区域中绘