關鍵字框架補充2

1、增加清除結果

2、增加定位方式、定位路徑配置文件

3、增加ObjectMap工具類

4、修改測試腳本兼容測試數據定位方式和定位表達式為配置文件中的section、option名字;

LocateInfo.ini

[baidu]

SearchPage.SearchBox=id>kw

SearchPage.SearchButton=xpath>//*[@id="su"]

[sogou]

searchPage.SearchBox=id>query

searchPage.SearchButton=xpath>//*[@id="stb"]

ProjectVar.py

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

# Time? : 2019/3/3 22:12

# Author : huhongqiang

# File? : ProjectVar.py

import os

#工程根目錄

project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

#測試數據文件目錄

excel_file_path =? os.path.join(project_path,"TestData","126測試用例.xlsx")

#日志配置文件目錄

log_conf_file = os.path.join(project_path,"Config","Logger.conf")

#定位方式配置文件路徑

locate_info_config_file_path = os.path.join(project_path,"Config","LocateInfo.ini")

#瀏覽器驅動文件路徑

ie_driver_path = "d:\\IDServerDriver"

chrome_driver_path = "d:\\chromedriver"

firefox_driver_path = "d:\\geckodriver"

#測試用例列號字段

#這兩個是遍歷所有行時候用,因為sheet.rows()返回一個列表,索引從0開始

testcase_name? = 1

testcase_test_data_sheet_col = 2

testcase_is_execute_col = 3

#這兩個是寫結果用,因為用cell(row=no,column=no)都是從1開始

testcase_execute_time_col = 5

testcase_result_col = 6

#測試步驟列號字段

#遍歷所有行時候用,因為sheet.rows()返回一個列表,索引從0開始

teststep_stepname_col = 1

teststep_action_col = 2

teststep_locate_type_col = 3

teststep_locate_expression_col = 4

teststep_operate_value_col = 5

#寫結果用,因為用cell(row=no,column=no)索引都是從1開始

teststep_execute_time_col= 7

teststep_result_col = 8

teststep_error_screen_col = 9

teststep_exception_message_col = 10

if __name__ == "__main__":

? ? # print(project_path)

? ? # print(log_conf_file)

? ? # print(excel_file_path)

? ? print(locate_info_config_file_path)

GetConfigInfo.py

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

'''

根據配置Config.LocateInfo.ini的配置文件內容

獲取對應option的內容

'''

import? configparser

class GetConfigInfo(object):

? ? def __init__(self,config_path):

? ? ? ? self.cf = configparser.ConfigParser()

? ? ? ? self.cf.read(config_path)

? ? #獲取section下option的值

? ? def get_option_value(self,section_name,option_name):

? ? ? ? return self.cf.get(section_name,option_name)

? ? #獲取所有的section

? ? def get_all_section(self):

? ? ? ? return self.cf.sections()

? ? #獲取某section下的所有option

? ? def get_all_option(self,section_name):

? ? ? ? return? self.cf.options(section_name)

? ? #獲取section下所有option的鍵值對

? ? def get_section_items(self,section_name):

? ? ? ? items = self.cf.items(section_name)

? ? ? ? #把option的選項名和值作為鍵值對返回

? ? ? ? return dict(items)

if __name__ == "__main__":

? ? from Config.ProjectVar import *

? ? g = GetConfigInfo(locate_info_config_file_path)

? ? print(g.get_all_section())

? ? print(g.get_all_option("baidu"))

? ? print(g.get_option_value("baidu","SearchPage.SearchBox"))

? ? print(g.get_section_items("baidu"))

ObjectMap.py

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

'''.

此文件主要用于根據Config.LocateInfo.ini中的定位表達式

獲取頁面元素對象;

當excel測試數據文件中的定位方式、定位表達式列指定為Config.LocateInfo.ini

中section和option時候用于獲取頁面元素;

'''

from Util.GetConfigInfo import GetConfigInfo

from selenium.webdriver.support.ui import? WebDriverWait

from Config.ProjectVar import *

import traceback

class ObjectMap(object):

? ? def __init__(self):

? ? ? ? self.get_config_info = GetConfigInfo(locate_info_config_file_path)

? ? #根據配置文件中section和option獲取對應的定位方式和定位表達式

? ? #并獲取頁面元素

? ? #參數section_name,option_name對應excel表測試數據中的定位方式和定位表達式列的值

? ? #excel表測試數據中的定位方式和定位表達式列分別可寫真正的定位方式和定位表達式

? ? #也可以寫配置文件中對應的section和option

? ? def get_element(self,driver,section_name,option_name):

? ? ? ? try:

? ? ? ? ? ? #獲取section下option的值

? ? ? ? ? ? option_value = self.get_config_info.get_option_value(section_name,option_name).strip()

? ? ? ? ? ? #查分求出定位方式和定位表達式

? ? ? ? ? ? locate_type,locate_expression = option_value.split(">")

? ? ? ? ? ? # print(type(locate_type),locate_expression)

? ? ? ? ? ? #顯式等待獲取頁面元素

? ? ? ? ? ? element = WebDriverWait(driver,10,0.2).until(\

? ? ? ? ? ? ? ? lambda x : x.find_element(locate_type,locate_expression))

? ? ? ? ? ? return element

? ? ? ? except Exception as e:

? ? ? ? ? ? traceback.print_exc()

? ? ? ? ? ? raise e

? ? #獲取配置文件中section下option的值

? ? def get_locateType_locateExpression(self,section_name,option_name):

? ? ? ? # 獲取section下option的值

? ? ? ? option_value = self.get_config_info.get_option_value(section_name, option_name).strip()

? ? ? ? # 查分求出定位方式和定位表達式

? ? ? ? locate_type, locate_expression = option_value.split(">")

? ? ? ? return locate_type,locate_expression

if __name__ == "__main__":

? ? from selenium import webdriver

? ? import time

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

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

? ? # element = ObjectMap().get_element(driver,"baidu","SearchPage.SearchBox")

? ? # element.send_keys("yoursel")

? ? print(ObjectMap().get_locateType_locateExpression("baidu","SearchPage.SearchBox"))

? ? time.sleep(3)

? ? # print(element)

? ? driver.quit()

TestScrip.py

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

from Util.ParseExcel import ParseExcel

from Config.ProjectVar import *

import traceback

from Action.PageAction import *

from Util.Log import *

from Util.ClearTestResult import clear_test_result

from Util.ObjectMap import ObjectMap

"""

此腳本兼容測試數據中定位方式和定位表達式總中的值

1、定位方式和定位表達式

2、配置文件section名和option名

"""

def run():

? ? try:

? ? ? ? #首先清空測試數據中的測試結果列

? ? ? ? clear_test_result()

? ? ? ? #創(chuàng)建ParseExcel對象,用于處理excel數據

? ? ? ? pe = ParseExcel(excel_file_path)

? ? ? ? #切換到測試用例sheet

? ? ? ? pe.set_sheet_by_name("測試用例")

? ? ? ? #獲取測試用例sheet的所有行

? ? ? ? testCaseRows = pe.get_all_rows()

? ? ? ? #遍歷測試用例sheet的所有行,去除標題行

? ? ? ? for case_index,case_row in enumerate(testCaseRows[1:]):

? ? ? ? ? ? testCaseName = case_row[testcase_name].value

? ? ? ? ? ? testDataSheet = case_row[testcase_test_data_sheet_col].value

? ? ? ? ? ? isExecute = case_row[testcase_is_execute_col].value

? ? ? ? ? ? # print(testDataSheet,isExecute)

? ? ? ? ? ? #如果此行用例需要執(zhí)行,切換到對應的sheet

? ? ? ? ? ? if isExecute.lower() == "y":

? ? ? ? ? ? ? ? pe.set_sheet_by_name(testDataSheet)

? ? ? ? ? ? ? ? #獲取所有的測試步驟行

? ? ? ? ? ? ? ? testStepRows = pe.get_all_rows()

? ? ? ? ? ? ? ? #遍歷所有的步驟行,去除標題行

? ? ? ? ? ? ? ? #測試用例是否執(zhí)行成功標志

? ? ? ? ? ? ? ? sucess_flag = True

? ? ? ? ? ? ? ? for step_index,step_row in enumerate(testStepRows[1:]):

? ? ? ? ? ? ? ? ? ? #獲取每行的單元格值

? ? ? ? ? ? ? ? ? ? stepName = step_row[teststep_stepname_col].value

? ? ? ? ? ? ? ? ? ? action = step_row[teststep_action_col].value

? ? ? ? ? ? ? ? ? ? locteType = step_row[teststep_locate_type_col].value

? ? ? ? ? ? ? ? ? ? locateExpression = step_row[teststep_locate_expression_col].value

? ? ? ? ? ? ? ? ? ? operatorValue = step_row[teststep_operate_value_col].value

? ? ? ? ? ? ? ? ? ? # print("first",stepName,action,locteType,locateExpression,operatorValue)

? ? ? ? ? ? ? ? ? ? #判斷測試數據文件中定位表達式列是否為配置文件中的option名

? ? ? ? ? ? ? ? ? ? #如果是的話,需要從配置文件中讀取定位方式和定位表達式的值

? ? ? ? ? ? ? ? ? ? #需要判斷l(xiāng)ocateExpression不能為空

? ? ? ? ? ? ? ? ? ? if locateExpression and "Page." in locateExpression:

? ? ? ? ? ? ? ? ? ? ? ? #返回一個元組,此根據locteType、locateExpression獲取配置文件中的的

? ? ? ? ? ? ? ? ? ? ? ? #定位方式和定位表達式

? ? ? ? ? ? ? ? ? ? ? ? locteType,locateExpression= ObjectMap().get_locateType_locateExpression(\

? ? ? ? ? ? ? ? ? ? ? ? ? ? locteType,locateExpression)

? ? ? ? ? ? ? ? ? ? ? ? print("second",stepName,action,locteType,locateExpression,operatorValue)

? ? ? ? ? ? ? ? ? ? #拼接需要執(zhí)行的關鍵字命令

? ? ? ? ? ? ? ? ? ? if locteType is None and locateExpression is None and \

? ? ? ? ? ? ? ? ? ? ? ? ? ? operatorValue is not None:

? ? ? ? ? ? ? ? ? ? ? ? command = "%s('%s')" %(action,operatorValue)

? ? ? ? ? ? ? ? ? ? elif locteType is None and locateExpression is None and \

? ? ? ? ? ? ? ? ? ? ? ? ? ? operatorValue is? None:

? ? ? ? ? ? ? ? ? ? ? ? command = "%s()" %action

? ? ? ? ? ? ? ? ? ? elif locteType is not None and locateExpression is not None and \

? ? ? ? ? ? ? ? ? ? ? ? ? ? operatorValue is? None:

? ? ? ? ? ? ? ? ? ? ? ? command = "%s('%s','%s')" %(action,locteType,locateExpression)

? ? ? ? ? ? ? ? ? ? elif locteType is not None and locateExpression is not None and \

? ? ? ? ? ? ? ? ? ? ? ? ? ? operatorValue is not None:

? ? ? ? ? ? ? ? ? ? ? ? command = "%s('%s','%s','%s')" %(action,locteType,locateExpression,operatorValue)

? ? ? ? ? ? ? ? ? ? # print(command)

? ? ? ? ? ? ? ? ? ? try:

? ? ? ? ? ? ? ? ? ? ? ? #執(zhí)行關鍵字命令

? ? ? ? ? ? ? ? ? ? ? ? eval(command)

? ? ? ? ? ? ? ? ? ? except Exception as e:

? ? ? ? ? ? ? ? ? ? ? ? #打印異常信息

? ? ? ? ? ? ? ? ? ? ? ? traceback.print_exc()

? ? ? ? ? ? ? ? ? ? ? ? #執(zhí)行關鍵字命令發(fā)生異常,sucess_flag賦值False

? ? ? ? ? ? ? ? ? ? ? ? sucess_flag = False

? ? ? ? ? ? ? ? ? ? ? ? #寫異常日志

? ? ? ? ? ? ? ? ? ? ? ? error("測試步驟: [%s] 執(zhí)行失??!" %(stepName) + str(traceback.format_exc()))

? ? ? ? ? ? ? ? ? ? ? ? #寫失敗的測試結果,因為step_index從0開始,測試數據行從去除標題行的

? ? ? ? ? ? ? ? ? ? ? ? #第二行開始

? ? ? ? ? ? ? ? ? ? ? ? pe.write_cell_value(step_index + 2,teststep_result_col,"Failed")

? ? ? ? ? ? ? ? ? ? ? ? #寫執(zhí)行時間

? ? ? ? ? ? ? ? ? ? ? ? pe.write_cell_time(step_index + 2,teststep_execute_time_col)

? ? ? ? ? ? ? ? ? ? ? ? #發(fā)生異常截取當前屏幕,并返回截圖保存路徑

? ? ? ? ? ? ? ? ? ? ? ? pic_path = capture_screen()

? ? ? ? ? ? ? ? ? ? ? ? #寫截圖路徑

? ? ? ? ? ? ? ? ? ? ? ? pe.write_cell_value(step_index + 2,teststep_error_screen_col,pic_path)

? ? ? ? ? ? ? ? ? ? ? ? #寫異常信息到excel

? ? ? ? ? ? ? ? ? ? ? ? pe.write_cell_value(step_index + 2,teststep_exception_message_col,str(traceback.format_exc()))

? ? ? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? ? ? #寫日志

? ? ? ? ? ? ? ? ? ? ? ? info("測試步驟: [%s] 執(zhí)行成功!" %(stepName))

? ? ? ? ? ? ? ? ? ? ? ? #寫成功的測試結果,因為step_index從0開始,測試數據行從去除標題行的

? ? ? ? ? ? ? ? ? ? ? ? #第二行開始

? ? ? ? ? ? ? ? ? ? ? ? pe.write_cell_value(step_index + 2,teststep_result_col,"PASS")

? ? ? ? ? ? ? ? ? ? ? ? #寫執(zhí)行時間

? ? ? ? ? ? ? ? ? ? ? ? pe.write_cell_time(step_index + 2,teststep_execute_time_col)

? ? ? ? ? ? ? ? #切換到測試用例sheet

? ? ? ? ? ? ? ? pe.set_sheet_by_name("測試用例")

? ? ? ? ? ? ? ? #寫測試用例執(zhí)行結果

? ? ? ? ? ? ? ? if sucess_flag == True:

? ? ? ? ? ? ? ? ? ? pe.write_cell_value(case_index + 2,testcase_result_col,"PASS")

? ? ? ? ? ? ? ? ? ? info("測試用例: [%s] 執(zhí)行成功!" %testCaseName)

? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? pe.write_cell_value(case_index + 2, testcase_result_col, "Failed")

? ? ? ? ? ? ? ? ? ? error("測試用例: [%s] 執(zhí)行失敗!" % testCaseName)

? ? ? ? ? ? ? ? ##寫測試用例執(zhí)行時間

? ? ? ? ? ? ? ? pe.write_cell_time(case_index + 2,testcase_execute_time_col)

? ? except Exception as e:

? ? ? ? traceback.print_exc()

if __name__ == "__main__":

? ? run()

?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容