關鍵字框架補充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閱讀 228,646評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,595評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,560評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,035評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,814評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,224評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,301評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,444評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,988評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,804評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,998評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,544評論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,237評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,665評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,927評論 1 287
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,706評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,993評論 2 374

推薦閱讀更多精彩內容