IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python excel框架搭建 -> 正文阅读

[Python知识库]python excel框架搭建

测试框架.py

import re
from Config.ProjVar import *
from Action.WebElementAction import *
from Util.ObjectMap import *

from Action.ResetTestCaseFileResult import *

def read_test_data_from_excel(test_data_file_path, test_data_sheet_name):
try:
test_data_wb = ParseExcel(test_data_file_path)
test_data_wb.set_sheet_by_name(test_data_sheet_name)
data = test_data_wb.get_rows_values(2)
return data

except Exception as e:
    traceback.print_exc()
    return None

def execute_test_cases(test_data_file_path):
clear_all_executed_info(test_data_file_path)
test_data_wb = ParseExcel(test_data_file_path)
test_data_wb.set_sheet_by_name(test_case_sheet)
execute_status_col_cells = test_data_wb.get_col(test_case_is_executed_col_no)
driven_style_col_cells = test_data_wb.get_col(7)
test_data_sheet_col_cells = test_data_wb.get_col(8)
success_step_num = 0
for id, i in enumerate(range(1, len(execute_status_col_cells))):
print(id + 2, execute_status_col_cells[i].value)
if execute_status_col_cells[i].value.lower() == “y”:
driven_style = driven_style_col_cells[id + 1].value
test_data_sheet = test_data_sheet_col_cells[id + 1].value
print(driven_style, test_data_sheet)
if driven_style == “key”:
test_step_sheet = test_data_wb.get_cell_value(id + 2, 3)
print(test_step_sheet)
test_data_wb.set_sheet_by_name(test_step_sheet)
max_step_row_no = test_data_wb.get_max_row()
for i in range(2, max_step_row_no + 1):
step_row = test_data_wb.get_row(i)
action = step_row[test_step_action_col_no - 1].value
locate_method = step_row[test_step_locate_type_col_no - 1].value
locate_expression = step_row[test_step_locate_expression_col_no - 1].value
if action: action = action.strip()
if locate_method: locate_method = locate_method.strip()
if locate_expression: locate_expression = locate_expression.strip()
if locate_expression is not None and “Page.” in locate_expression:
locate_method, locate_expression = ObjectMap(
object_map_file_path).get_locatemethod_and_locateexpression(
locate_method, locate_expression
)
value = step_row[test_step_value_col_no - 1].value
print(action, locate_method, locate_expression
, value)
# eval(“open_browser(‘ie’)”)
if action is not None and locate_method is None
and locate_expression is None and value is not None:
command = “%s(’%s’)” % (action, value)
elif action is not None and locate_method is not None
and locate_expression is not None and value is not None:
command = “%s(’%s’,’%s’,’%s’)” % (action, locate_method,
locate_expression, value)
elif action is not None and locate_method is not None
and locate_expression is not None and value is None:
command = “%s(’%s’,’%s’)” % (action, locate_method,
locate_expression)
elif action is not None and locate_method is None
and locate_expression is None and value is None:
command = “%s()” % (action)
print(command)
try:
return_value = eval(command)
test_data_wb.write_cell(i, test_step_executed_result_col_no, “pass”)
success_step_num += 1
if “capture_pic” in command:
test_data_wb.write_cell(
i, test_step_executed_capture_pic_path_col_no, return_value)
except AssertionError as e:
info(command + “\n” + “断言失败:\n” + traceback.format_exc())
test_data_wb.write_cell(i, test_step_executed_result_col_no, “fail”)
pic_path = capture_pic()
test_data_wb.write_cell(
i, test_step_executed_capture_pic_path_col_no, pic_path)
test_data_wb.write_cell(
i, test_step_executed_exception_info_col_no, traceback.format_exc())

                except Exception as e:
                    capture_pic()
                    info(command + "\n" + e + "\n" + traceback.format_exc())
                    test_data_wb.write_cell(i, test_step_executed_result_col_no, "fail")
                    test_data_wb.write_cell(
                            i, test_step_executed_capture_pic_path_col_no, pic_path)
                    test_data_wb.write_cell(
                            i, test_step_executed_exception_info_col_no, traceback.format_exc())
                test_data_wb.write_cell(i, test_step_executed_time_col_no, get_current_datetime())

            if success_step_num == max_step_row_no - 1:
                test_data_wb.set_sheet_by_name(test_case_sheet)
                test_data_wb.write_cell(id + 2, test_case_executed_result_col_no, "pass")
            else:
                test_data_wb.set_sheet_by_name(test_case_sheet)
                test_data_wb.write_cell(id + 2, test_case_executed_result_col_no, "fail")
        elif driven_style == "mix":
            test_step_sheet = test_data_wb.get_cell_value(id + 2, 3)
            print(test_step_sheet)
            test_data_wb.set_sheet_by_name(test_step_sheet)
            max_step_row_no = test_data_wb.get_max_row()
            print("********", max_step_row_no)
            print(read_test_data_from_excel(
                    test_data_file_path, test_data_sheet))
            for id, line in enumerate(read_test_data_from_excel(
                    test_data_file_path, test_data_sheet)):
                print("------line:", line)
                flag = True
                for i in range(2, max_step_row_no + 1):
                    step_row = test_data_wb.get_row(i)
                    action = step_row[test_step_action_col_no - 1].value
                    locate_method = step_row[test_step_locate_type_col_no - 1].value
                    locate_expression = step_row[test_step_locate_expression_col_no - 1].value
                    if action: action = action.strip()
                    if locate_method: locate_method = locate_method.strip()
                    if locate_expression: locate_expression = locate_expression.strip()
                    if locate_expression is not None and "Page." in locate_expression:
                        locate_method, locate_expression = ObjectMap(
                                object_map_file_path).get_locatemethod_and_locateexpression(
                                locate_method, locate_expression
                        )
                    value = step_row[test_step_value_col_no - 1].value
                    if re.search(r"\$\{\d+\}", str(value)):
                        data_col_no = re.search(r"\$\{(\d+)\}", value).group(1)
                        value = line[int(data_col_no) - 1]
                        print("^^^^^^^value:", value)
                    print(action, locate_method, locate_expression
                          , value)
                    # eval("open_browser('ie')")
                    if action is not None and locate_method is None \
                            and locate_expression is None and value is not None:
                        command = "%s('%s')" % (action, value)
                    elif action is not None and locate_method is not None \
                            and locate_expression is not None and value is not None:
                        command = "%s('%s','%s','%s')" % (action, locate_method,
                                                          locate_expression, value)
                    elif action is not None and locate_method is not None \
                            and locate_expression is not None and value is None:
                        command = "%s('%s','%s')" % (action, locate_method,
                                                     locate_expression)
                    elif action is not None and locate_method is None \
                            and locate_expression is None and value is None:
                        command = "%s()" % (action)
                    print(command)
                    try:
                        return_value = eval(command)
                        success_step_num += 1
                        if "capture_pic" in command:
                            test_data_wb.write_cell(
                                    i, test_step_executed_capture_pic_path_col_no, return_value)
                    except AssertionError as e:
                        flag = False
                        info(command + "\n" + "断言失败:\n" + traceback.format_exc())
                        temp_sheet_name = test_data_wb.sheet.title
                        test_data_wb.set_sheet_by_name(test_data_sheet)
                        test_data_wb.write_cell(id + 2, 4, "断言失败")
                        pic_path = capture_pic()
                        test_data_wb.write_cell(
                                id + 2, 6, pic_path)
                        test_data_wb.write_cell(
                                id + 2, 5, traceback.format_exc())
                        test_data_wb.set_sheet_by_name(temp_sheet_name)

                    except Exception as e:
                        flag = False
                        capture_pic()
                        info(command + "\n" + str(e) + "\n" + traceback.format_exc())
                        temp_sheet_name = test_data_wb.sheet.title
                        test_data_wb.set_sheet_by_name(test_data_sheet)
                        test_data_wb.write_cell(id + 2, 4, "失败")
                        pic_path = capture_pic()
                        test_data_wb.write_cell(
                                id + 2, 6, pic_path)
                        test_data_wb.write_cell(
                                id + 2, 5, traceback.format_exc())
                        test_data_wb.set_sheet_by_name(temp_sheet_name)
                    test_data_wb.write_cell(i, test_step_executed_time_col_no, get_current_datetime())
                if flag is True:
                    temp_sheet_name = test_data_wb.sheet.title
                    test_data_wb.set_sheet_by_name(test_data_sheet)
                    test_data_wb.write_cell(id + 2, 4, "成功")
                    test_data_wb.set_sheet_by_name(temp_sheet_name)

        test_data_wb.set_sheet_by_name("测试用例")
        test_data_wb.write_cell(id + 2, test_case_executed_time_col_no, get_current_datetime())

if name == “main”:
# print(execute_tequst_case(test_data_file+"||搜狗"))
data = read_test_data_from_excel(test_data_file, “搜狗测试数据”)
execute_test_cases(test_data_file)

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-24 11:23:48  更:2021-07-24 11:25:11 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/6 22:19:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码