之前写过PO模式、数据驱动的测试框架,最近要做一个测试平台,先写一个关键字驱动的测试框架练练手

Excel 样式展示

如图
在这里插入图片描述

结果展示

在这里插入图片描述

代码

结构
在这里插入图片描述

  1. 读取excel
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/9/8 2:11
# @Author  : Paulson
# @File    : opera_excel.py
# @Software: PyCharm
# @define  : function


import xlrd


class OperaExcel:
    def __init__(self, file_path=None):
        if file_path is None:
            self.file_path = '../dataconfig/selenium.xlsx'
        else:
            self.file_path = file_path
        self.excel = self.get_excel()
    
    def get_excel(self):
        tables = xlrd.open_workbook(self.file_path)
        return tables
    
    def get_sheet(self, i=None):
        if i is None:
            i = 0
        sheet_data = self.excel.sheets()[i]
        return sheet_data
    
    def get_lines(self):
        """获取行数"""
        lines = self.get_sheet().nrows
        return lines
    
    def get_cell(self, row, cell):
        """获取单元格内容"""
        data = self.get_sheet().cell(row, cell).value
        return data


if __name__ == '__main__':
    opera = OperaExcel()
    print(opera.get_lines())
    print(opera.get_cell(0, 1))

  1. 使用Excel中关键字对selenium 常用操作方法的封装

这里这有基本方法,可以自己丰富其他的的基本操作

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/9/8 1:35
# @Author  : Paulson
# @File    : base.py
# @Software: PyCharm
# @define  : function
import time

from selenium import webdriver
from util.get_by_local import GetByLocal


class ActionMethod:
    def open_browser(self, *args):
        """打开浏览器"""
        # self.driver = None
        browser = args[0]
        print(browser)
        if browser == "chrome":
            self.driver = webdriver.Chrome()
        else:
            self.driver = webdriver.Firefox()
            
    def get_url(self, *args):
        """打开 url"""
        url = args[0]
        self.driver.get(url)
        
    def get_element(self, *args):
        """定位方式"""
        key = args[0]
        get_by_element = GetByLocal(self.driver)
        element = get_by_element.get_local_element(key)
        return element
    
    def element_send_keys(self, *args):
        """对元素进行输入"""
        
        key, value = args[0], args[1]
        element = self.get_element(key)
        element.send_keys(value)
        
    def click_element(self, *args):
        """点击元素"""
        key = args[0]
        element = self.get_element(key)
        element.click()
        
    def sleep_time(self):
        """等待"""
        time.sleep(3)
        
    def close_browser(self):
        """关闭浏览器"""
        self.driver.quit()
     
     
    
  1. 对selenium定位方法的通用方法提供的util方法
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/9/8 1:56
# @Author  : Paulson
# @File    : get_by_local.py
# @Software: PyCharm
# @define  : function

class GetByLocal:
    def __init__(self, driver):
        self.driver = driver
    
    def get_local_element(self, key):
        """
        name=email
        :return:
        """
        by = key.split('=')[0]
        by_value = key.split('=')[1]
        if by == 'id':
            return self.driver.find_element_by_id(by_value)
        elif by == 'name':
            return self.driver.find_element_by_name(by_value)
        elif by == 'className':
            return self.driver.find_element_by_class_name(by_value)
        else:
            return self.driver.find_element_by_xpath(by_value)
        
  1. 执行case

这里使用了反射机制,需要注意传参的类型与数目

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/9/8 2:13
# @Author  : Paulson
# @File    : test_case.py
# @Software: PyCharm
# @define  : function
# import sys
# sys.path.append(r"C:\Users\ybsde\PycharmProjects\Life_Process_Study\StudyPacticePython\selenium_pageObject\selenium_excel")

from util.opera_excel import OperaExcel
from base.base import ActionMethod


class TestCase:
    def __init__(self):
        self.handle_excel = OperaExcel()
        self.action_method = ActionMethod()
        
    def run_main(self):
        
        case_lines = self.handle_excel.get_lines()
        for i in range(1, case_lines):
            is_run = self.handle_excel.get_cell(i, 2)
            if is_run == "yes":
                case_num = self.handle_excel.get_cell(i, 0)
                print(f"开始测试: {case_num}")
                method = self.handle_excel.get_cell(i, 3)
                handle_value = self.handle_excel.get_cell(i, 4)
                send_value = self.handle_excel.get_cell(i, 5)
                # print(method, handle_value, send_value)
                if send_value == '':
                    if handle_value == '':
                        self.run_method(method)
                    else:
                        self.run_method(method, handle_value)
                else:
                    self.run_method(method, handle_value, send_value)

    def run_method(self, method, handle_value=None, send_value=None):
        """反射机制"""
        # print(method, handle_value, send_value)
        action_function = getattr(self.action_method, method)
        if send_value is None:
            if handle_value is None:
                action_function()
            else:
                action_function(handle_value)
        else:
            action_function(handle_value, send_value)
        
            
if __name__ == '__main__':
    test = TestCase()
    test.run_main()

github

点击前往GitHub

以上,希望可以帮到大家
关注、点赞、评论
star

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐