日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python selenium框架_基于python+selenium的框架思路

發布時間:2025/3/20 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的框架思路的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。