python selenium框架_基于python+selenium的框架思路
設想:
1、使用excel編寫用例第一個sheet頁為用例概要格式如下:
后面的sheet頁為具體的用例步驟:
實現所有定位信息都與測試代碼分離
2、讀取該excel文件取出關鍵字等信息,作為關鍵字的參數,通過反射機制傳遞給關鍵字方法去執行。
關鍵字模塊如下:ObjectMap.py
# coding:utf-8
from selenium.webdriver.support.ui import WebDriverWait
#獲取單個頁面元素對象
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
# 由于關鍵字函數的參數個數不一樣,所以通過傳遞動態參數*args實現傳參,關鍵字方法
# 最多需要(driver , locationType, locatorExpression, operationValue)四個參數
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()
測試執行代碼如下:
# coding:utf-8
from util import ObjectMap, ExcelUtil
import xlrd, xlwt
import time
from xlutils.copy import copy
def baidu_search():
#初始化操作,創建driver
from selenium import webdriver
start_time = time.time()
# print start_time
driver = webdriver.Chrome()
#讀取excel中的關鍵字的值,定位方式的值,定位表達式,和操作值等參數值。然后將參數值傳到對應關鍵字方法中
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
# 要用到反射機制,通過函數名字符串調用對應方法:http://www.liujiangblog.com/course/python/48
if hasattr(ObjectMap, keyword):
# print ‘有這個方法‘
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()
總結
以上是生活随笔為你收集整理的python selenium框架_基于python+selenium的框架思路的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python控制电机_树莓派Python
- 下一篇: websocket python爬虫_p