1:先看看整个项目结构
2:commom(公共类封装)
2.1 路径处理封装
import os base_path=os.path.dirname(os.path.dirname(__file__)) #日志路径 logger_path=os.path.join(base_path,"out_put_log","System_of_Docter.log") #测试域名选择路径 switch_path=os.path.join(base_path,"config","url_config","switch.cfg") #线上域名路径 online_path=os.path.join(base_path,"config","url_config","online.cfg") #测试域名路径 test_path=os.path.join(base_path,"config","url_config","test.cfg")
2.2 配置文件读取封装
from configparser import ConfigParser from common.all_of_path import switch_path class Config: def __init__(self,file_path): self.conf=ConfigParser() self.conf.read(filenames=file_path,encoding="utf-8") def get_boolen_value(self,section,option): return self.conf.getboolean(section,option) def get_string_value(self,section,option): return self.conf.get(section,option) if __name__ == '__main__': c=Config(switch_path) print(c.get_boolen_value("switch","switch"))
2.3 日志处理封装
import logging from common.all_of_path import logger_path class Logger: def __init__(self,name="System_of_Docter"): self.logger = logging.getLogger(name) self.logger.setLevel(logging.DEBUG) #添加文件日志和控制台日志 self.filehandler=logging.FileHandler(filename=logger_path,mode="w+",encoding="utf-8") self.consolehandler=logging.StreamHandler() #为文件日志和控制台日志添加日志级别 self.filehandler.setLevel("INFO") self.consolehandler.setLevel("DEBUG") #文件日志处理器和控制台日志处理器添加日志输出格式 formatt = logging.Formatter("%(asctime)s____%(name)s___%(levelname)s___%(message)s")#___[%(filename)s:%(lineno)d] self.filehandler.setFormatter(formatt) self.consolehandler.setFormatter(formatt) self.logger.addHandler(self.filehandler) self.logger.addHandler(self.consolehandler) self.filehandler.close() self.consolehandler.close() def get_logger(self): return self.logger def remove_handle(self): self.logger.removeHandler(self.consolehandler) self.logger.removeHandler(self.filehandler) if __name__ == '__main__': l=Logger() t=l.get_logger() t.info("我是info1信息") t.info("我是info2信息") # t.exception("超时异常") # l.remove_handle()
2.4 basepage(重用元素操作封装)
from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from common.logger import Logger from selenium.webdriver.support.select import Select import win32clipboard as w import win32con import win32api import time import os from PIL import Image import math import operator from functools import reduce from test_data.add_new_patient_data.clinical_setings_photo.get_screenshot_path import ele_screenshot_path logger=Logger().get_logger() class BasePage: def __init__(self,driver): self.driver=driver def wait_presence_element(self,loc,timeout=30,frequency=1): """等待元素被加载到dom树并不一定可见""" logger.info("正在准备等待元素{}被加载到dom树中".format(loc)) try: WebDriverWait(self.driver,timeout,frequency).until(EC.presence_of_element_located(loc)) logger.info("{}元素被成功加载到dom树".format(loc)) except: logger.exception("{}元素不存在dom树中") def wait_element_visible(self,loc,timeout=30,frequency=1): """等待元素可见""" logger.info("正在准备等待{}元素可见".format(loc)) try: WebDriverWait(self.driver,timeout,frequency).until(EC.visibility_of_element_located(loc)) logger.info("{}元素可见成功".format(loc)) except: logger.exception("等待{}元素可见失败".format(loc)) def find_element(self,loc): logger.info("正在准备查找{}元素".format(loc)) try: web_object=self.driver.find_element(*loc) logger.info("{}元素查找成功".format(loc)) return web_object except: logger.exception("{}元素查找失败") def find_elements_back_length(self,loc): logger.info("正在准备查找一些{}元素".format(loc)) try: ele_list=self.driver.find_elements(*loc) logger.info("一些{}元素查找成功".format(loc)) return len(ele_list) except: logger.exception("一些{}元素查找失败".format()) def find_elements(self,loc,index): logger.info("正在准备查找一些{}元素".format(loc)) try: web_eles=self.driver.find_elements(*loc) logger.info("一些{}元素查找成功".format(loc)) return web_eles[index] except: logger.exception("一些{}元素查找失败".format(loc)) def find_elements_back_all(self,loc): logger.info("正在准备查找一些{}元素".format(loc)) try: web_eles=self.driver.find_elements(*loc) logger.info("一些{}元素查找成功".format(loc)) return web_eles except: logger.exception("一些{}元素查找失败".format(loc)) def clear_text(self,loc): logger.info("正在准备清除{}元素输入框内容".format(loc)) try: self.find_element(loc).clear() logger.info("{}元素输入框内容清除成功".format(loc)) except: logger.exception("{}元素输入框内容清除失败".format(loc)) def send_value(self,loc,value): logger.info("正在准备在{}元素输入框输入内容".format(loc,value)) try: self.find_element(loc).send_keys(value) logger.info("{}元素输入框输入{}内容成功".format(loc,value)) except: logger.exception("{}元素输入框输入{}内容失败".format(loc,value)) def click_element(self,loc): logger.info("正在准备点击{}元素".format(loc)) try: self.find_element(loc).click() logger.info("{}元素点击成功".format(loc)) except: logger.exception("{}元素点击失败".format(loc)) def click_element_by_ele(self, ele): logger.info("正在准备点击{}对象".format(ele)) try: ele.click() logger.info("{}对象点击成功".format(ele)) except: logger.exception("{}对象点击失败".format(ele)) def click_elements(self,loc,index): logger.info("正在准备点击{}元素下标为{}的元素".format(loc,index)) try: self.find_elements(loc,index).click() logger.info("{}元素下标为{}的元素点击成功".format(loc,index)) except: logger.exception("{}元素下标为{}元素点击失败".format(loc,index)) def get_element_text(self,loc): logger.info("正在准备获取{}元素的文本值".format(loc)) try: text_value=self.find_element(loc).text logger.info("获取{}元素文本值成功".format(loc)) return text_value except: logger.exception("获取{}元素文本值失败".format(loc)) def is_selected(self,loc): logger.info("正在准备判断{}元素是否可被选中".format(loc)) try: is_selected_value=self.find_element(loc).is_selected() if is_selected_value: logger.info("{}元素已经被选中".format(loc)) return is_selected_value else: logger.info("{}元素没有被选中".format(loc)) return is_selected_value except: logger.exception("判断{}元素是否被选中失败".format(loc)) def is_selected_by_ele(self, ele): logger.info("正在准备判断{}对象是否可被选中".format(ele)) try: is_selected_value = ele.is_selected() if is_selected_value: logger.info("{}对象已经被选中".format(ele)) return is_selected_value else: logger.info("{}对象没有被选中".format(ele)) return is_selected_value except: logger.exception("判断{}对象是否被选中失败".format(ele)) def scroll_mouse(self,loc): logger.info("正在准备滚动屏幕到目标元素{}".format(loc)) try: self.driver.execute_script("arguments[0].scrollIntoView();",self.find_element(loc)) logger.info("滚动到目标元素{}成功".format(loc)) except: logger.exception("滚动到目标元素{}失败".format(loc)) def select_down_element(self,loc,select_value): logger.info("正在准备选中下拉列表的value值为{}的元素".format(select_value)) try: select=Select(self.find_element(loc)) select.select_by_value(select_value) logger.info("下拉列表value值为{}的元素被选中".format(select_value)) return select.first_selected_option.text except: logger.exception("下拉列表value值为{}的元素选中失败".format()) return select.first_selected_option.text def cutting(self,file_path): logger.info("正在准备剪切本地路径为:{}的图片".format(file_path)) try: w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_UNICODETEXT,file_path) w.CloseClipboard() time.sleep(2) logger.info("剪切本地路径{}图片成功".format(file_path)) except: logger.exception("本地路径为:{}的图片上传失败".format(file_path)) def upload_image(self): logger.info("正在准备打开图片") try: VK_CODE = { "ctrl": 0x11, "v": 0x56, "enter": 0x0D, } win32api.keybd_event(VK_CODE["ctrl"],0,0,0) win32api.keybd_event(VK_CODE["v"], 0, 0, 0) win32api.keybd_event(VK_CODE["ctrl"], 0, win32con.KEYEVENTF_KEYUP, 0) win32api.keybd_event(VK_CODE["v"], 0, win32con.KEYEVENTF_KEYUP, 0) win32api.keybd_event(VK_CODE["enter"], 0, 0, 0) win32api.keybd_event(VK_CODE["enter"], 0, win32con.KEYEVENTF_KEYUP, 0) time.sleep(2) logger.info("图片打开成功") except: logger.exception("图片打开失败") def alter_is_present(self,timeout=30,frequency=1): logger.info("正在准备判断alter是否出现") try: WebDriverWait(self.driver,timeout,frequency).until(EC.alert_is_present()) logger.info("alter 弹框已经出现") except: logger.exception("alter 弹框没有出现") def deal_alter(self,value="1"):#driver.switch_to.alert.accept() logger.info("正在准备处理alter弹框") try: if value=="1": text=self.driver.switch_to.alert.text self.driver.switch_to.alert.accept() logger.info("alter接受成功") return text else: self.driver.switch_to.alert.dismiss() logger.info("alter 取消接收") except: logger.exception("alter弹框处理失败") def refresh(self): logger.info("正在准备刷新页面") try: self.driver.refresh() logger.info("页面刷新成功") except: logger.exception("页面刷新失败") def get_ele_sreenshot(self,loc,file_path,filename): logger.info("正在准备对{}元素对象进行截屏操作") try: self.find_element(loc).screenshot(os.path.join(file_path,filename)) logger.info("{}元素对象截屏成功,并命名为{}".format(loc,filename)) except: logger.exception("{}元素对象截屏失败".format(loc)) def get_ele_sreenshot_by_ele(self,ele,file_path,filename): logger.info("正在准备对{}元素对象进行截屏操作") try: ele.screenshot(os.path.join(file_path,filename)) logger.info("{}元素对象截屏成功,并命名为{}".format(ele,filename)) except: logger.exception("{}元素对象截屏失败".format(ele)) def query_photo(self,filename_one,filename_other): logger.info("正在准备判断这张图片{}和这张图片{}是否一样".format(filename_one,filename_other)) try: image1=Image.open(os.path.join(ele_screenshot_path,filename_one)) image2=Image.open(os.path.join(ele_screenshot_path,filename_other)) histogram1=image1.histogram() histogram2=image2.histogram() result= math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1)) if result ==0.0: logger.info("两张图片一样") return True else: logger.info("两张图片不一样") return False except: logger.exception("判断两张图片是否一样失败")