1. 程式人生 > 實用技巧 >自動化測試程式碼閱讀-關鍵字驅動核心程式碼

自動化測試程式碼閱讀-關鍵字驅動核心程式碼

1.業務場景

閱讀關鍵字驅動自動化測試框架的核心程式碼。

2.完整程式碼

https://github.com/O-Aiden/AutomatedTesting/tree/master/KeyWordDriverTestFrameWork

3.核心程式碼

"""
------------------------------------
@Auth : Yuki
@File : storyTest.py
@IDE  : PyCharm
@Motto:
------------------------------------
"""
import traceback
import logging

from
KeyWordDriverTestFrameWork.action.PageAction import * from KeyWordDriverTestFrameWork.config.VarConfig import ( testCase_testIsExecute, testCase_testStepName, testStep_testNum, testStep_testStepDescribe, testStep_keyWord, testStep_elementBy, testStep_elementLocator, testStep_operateValue, testCase_testResult, excelPath )
from KeyWordDriverTestFrameWork.util.log import Logger from KeyWordDriverTestFrameWork.util.ParseExcel import ParseExcel log = Logger(__name__, CmdLevel=logging.INFO, FileLevel=logging.INFO) p = ParseExcel(excelPath) sheetName = p.wb.sheetnames # 獲取到excel的所有sheet名稱 def test_gdce_story(): try
: test_case_pass_num = 0 required_case = 0 print(sheetName) is_execute_column_values = p.get_column_value(sheetName[0], testCase_testIsExecute) print(is_execute_column_values) # print(columnValues) for index, value in enumerate(is_execute_column_values): print(index, value) # 獲取對應的步驟sheet名稱 step_sheet_name = p.get_cell_of_value(sheetName[0], index + 2, testCase_testStepName) print(step_sheet_name) if value.strip().lower() == 'y': required_case += 1 test_step_pass_num = 0 print('開始執行測試用例"{}"'.format(step_sheet_name)) log.logger.info('開始執行測試用例"{}"'.format(step_sheet_name)) # 如果用例被標記為執行y,切換到對應的sheet頁 # 獲取對應的sheet表中的總步驟數,關鍵字,定位方式,定位表示式,操作值 # 步驟總數 values = p.get_column_value(step_sheet_name, testStep_testNum) # 第一列資料 #print(values) step_num = len(values) for step in range(2, step_num + 2): raw_value = p.get_row_value(step_sheet_name, step) #print(raw_value) # 執行步驟名稱 step_name = raw_value[testStep_testStepDescribe - 2] # 關鍵字 key_word = raw_value[testStep_keyWord - 2] # 定位方式 by = raw_value[testStep_elementBy - 2] # 定位表示式 locator = raw_value[testStep_elementLocator - 2] # 操作值 operate_value = raw_value[testStep_operateValue - 2] if key_word and by and locator and operate_value: func = \ key_word \ + '(' + '"' + by + '"' + ',' + '"' + locator + '"' + ',' + '"' + operate_value + '"' + ')' elif key_word and by and locator and operate_value is None: func = \ key_word \ + '(' + '"' + by + '"' + ',' + '"' + locator + '"' + ')' elif key_word and operate_value and type(operate_value) == str and by is None and locator is None: func = \ key_word \ + '(' + '"' + operate_value + '"' + ')' elif key_word and operate_value and type(operate_value) == int and by is None and locator is None: func = \ key_word \ + '(' + str(operate_value) + ')' else: func = \ key_word \ + '(' + ')' try: # 執行測試步驟 #print(func) eval(func) except Exception: # 截圖 pic_path = save_screen_shot() # 寫回測試結果 error_info = traceback.format_exc() p.write_test_result(step_sheet_name, step, 'Failed', error_info, pic_path) print('步驟"{}"執行失敗'.format(step_name)) log.logger.info('步驟"{}"執行失敗'.format(step_name)) else: #print('步驟"{}"執行通過'.format(step_name)) log.logger.info('步驟"{}"執行通過'.format(step_name)) # 標記測試步驟為pass p.write_test_result(step_sheet_name, step, 'Pass') test_step_pass_num += 1 print('通過用例步數數:',test_step_pass_num) print('總步驟:',step_num) if test_step_pass_num == step_num: # 標記測試用例sheet頁的執行結果為pass p.write_cell(sheetName[0], index + 2, testCase_testResult, 'Pass') test_case_pass_num += 1 print('test_case_pass_num1: ',test_case_pass_num) else: p.write_cell(sheetName[0], index + 2, testCase_testResult, 'Failed') print('test_case_pass_num: ') print('共{}條用例,{}條需要被執行,本次執行通過{}條'.format(len(is_execute_column_values), required_case, test_case_pass_num)) log.logger.info('共{}條用例,{}條需要被執行,本次執行通過{}條'.format(len(is_execute_column_values), required_case, test_case_pass_num)) except Exception as e: log.logger.info(e) if __name__ == '__main__': test_gdce_story()
storyTest.py

4.思維導圖

5.參考:

https://www.cnblogs.com/linuxchao/p/linux-python-selenium-keywordFW.html