搭建測驗框架,框架目錄決議

config : 組態檔,將專案相關的配置全放到這個檔案夾中,python支持yaml,ini
ini檔案介紹
以[section]開始
以[option=value]結尾
備注以;開頭
section不可重名
yaml檔案介紹
以---開頭,表明檔案的開始
串列中的所有成員都開始于相同的縮進級別,并且使用一個“-”作為開頭(一個橫杠和一個空格)
一個字典是由一個簡單的鍵:值的形式(這個冒號后面必須是一個空格)
ini讀取檔案封裝,yaml讀取檔案封裝
configutil.py
import configparser
import os
import yaml
class ReadIni():
def read_ini(file, section, option):
conf = configparser.ConfigParser()
conf.read(file)
res=conf.get(section, option)
print(res)
return res
class ReafYaml():
def read_yaml(file,key):
f=open(file,encoding='utf-8')
file_data =f.read()
res=yaml.load(file_data,Loader=yaml.FullLoader)
print(res.get(key))
return res.get(key)
if __name__ == '__main__':
current_path = os.path.dirname(os.path.realpath(__file__))
config_path = os.path.dirname(current_path) + os.sep + "config"
yaml_file = os.path.join(config_path, 'test.yaml')
ReafYaml.read_yaml(yaml_file, 'username')
data : 資料檔案,將測驗用例引數化相關的檔案放在這里,xlsx,csv,json
driver :驅動檔案
log :日志檔案,如test log,error log
report :測驗報告
test :測驗檔案
case-測驗用例
test.py
import unittest
from selenium import webdriver
from test.locators import *
from utils.configutil import ReadIni,ReafYaml
from test.page import *
from utils.excelutil import *
from selenium.webdriver.common.action_chains import ActionChains
import yaml
import os
from utils.logutil import *
current_path = os.path.dirname(os.path.realpath(__file__))
config_path = os.path.dirname(current_path) + os.sep + "config"
ini_file = os.path.join(config_path, 'test.ini')
ip = ReadIni.read_ini(ini_file, 'ip_address', 'ip')
# print(ip)
url = '{}user/login?redirect=http%3A%2F%2Fmis-next.aunbox.ce%2FuserDetail'.format(ip)
excel_file = os.path.join(os.path.dirname(current_path) + os.sep + "data", 'case.xlsx')
username = ReadExcel.read_excel(excel_file,'Sheet1','A')
class LoginTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(10)
self.driver.get(url)
def test_login(self):
Logger('C:\\Users\\Administrator\\PycharmProjects\\yunding\\log\\test.log','info').info('add project')
for user in username:
loginpage=LoginPage(self.driver)
loginpage.enter_username(user)
loginpage.enter_password()
loginpage.click_login_button()
self.assertEqual('超級管理員',loginpage.get_login_name())
quitlogin=self.driver.find_element_by_xpath('//*[@id="root"]/section/section/header/div[2]/span')
ActionChains(self.driver).move_to_element(quitlogin).perform()
self.driver.find_element_by_class_name('ant-dropdown-menu-item').click()
self.driver.get(url)
# self.driver.find_element(*LoginLocators.username).send_keys("{}".format(user))
# self.driver.find_element(*LoginLocators.password).send_keys("{}".format(pad))
# self.driver.find_element(*LoginLocators.loginbutton).click()
# id=self.driver.find_element(*LoginLocators.loginname)
# self.assertEqual('超級管理員',id.text)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
common-測驗相關的抽象通用代碼
page-頁面類
元素定位
locators.py
from selenium.webdriver.common.by import By
# 頁面元素
class LoginLocators():
username=(By.ID,'account')
password=(By.ID,'password')
loginbutton=(By.CLASS_NAME,'ant-btn')
loginname=(By.CLASS_NAME,'userName___fQOhV')
元素操作
page.py
from test.locators import *
# 頁面元素的操作
class BasePage():
def __init__(self,driver):
self.driver = driver
class LoginPage(BasePage):
'''
用戶登錄頁面元素的操作,,,到這里消失
'''
UserName = (By.XPATH,'//*[@id="username"]') #登錄名
def enter_username(self,name):
ele = self.driver.find_element(*LoginLocators.username)
# ele.clear()
ele.send_keys(name) #對用戶名元素進行輸入
def enter_password(self):
ele = self.driver.find_element(*LoginLocators.password)
ele.send_keys('123456') #輸入密碼
def click_login_button(self):
ele = self.driver.find_element(*LoginLocators.loginbutton)
ele.click() #點擊登錄按鈕
def get_login_name(self):
ele = self.driver.find_element(*LoginLocators.loginname)
return ele.text #回傳登錄名
utils :公共方法
config的類
log的類
logutil.py
import logging
from logging import handlers
class Logger(object):
level_relations = {
'debug':logging.DEBUG,
'info':logging.INFO,
'warning':logging.WARNING,
'error':logging.ERROR,
'critical':logging.CRITICAL
}
def __init__(self,fp='d:\\Project_Redmine_01\\log\\test.log',level='info'):
self.level = self.level_relations.get(level)
self.logger = logging.getLogger(fp)
self.logger.setLevel(self.level)
formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
th = handlers.TimedRotatingFileHandler(fp)
th.setFormatter(formatter)
th.setLevel(self.level)
self.logger.addHandler(th)
def debug(self,msg):
self.logger.debug(msg)
def info(self,msg):
self.logger.info(msg)
def warning(self,msg):
self.logger.warning(msg)
def error(self,msg):
self.logger.error(msg)
def critical(self,msg):
self.logger.critical(msg)
if __name__ == '__main__':
log = Logger('abcd.log','debug')
log.info('this is info msg')
log.critical('this is critical msg')
讀,寫excel的類
excelutil.py
import configparser
import os
import yaml
class ReadIni():
def read_ini(file, section, option):
conf = configparser.ConfigParser()
conf.read(file)
res=conf.get(section, option)
print(res)
return res
class ReafYaml():
def read_yaml(file,key):
f=open(file,encoding='utf-8')
file_data =f.read()
res=yaml.load(file_data,Loader=yaml.FullLoader)
print(res.get(key))
return res.get(key)
if __name__ == '__main__':
current_path = os.path.dirname(os.path.realpath(__file__))
config_path = os.path.dirname(current_path) + os.sep + "config"
yaml_file = os.path.join(config_path, 'test.yaml')
ReafYaml.read_yaml(yaml_file, 'username')
生成報告的類
run.py
import os
import time
import unittest
import HTMLTestRunner
current_path = os.path.dirname(os.path.realpath(__file__))
# print(current_path)
report_path = os.path.join(current_path,'report')
# print(report_path)
case_path = os.path.join(current_path,'test')
# print(case_path)
report_name = time.strftime('%Y%m%d%H%M%S',time.localtime((time.time())))
testsuite = unittest.TestLoader().discover(case_path)
filename = "{}\\{}.html".format(report_path,report_name)
f = open(filename,'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=f,title='report',description='this is a report')
runner.run(testsuite)
f.close()
最后感謝每一個認真閱讀我文章的人,看著粉絲一路的上漲和關注,禮尚往來總是要有的,雖然不是什么很值錢的東西,如果你用得到的話可以直接拿走:
這些資料,對于【軟體測驗】的朋友來說應該是最全面最完整的備戰倉庫,這個倉庫也陪伴上萬個測驗工程師們走過最艱難的路程,希望也能幫助到你!
在我的QQ技術交流群里(技術交流和資源共享,廣告勿擾)
可以自助拿走,群號:175317069 群里的免費資料都是筆者十多年測驗生涯的精華,還有同行大神一起交流技術哦

如果對你有一點點幫助,各位的「點贊」就是小編創作的最大動力,我們下篇文章見!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/293116.html
標籤:其他
