我是靠谱客的博主 欣喜草莓,最近开发中收集的这篇文章主要介绍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:KeyWordsFrameWorktestScriptssearch.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:KeyWordsFrameWorktestScriptssearch.xlsx")

if __name__ == ‘__main__‘:

baidu_search()

最后

以上就是欣喜草莓为你收集整理的python selenium框架_基于python+selenium的框架思路的全部内容,希望文章能够帮你解决python selenium框架_基于python+selenium的框架思路所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部