基于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()

  

猜你喜欢

转载自www.cnblogs.com/gcgc/p/9497339.html