之前写的代码中都没有加入异常处理,规范写法,应该在每次查找元素或操作时加上异常处理、日志信息、失败截图等,如下:
def input_account(self, account): '''输入账号''' try: WebDriverWait(self.driver, 20).until(EC.visibility_of_element_located(self.account)) self.driver.find_element(*self.account).send_keys(account) except Exception: raise
但是为所有的查找元素和操作都加上这些耗时且不方便维护,为了简化操作,可以把一些公用的方法封装到 BasePage 类,其它页面 page 直接继承 BasePage 即可调用公共方法。
![82b75d34ea9538f2166ab3c1443ee6ae.png](https://i-blog.csdnimg.cn/blog_migrate/7a0bd2fb66a943adda9d826395a32c15.png)
1. 封装基本函数:执行日志、失败截图、异常处理等。
2. 所有页面公共操作方法。
文件结构:
示例代码:
base_page.pyimport osimport timefrom selenium.webdriver.remote.webelement import WebElementfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom common.logger import loggerimport configsclass BasePage: def __init__(self, driver): self.driver = driver def open(self, url): self.driver.get(url) def wait_ele_visible(self, locator:tuple, doc="", timeout=30, poll_frequency=0.5, ignored_exceptions=None): ''' 等待元素可见 :param locator: 元素定位方式,元组形式 :param timeout: 超时时间,默认30秒 :param poll_frequency: 轮询时间,默认0.5秒 :param ignored_exceptions: 要忽略的异常 :param doc: 截图模块名称,若没有则只以时间戳明明 :return: ''' logger.info(f"等待元素 {locator} 可见") try: WebDriverWait(self.driver, timeout, poll_frequency, ignored_exceptions).until(EC.visibility_of_element_located(locator)) except Exception as e: logger.exception(f"等待元素 {locator} 可见失败") self.save_screenshot(doc) raise e # 查找元素 def get_element(self, locator: tuple, doc="")->WebElement: logger.info(f"查找元素 {locator} ") try: return self.driver.find_element(*locator) except: logger.exception(f"查找元素 {locator} 失败") self.save_screenshot(doc) raise # 点击元素 def click_element(self, locator:tuple, doc=""): ele = self.get_element(locator, doc) try: ele.click() except: logger.exception(f"点击元素 {locator} 失败") self.save_screenshot(doc) raise # 输入文本 def input_text(self, locator:tuple, text, doc=""): ele = self.get_element(locator, doc) try: ele.send_keys(text) except: logger.exception(f"元素 {locator} 输入文本 {text} 失败") self.save_screenshot(doc) raise # iframe 切换 def switch_to_iframe(self, locator:tuple, doc=""): ele = self.get_element(locator, doc) try: self.driver.switch_to.frame(ele) except: logger.exception(f"切换到iframe {locator} 失败") self.save_screenshot(doc) raise # 失败截图 def save_screenshot(self, filename): if not os.path.exists(configs.SCREENSHOT_PATH): os.mkdir(configs.SCREENSHOT_PATH) # 图片名称:模块名-页面名-函数名-年月日时分秒.png file_name = configs.SCREENSHOT_PATH + filename + "_" + time.strftime("%Y%m%d%H%M%S") + ".png" self.driver.save_screenshot(file_name) logger.info(f"成功获取截图,路径:{file_name}")
demo_login_page.py,页面 page 类需继承 BasePage 类
import osfrom configparser import ConfigParserfrom common.base_page import BasePageclass LoginPage(BasePage): def get_locator(self): conf = ConfigParser() print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\page_locator\\demo_locator.ini") conf.read(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\page_locator\\demo_locator.ini", encoding="utf-8") self.url = conf.get("Base", "url") section = "LoginPageLocator" # eval 将字符串元组转换为元组 self.login_by_pwd_btn = eval(conf.get(section, "login_by_pwd_btn")) self.account = eval(conf.get(section, "account")) self.pwd = eval(conf.get(section, "pwd")) self.login_btn = eval(conf.get(section, "login_btn")) def __init__(self, driver): super().__init__(driver) self.get_locator() def login(self, account, password): doc = "登录页面_登录" self.driver.get(self.url) # 打开登录页面 self.click_element(self.login_by_pwd_btn, doc) # 点击密码登录 self.input_text(self.account, account, doc) # 输入账号 self.input_text(self.pwd, password, doc) # 输入密码 self.click_element(self.login_btn, doc) # 点击登录
demo_home_page.py,页面 page 类需继承 BasePage 类
import osfrom configparser import ConfigParserfrom common.base_page import BasePageclass HomePage(BasePage): def get_locator(self): conf = ConfigParser() conf.read(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\page_locator\\demo_locator.ini",encoding="utf-8") section = "HomePageLocator" self.account_info = eval(conf.get(section, "account_info")) def __init__(self, driver): super().__init__(driver) self.get_locator() def is_account_info_exist(self): try: self.wait_ele_visible(self.account_info) return True except Exception: return False
test_demo.py
from pytest_assume.plugin import assumefrom selenium import webdriverfrom po.page_object.demo_home_page import HomePagefrom po.page_object.demo_login_page import LoginPageclass TestLogin(): def setup_class(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(10) def teardown_class(self): self.driver.quit() def test_demo(self): self.login_page = LoginPage(self.driver) self.home_page = HomePage(self.driver) self.login_page.login("15348369418", "Pan910124") with assume: assert self.home_page.is_account_info_exist()
对于一些通用的方法,我们也可根据自己的需要进行封装,譬如从 demo_locator.ini 文件获取数据,我们可以封装一个工具类 handle_ini.py 放在 common 模块下:
import osfrom configparser import ConfigParserclass HandleIni(ConfigParser): # 已字典格式返回 ini 文件中某个 section 的数据 def get_locator_dict(self, locator_file, section): conf = ConfigParser() conf.read(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\po\\page_locator\\" + locator_file, encoding="utf-8") section_data = {} for key in conf.options(section): section_data[key] = conf.get(section, key) return section_data
那么其他页面类就可以使用 HandleIni 工具类来获取数据:
# demo_home_page.pyfrom common.base_page import BasePagefrom common.handle_ini import HandleIniclass HomePage(BasePage): def get_locator(self): locators = HandleIni().get_locator_dict("demo_locator.ini", "HomePageLocator") self.account_info = eval(locators.get("account_info")) def __init__(self, driver): super().__init__(driver) self.get_locator() def is_account_info_exist(self): try: self.wait_ele_visible(self.account_info) return True except Exception: return False
![ad3b4e64fe879de4a1a732447bc26f6a.gif](https://i-blog.csdnimg.cn/blog_migrate/980f7d6f6c860368bbe1754efd5dd43c.gif)
51Testing测试交流群,欢迎大家加入!
每日分享测试干货+技术交流
加下方小姐姐,
可加微信群
备注【测试圈】即可~
![bd0a31bce4550687e77be941c0cadbc9.gif](https://i-blog.csdnimg.cn/blog_migrate/603585ab181b72cd83963dfa65eb4490.gif)
链接:https://www.cnblogs.com/sharef/p/13645356.html
本文为51Testing经授权转载,转载文章所包含的文字来源于作者。如因内容或版权等问题,请联系51Testing进行删除
推荐阅读点击阅读☞Selenium那些值得再次巩固的知识
点击阅读☞元素定位方法吃得透,Selenium才能学得好
点击阅读☞WebDriver和浏览器间的竞争关系,Selenium竟然能解决!
点击阅读☞从零到设计Python+Selenium自动化测试框架,如何开始?
点击阅读☞想知道Selenium自动化会遇到哪些常见问题?你算来对了!
![c486345a023313889fd8a3ddd3f0c82b.gif](https://i-blog.csdnimg.cn/blog_migrate/6d0d2e77be6070fe040acdfd71cedd71.gif)