框架采用python3 + selenium3 + PO + yaml + ddt + unittest等技术编写成基础测试框架,能适应日常测试工作需要。
本篇主要介绍基类方法封装,包含 打开页面,定位元素,多表单嵌套切换,多窗口切换等方法
# -*- coding = UTF-8 -*- # Autohr : 叶松桥 # File : base.py # project : Caps_UI_Test # time : 2020/11/27 18:39 # Describe : 基础方法 # --------------------------------------- import os,sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait import logging.config from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchFrameException,NoSuchWindowException,NoAlertPresentException,NoSuchElementException import time CON_LOG = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+'/config/log.conf' logging.config.fileConfig(CON_LOG) logging = logging.getLogger() class BaseView(object): def __init__(self, driver): self.driver = driver self.base_url = 'http://mcenter.uat.mall' self.timeout = 11 def _open(self,url): """ 打开浏览器并访问URL地址 :param url: url地址 :return: """ url_ = self.base_url + url logging.info("this page is %s"%url_) self.driver.maximize_window() self.driver.get(url_) assert self.driver.current_url == url_, 'Did ont land on %s' % url_ def open(self): """ 调用私有函数 :return: """ self._open(self.url) # def browser(self): # driver = webdriver.Chrome() # driver.maximize_window() # driver.implicitly_wait(10) # return driver # def get_url(self,url): # return self.driver.get(url) # # def quit(self): # return self.driver.quit() def __locate_Element_selector(self, selector): """ 八种定位方式选择 :param selector: 传入的格式必须为:定位方式,定位元素值,顺序不可改变 :return: 返回定位方式 """ selector_by = selector.split(',')[0].strip() selector_value = selector.split(',')[1].strip() if selector_by in ('i', 'id'): locator = (By.ID, selector_value) elif selector_by in ('n', 'name'): locator = (By.NAME, selector_value) elif selector_by in ('c', 'class'): locator = (By.CLASS_NAME, selector_value) elif selector_by in ('x', 'xpath'): locator = (By.XPATH, selector_value) elif selector_by in ('s', 'css'): locator = (By.CSS_SELECTOR, selector_value) elif selector_by in ('t', 'tag_name'): locator = (By.TAG_NAME, selector_value) elif selector_by in ('l', 'link_text'): locator = (By.LINK_TEXT, selector_value) elif selector_by in ('ll', 'partial_link_text'): locator = (By.PARTIAL_LINK_TEXT, selector_value) else: raise Exception('selector Error') return locator def find_element(self,selector): """ 单个元素定位 :param loc:定位方式和元素属性 :return: """ time.sleep(0.5) try: loc = self.__locate_Element_selector(selector) WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(loc)) return self.driver.find_element(*loc) except: logging.error('-------------定位异常{0}--------------'.format(selector)) def find_elements(self,selector): """ 多个元素定位 :param loc:定位方式和元素属性 :return: """ time.sleep(0.5) try: loc = self.__locate_Element_selector(selector) WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(loc)) return self.driver.find_elements(*loc) except: logging.error('-------------定位异常{0}--------------'.format(selector)) def select_text(self,loc,text): """ 遍历获取到的元素并点击 :param loc:定位到的一组元素 :param text: 想要点击的元素文本 :return: """ listtext = loc for i in listtext: if i.text == text: i.click() break else: logging.error("没有找到想要的元素%s" % text) def switch_frame(self, loc): """ 多表单嵌套切换 :param loc: 传元素的属性值 :return: 定位到的元素 """ try: return self.driver.switch_to_frame(loc) except NoSuchFrameException as msg: logging.error("查找iframe异常-> {0}".format(msg)) def switch_windows(self, loc): """ 多窗口切换 :param loc: :return: """ try: return self.driver.switch_to_window(loc) except NoSuchWindowException as msg: logging.error("查找窗口句柄handle异常-> {0}".format(msg)) def switch_alert(self): """ 警告框处理 :return: """ try: return self.driver.switch_to_alert() except NoAlertPresentException as msg: logging.error("查找alert弹出框异常-> {0}".format(msg)) def get_element_attribute(self, selector: str, value='value') -> str: """获取元素属性""" ele = self.find_element(selector) return ele.get_attribute(value) def excute_script(self, script: str, element: str) -> None: """执行js脚本""" self.driver.execute_script(script, element) def exhibition_element(self, selector: str) -> None: """将元素显示到可见窗口中 """ ele = self.find_element(selector) js = "arguments[0].scrollIntoView();" self.excute_script(js, ele) def send_key(self, selector, value, clear_first=True, click_first=True): """重写定义send_keys方法""" loc = self.__locate_Element_selector(selector) try: #loc = getattr(self, "_%s" % loc) # getattr相当于实现self.loc if click_first: self.driver.find_element(*loc).click() if clear_first: self.driver.find_element(*loc).clear() self.driver.find_element(*loc).send_keys(value) except AttributeError: logging.error("%s 页面中未能找到 %s 元素" % (self,loc))