目标:如何封装自己的日志类和浏览器引擎类。
相关步骤:
1.打开PyCharm,创建如下格式的项目层级结构,文件名称和我保持一样;
配置文件config.ini:
# this is config file, only store browser type and server URL [browserType] #browserName = Firefox browserName = Chrome #browserName = IE [testServer] URL = https://www.baidu.com #URL = www.google.com
日志类logger.py代码:
import logging import os.path import time class Logger(object): def __init__(self,logger): """ 指定保存日志的文件路径,日志级别,以及调用文件 将日志存入到指定的文件中 :param logger: """ # 创建一个logger self.logger = logging.getLogger(logger) self.logger.setLevel(logging.DEBUG) # 创建一个handler,用于写入日志文件 rq = time.strftime('%Y%m%d%H%M',time.localtime(time.time())) # log_path = os.path.dirname(os.getcwd()) + '/logs/' # 项目根目录下的logs,保存日志 log_path = os.path.dirname(os.path.abspath('.')) + '/logs/' # 如果case组织结构式 /testsuit/featuremodel/xxx.py , 那么得到的相对路径的父路径就是项目根目录 log_name = log_path + rq + '.log' fh = logging.FileHandler(log_name) fh.setLevel(logging.INFO) # 再创建一个handle,用于输出到控制台 ch = logging.StreamHandler() ch.setLevel(logging.INFO) # 定义handle的输出格式 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) # 给logger添加handler self.logger.addHandler(fh) self.logger.addHandler(ch) def getlog(self): return self.logger
浏览器引擎类browser_engine.py代码:
import configparser import os.path from selenium import webdriver from framework.logger import Logger logger = Logger(logger="BeowsweEngine").getlog() class BrowserEngine(object): dir = os.path.dirname(os.path.abspath('.')) # 注意相对路径获取方法 chrome_driver_path = dir + '/tools/chromedriver.exe' ie_driver_path = dir + '/tools/IEDriverServer.exe' firefox_driver_path = dir + '/tools/geckodriver.exe' def __init__(self,driver): self.driver =driver def open_browser(self,driver): config = configparser.ConfigParser() file_path = os.path.dirname(os.path.abspath('.')) + '/config/config.ini' config.read(file_path) # config.read(file_path,encoding='UTF-8'), 如果代码有中文注释,用这个,不然报解码错误 browser = config.get("browserType","browserName") logger.info("You had select %s borwser."%browser) url = config.get("testServer","URL") logger.info("The test server url is : %s"%url) if browser == "Firefox": driver == webdriver.Firefox(self.firefox_driver_path) logger.info("Starting firefox browser.") elif browser == "Chrome": driver = webdriver.Chrome(self.chrome_driver_path) logger.info("Starting Chrome browser.") elif browser == "IE": driver = webdriver.Ie(self.ie_driver_path) logger.info("Starting IE browser.") driver.get(url) logger.info("Open url: %s "%url) driver.maximize_window() logger.info("Maximize the current window.") driver.implicitly_wait(10) logger.info("Set implicitly wait 10 seconds") return driver def quit_browser(self): logger.info("Now,Close and quit the browser.") self.driver.quit()
测试脚本baidu_search.py代码:
import time import unittest from framework.browser_engine import BrowserEngine class BaiduSearch(unittest.TestCase): def setUp(self): """ 测试固件的setUP()的代码,主要是测试的前提准备工作 :return: """ browse = BrowserEngine(self) self.driver = browse.open_browser(self) def tearDown(self): """ 测试结束后的操作,这里基本上都是关闭浏览器 :return: """ self.driver.quit() def test_baidu_search(self): """ 这里一定要test开头,把测试逻辑代码封装到一个test开头的方法里 :return: """ self.driver.find_element_by_id('kw').send_keys('selemiun') time.sleep(1) try: assert 'selenium' in self.driver.title print('Test Pass.') except Exception as e: print('Test Fail.',format(e)) if __name__ == '__main__': unittest.main()
工具包,这里把三个浏览器的driver文件都放根目录一个文件夹里,这样别人拷贝这个项目也不需要去下载这些文件。
运行结果:
参考文章:https://blog.csdn.net/u011541946/article/details/70243441