Python PO模型中basepage常见方法封装

项目目录:

1、commom  公共管理方法

2、config   环境切换的方法及各个环境的地址

3、outputs  这里存放的为输出的日志,报告,以及错误截图

4、PageLocators  这里是各个页面的元素定位表达式

5、PageObject    这里是所有关于页面操作的封装

6、TestCases   这里存放的是所有测试用例

7、TestDatas   这里放的是各个页面中需要用到的测试数据

附上commom中basepage的封装:目前封装的是在项目中常用到,后续有使用的会继续添加

from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from Common.logger import log
import datetime
import time


class BasePage:

    def __init__(self,driver:WebDriver):
        self.driver = driver

    def save_img(self,img_name):   #保存截图
        """
        :param img_description:   图片描述 格式为 页面名称——功能名
        :return:
        """
        # 时间戳 time 模块
        now = int(time.time())
        otherStyleTime = time.strftime("%Y--%m--%d %H-%M-%S", time.localtime(now))
        img_path = "Outputs/imgs" + "/{}_{}.png".format(img_name,otherStyleTime)
        try:
            self.driver.save_screenshot(img_path)
        except:
            log.exception("网页截图失败")
        else:
            log.info("截图成功,截图存放在:{}".format(img_path))

    # 等待元素可见
    def wait_ele_visible(self,loc,img_name,timeout=60,frequency=0.5):
        start =  datetime.datetime.now()    #   "开始时间"   用datetime模块获取时间
        # time.sleep(3)
        try:
            time.sleep(0.3)
            WebDriverWait(self.driver,30,frequency).until(EC.visibility_of_element_located(loc))
        except:
            # 日志
            log.exception("等待元素 {} 可见 失败!".format(loc))
            # 截图
            self.save_img(img_name)
            raise #抛出异常,让用例识别到异常将用例状态为失败
        else:
            time.sleep(0.3)
            end = datetime.datetime.now()   #   "等待结束时间"   用datetime模块获取时间
            log.info("等待 {} 元素 {} 可见成功, 耗时:{}".format(img_name,loc,end-start))

    # 查找元素
    def get_element(self,loc,img_name):
        try:
            time.sleep(0.3)
            ele = self.driver.find_element(*loc)
        except:
            # 日志
            log.exception("查找 {} 可见 {} 失败!".format(img_name,loc))
            # 截图
            self.save_img(img_name)
            raise  # 抛出异常,让用例识别到异常将用例状态为失败
        else:
            log.info("查找 {} 可见 {} 成功!".format(img_name,loc))
            return ele
    # 等待元素可以点击
    def wait_element_clickable(self,loc,img_name,timeout=60,frequency=0.5):
        try:
            time.sleep(0.3)
            ele = WebDriverWait(self.driver,timeout,frequency).until(EC.element_to_be_clickable(loc))
            return ele
        except:
            self.save_img(img_name)
            log.exception("元素找不到{}".format(img_name))
    # 操作元素
    def click_element(self,loc,img_name,timeout=60,frequency=0.5):
        # 先等待元素可见
        time.sleep(0.3)
        self.wait_ele_visible(loc,img_name,timeout,frequency)
        # 再查找元素
        ele = self.wait_element_clickable(loc,img_name)
        # ele = self.get_element(loc,img_name)
        # 操作元素
        try:
            time.sleep(0.3)
            ele.click()  # 点击操作
            log.info("点击 {} 元素 {} 成功!".format(img_name, loc))
        except:
            # 日志
            log.exception("点击 {} 元素 {} 失败!".format(img_name, loc))
            # 截图
            self.save_img(img_name)
            raise  # 抛出异常,让用例识别到异常将用例状态为失败

    # 复选框时使用该方法
    def click1_element(self,loc,img_name,timeout=60,frequency=0.5):
        time.sleep(0.3)
        # 查找元素
        ele = self.get_element(loc,img_name)
        # 操作元素
        try:
            time.sleep(0.3)
            ele.click()  # 点击操作
            log.info("点击 {} 元素 {} 成功!".format(img_name, loc))
        except:
            # 日志
            log.exception("点击 {} 元素 {} 失败!".format(img_name, loc))
            # 截图
            self.save_img(img_name)
            raise  # 抛出异常,让用例识别到异常将用例状态为失败
    # 悬停操作
    def hover(self, loc,img_name):
        ele = self.get_element(loc,img_name)
        try:
            ActionChains(self.driver).move_to_element(ele).perform()
            log.info("鼠标悬停在 {} 元素 {} 成功".format(img_name,loc))
        except:
            log.exception("鼠标悬停在 {} 元素 {} 失败".format(img_name,loc))
            self.save_img(img_name)
            raise

    # 跳转至页面顶部
    def scroll(self, height=None, width=None):
        time.sleep(0.3)
        if not height:
            height = 0
        if not width:
            width = 0
        js_code = "window.scrollTo({}, {});".format(width, height)
        self.driver.execute_script(js_code)
        return self

    def input_text(self,loc,value,img_name,timeout=60,frequency=0.5):
        # 先等待元素可见
        time.sleep(0.3)
        self.wait_ele_visible(loc, img_name, timeout, frequency)
        # 再查找元素
        ele = self.get_element(loc, img_name)
        # 操作元素
        try:
            time.sleep(0.3)
            ele.send_keys(value)  # 输入操作
            log.info("在 {} 元素 {} 上输入文本值:{} 成功!".format(img_name, loc,value))
        except:
            # 日志
            log.exception("在 {} 元素 {} 上输入文本值:{} 失败!".format(img_name, loc,value))
            # 截图
            self.save_img(img_name)
            raise  # 抛出异常,让用例识别到异常将用例状态为失败

    def get_element_attribute(self,loc,attr_name,img_name,timeout=60,frequency=0.5):
        self.wait_ele_visible(loc,img_name,timeout,frequency)   #    等待元素存在
        ele = self.get_element(loc,img_name)   #    查找元素
        # 获取属性
        try:
            attr_value = ele.get_attribute(attr_name)
        except:
            # 日志
            log.exception("获取 {} 元素 {} 的属性:{} 失败!".format(img_name, loc, attr_name))
            # 截图
            self.save_img(img_name)
            raise  # 抛出异常,让用例识别到异常将用例状态为失败
        else:
            log.info("获取 {} 元素 {} 的属性:{} 值为{}!".format(img_name, loc, attr_name,attr_value))
            return attr_value

    def get_text(self,loc,img_name,timeout=60,frequency=0.5):
        #  等待元素存在
        time.sleep(0.3)
        self.wait_ele_visible(loc, img_name, timeout, frequency)
        ele = self.get_element(loc,img_name)    # 查找元素
        # 获取文本值
        try:
            text = ele.text
        except:
            # 日志
            log.exception("获取 {} 元素 {} 的文本失败!".format(img_name, loc))
            # 截图
            self.save_img(img_name)
            raise  # 抛出异常,让用例识别到异常将用例状态为失败
        else:
            log.info("获取 {} 元素 {} 文本值为:{} !".format(img_name, loc, text))
            return text



    def switch_iframe(self,refrence,img_desc):
        """
        :param refrence: 识别iframe 可以是iframe的下标、name属性、WebElement对象、元组形式的定位表达
        :return:
        """
        try:
            time.sleep(0.3)
            WebDriverWait(self.driver,10).until(
                EC.frame_to_be_available_and_switch_to_it(refrence)
            )
            # self.driver.switch_to.default_content()
        except:
            # 日志
            log.exception("切换到{} 的iframe:{} 失败!".format(img_desc,refrence))
            # 截图
            self.save_img(img_desc)
            raise
        else:
            log.info("切换到{} 的iframe:{} 成功!".format(img_desc,refrence))



    def switch_to_default_content(self, img_desc):
        """
        切换iframe到main页面
        :param img_desc:
        :return:
        """
        try:
            time.sleep(0.3)
            log.info("切换iframe到main页面")
            self.driver.switch_to_default_content()
        except :
            log.error("切换iframe到main页面失败!")
            self.save_img(img_desc)
            raise

    def switch_to_parent(self, img_desc):
        '''
        切换iframe到上一层页面
        :param img_doc: 截图说明
        :return:
        '''
        try:
            time.sleep(0.3)
            log.info("切换iframe到上一层页面")
            self.driver.switch_to_parent()
        except :
            log.error("切换iframe到上一层页面失败!")
            self.save_img(img_desc)
            raise


    def clear_text(self,loc,img_name,timeout=60,frequency=0.5):
      # 清除文本框的内容
        try:
            time.sleep(0.3)
            log.info("清除 {} 元素 {} 的文本值成功!".format(img_name,loc))
            self.wait_ele_visible(loc,img_name,timeout,frequency)
            self.get_element(loc,img_name).clear()
        except:
            # 日志
            log.exception("清除 {} 元素 {} 的文本值失败!".format(img_name, loc))
            # 截图
            self.save_img(img_name)
            raise  # 抛出异常,让用例识别到异常将用例状态为失败

    # 移除js的操作(日历控件等)
    def execute_script(self,js):
        return self.driver.execute_script(js)

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值