關鍵字框架補充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())

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容