关键字框架补充1

ConfigParser.py:解析ini文件

# -*-coding:utf-8 -*-

import configparser

import traceback

class ConfigParser(object):

    """解析ini文件的类"""

    def __init__(self, config_file_path):

        self.config_file_path = config_file_path

        # 实例化ConfigParser

        self.cf = configparser.ConfigParser()

        # 读取指定的ini文件

        self.cf.read(self.config_file_path,encoding="utf-8-sig")

    def get_all_sections(self):

        """获取所有的section,也就是用[]中的内容"""

        return self.cf.sections()

    def get_option(self, section_name, option_name):

        """获取所有的section,也就是用[]中的内容"""

        try:

            value = self.cf.get(section_name, option_name)

            return value

        except configparser.NoSectionError:

            traceback.print_exc()

            # raise configparser.NoSectionError("section或action不存在!")

        except Exception:

            traceback.print_exc()

            # raise Exception("get_option失败!")

    def get_all_option_items(self, section_name):

        """获取section下所有的option,以字典的形式返回"""

        try:

            items = self.cf.items(section_name)

            return dict(items)

        except configparser.NoSectionError:

            traceback.print_exc()

            # raise configparser.NoSectionError("section不存在!")

        except Exception:

            traceback.print_exc()

            # raise Exception("get_all_option_items失败!")

if __name__ == "__main__":

    from Config.ProjVar import object_map_file_path

    cf = ConfigParser(object_map_file_path)

    print(cf.get_all_sections())

    print(cf.get_option('baidu', "SearchPage.InputBox"))

    # print(cf.get_option('bing', "SearchPage.InputBox"))

    print(cf.get_all_option_items('baidu'))

ObjectMap.py:映射类,通过定位方式和定位表达式找到对应的元素

# -*-coding:utf-8 -*-

from Utils.ConfigParser import ConfigParser

from Utils.WaitUtil import WaitUtil

class ObjectMap(object):

    """对象映射类"""

    def __init__(self, config_file_path):

        self.cf = ConfigParser(config_file_path)

    def get_locate_method_and_locate_exp(self, section_name, option_name):

        """获取定位方法和定位表达式"""

        locators = self.cf.get_option(section_name, option_name).split(">")

        return locators

    def get_element_object(self, driver, section_name, option_name):

        """由ini文件中指定的section_name和option_name,得到webElement对象"""

        try:

            # 获取定位方法和定位表达式

            locators = self.cf.get_option(section_name, option_name).split(">")

            # 根据定位方法和定位表达式,通过显示等待得到页面元素对象

            element = WaitUtil(driver).presenceOfElement(locators[0], locators[1])

        except Exception as e:

            raise e

        else:

            # 返回页面元素对象

            return element

if __name__ == '__main__':

    from selenium import webdriver

    from Config.ProjVar import object_map_file_path

    driver = webdriver.Chrome(executable_path="f:\\chromedriver")

    driver.get("http://www.baidu.com")

    objmap = ObjectMap(object_map_file_path)

    print(objmap.get_element_object(driver, "baidu", "SearchPage.InputBox"))

    print(objmap.get_element_object(driver, "baidu", "SearchPage.SubmitButton"))

capature.py:截图

# -*-coding:utf-8 -*-

import traceback

from PIL import ImageGrab

from Utils.Log import *

from Utils.Dir import *

def capture_all_screen(picture_file_name):

    """截取整个电脑屏幕"""

    try:

        # 创建当前日期目录

        current_date = make_date_dir(picture_path)

        # 拼接完整的图片名称,格式为:picture_path\\current_date\\picture_file_name

        all_pic_file_path = os.path.join(picture_path, current_date, picture_file_name)

        # 保存图片

        im = ImageGrab.grab()

        im.save(all_pic_file_path, "png")

        return all_pic_file_path

    except Exception as e:

        traceback.print_exc()

        info("截屏失败,错误信息为%s" % traceback.format_exc())

        raise Exception("截屏失败!")

def capture_browser_screen(driver, picture_file_name):

    """截取浏览器屏幕"""

    try:

        # 创建当前日期目录

        current_date = make_date_dir(picture_path)

        # 拼接完整的图片名称,格式为:picture_path\\current_date\\picture_file_name

        all_pic_file_path = os.path.join(picture_path, current_date, picture_file_name)

        # 保存图片

        driver.get_screenshot_as_file(all_pic_file_path)

        return all_pic_file_path

    except Exception as e:

        traceback.print_exc()

        info("截屏失败,错误信息为%s" % traceback.format_exc())

        raise Exception("截屏失败!")

if __name__ == "__main__":

    from selenium import webdriver

    driver = webdriver.Chrome(executable_path="f:\\chromedriver")

    driver.get("http://www.baidu.com")

    capture_browser_screen(driver, 'baidu.png')

    capture_all_screen('screen.png')

    driver.quit()

TestCaseFileParser.py:重新封装了清除测试结果的类

# -*-coding:utf-8 -*-

from Utils.Excel import *

from Config.ProjVar import *

class TestCaseFileParser(object):

    """测试文件解析类"""

    def __init__(self, test_data_file, test_sheet_name):

        # 根据指定的文件名获取excel对象

        self.test_data_wb = ParseExcel(test_data_file)

        self.test_data_wb.set_sheet_by_name(test_sheet_name)

    def get_excel_file_obj(self):

        return self.test_data_wb

    def get_execute_sheet_names(self):

        """从测试用例sheet中获取所有要执行的sheet名称,以字典形式返回"""

        # 要执行的sheet名称为key,在测试用例sheet中行号为value,方便后续写测试结果使用

        execute_sheet_names_dict = {}

        # 在测试用例工作表中找到"是否执行"这一整列

        is_execute_col = self.test_data_wb.get_col(test_case_is_executed_col_no)

        # 忽略表头所在的行,所以行索引从2开始

        # 将值为y的单元格所对应的测试步骤工作表名取出来,添加到execute_sheet_names_list中

        for row_no, cell in enumerate(is_execute_col[1:], start=2):

            if cell.value and cell.value.strip().lower() == "y":

                # print(row_no, cell,cell.value)

                execute_sheet_names_dict[self.test_data_wb.get_cell_value(

                    row_no, test_case_test_step_sheet_name_col_no)] = row_no

        # 将execute_sheet_names_dict返回

        return execute_sheet_names_dict

    def clear_test_case_result(self):

        """清除测试用例sheet中测试结果和执行时间单元格内容"""

        try:

            # 获取所有要执行的测试sheet名称,以及在测试用例sheet中的行号

            execute_sheet_names_dict = self.get_execute_sheet_names()

            # print(execute_sheet_names_dict)

            for sheet_name, row_no in execute_sheet_names_dict.items():

                self.test_data_wb.write_cell(row_no=row_no, col_no=test_case_executed_time_col_no, value="")

                self.test_data_wb.write_cell(row_no=row_no, col_no=test_case_executed_result_col_no, value="")

        except:

            traceback.format_exc()

            return False

        else:

            return True

    def clear_test_step_result(self):

        """清除测试步骤sheet中,测试结果、执行时间,异常信息和错误截图单元格内容"""

        try:

            # 保存切换前的sheet名称

            default_sheet_name = self.test_data_wb.get_current_sheet_name()

            # 获取所有要执行的测试sheet名称,以及在测试用例sheet中的行号

            execute_sheet_names_dict = self.get_execute_sheet_names()

            for sheet_name, row_no in execute_sheet_names_dict.items():

                # 切换sheet

                self.test_data_wb.set_sheet_by_name(sheet_name)

                # 遍历所有的行,忽略表头所在的行,所以索引从2开始

                for line in range(2, self.test_data_wb.get_max_row()+1):

                    # 清除"执行时间"内容

                    self.test_data_wb.write_cell(row_no=line, col_no=test_step_executed_time_col_no, value="")

                    # 清除"测试结果"内容

                    self.test_data_wb.write_cell(row_no=line, col_no=test_step_executed_result_col_no, value="")

                    # 清除"异常信息"内容

                    self.test_data_wb.write_cell(row_no=line, col_no=test_step_executed_exception_info_col_no, value="")

                    # 清除"截图位置"内容

                    self.test_data_wb.write_cell(row_no=line, col_no=test_step_executed_capture_pic_path_col_no, value="")

        except:

            traceback.format_exc()

            return False

        else:

            return True

        finally:

            # 切换回初始的sheet

            self.test_data_wb.set_sheet_by_name(default_sheet_name)

    def clear_all_executed_info(self):

        """清除测试用例和测试步骤中执行过的信息"""

        result1 = self.clear_test_case_result()

        result2 = self.clear_test_step_result()

        if result1 and result2:

            return True

        else:

            return False

if __name__ == "__main__":

    fp = TestCaseFileParser(test_file_path, test_case_sheet_name)

    print(fp.get_execute_sheet_names())

    print(fp.clear_all_executed_info())

    print(fp.get_execute_sheet_names())

    # print(fp.clear_test_case_result())

    # print(fp.clear_test_step_result())

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容