UI自动化测试-常用的浏览器操作二次封装(干货分享)

UI自动化测试-常用的浏览器操作二次封装(干货分享)

前言:最近在学习巩固一些pytest+selenium的测试框架中的知识点,把学习过程中一些常用的操作做一下分享。

这里主要分享下基于PO模型的操作层中的常用操作方法的二次封装(干货分享)
废话不多说,直接上代码:

import os
import allure
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys


class BaseOperate:

    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver=self.driver, timeout=10, poll_frequency=0.5)
        self.action = ActionChains(self.driver)

    # 查找可交互的元素
    def findElement(self, locator, index=0):
        try:
            if index == -1:
                return self.wait.until(EC.visibility_of_any_elements_located((By.XPATH, locator)))
            else:
                return self.wait.until(EC.visibility_of_any_elements_located((By.XPATH, locator)))[index]
        except Exception:
            assert False, '查找元素失败,请检查!locator表达式为:%s' % str(locator)

    # 截取全屏
    def getFullScreenShot(self, title=''):
        allure.attach(self.driver.get_screenshot_as_png(), "%s截图" % title, allure.attachment_type.PNG)

    # 上传文件
    def uploadFile(self, locator, filename, index=0):
        filePath = os.path.join(os.path.realpath(os.path.dirname(os.path.dirname(__file__))), 'testdata', 'file')
        fullFilePath = os.path.join(filePath, filename)
        self.findElementByPresence(locator, index).send_keys(fullFilePath)

    # 执行JS
    def executeJS(self, js, element):
        self.driver.execute_script(js, element)

    # 查找加载到HTML里面的元素
    def findElementByPresence(self, locator, index=0):
        try:
            if index == -1:
                return self.wait.until(EC.presence_of_all_elements_located((By.XPATH, locator)))
            else:
                return self.wait.until(EC.presence_of_all_elements_located((By.XPATH, locator)))[index]
        except Exception:
            assert False, '查找元素失败,请检查!locator表达式为:%s' % str(locator)

    # 判断元素对象是否存在
    def judeElementExist(self, locator, index=0):
        try:
            if index == -1:
                return self.wait.until(EC.presence_of_all_elements_located((By.XPATH, locator)))
            else:
                return self.wait.until(EC.presence_of_all_elements_located((By.XPATH, locator)))[index]
        except Exception:
            return False

    # 滚动元素到指定位置
    def scrollElement(self, locator, index=0):
        ele = self.findElement(locator, index)
        js = "arguments[0].scrollIntoView();"
        self.driver.execute_script(js, ele)

    # 点击元素
    def clickElement(self, locator, index=0, isScroll=1):
        if isScroll:
            self.scrollElement(locator, index)
            time.sleep(0.5)
        self.findElement(locator, index).click()

    # 输入内容
    def elementSendKeys(self, locator, key, clear=1, isScroll=1, index=0):
        if isScroll:
            self.scrollElement(locator, index)
            time.sleep(0.5)
        element = self.findElement(locator, index)
        if clear:
            # element.clear()
            # clear方法部分情况下不能成功清除文本框中的内容,改为模拟键盘删除
            element.send_keys(Keys.CONTROL + "a")
            element.send_keys(Keys.DELETE)
        time.sleep(0.5)
        element.send_keys(key)

    # 获取元素文本
    def getElementText(self, locator, index=0, isScroll=0, type=1):
        if isScroll:
            self.scrollElement(locator)
            time.sleep(0.5)
        if type == 1:
            elements = self.findElement(locator, -1)
        else:
            elements = self.findElementByPresence(locator, -1)
        if index == -1:
            text = []
            for element in elements:
                text.append(element.text)
            return text
        else:
            return elements[index].text

    # 获取元素的属性
    def getElementAttribute(self, locator, attributeName, index=0, type=1):
        if type == 1:
            elements = self.findElement(locator, -1)
        else:
            elements = self.findElementByPresence(locator, -1)
        if index == -1:
            return [ele.get_attribute(attributeName) for ele in elements]
        else:
            return elements[index].get_attribute(attributeName)

    # 获取元素的CSS样式
    def getElementCSS(self, locator, CSSName, index=0):
        elements = self.findElement(locator, index=-1)
        if index == -1:
            return [ele.value_of_css_property(CSSName) for ele in elements]
        else:
            return elements.value_of_css_property(CSSName)

    # 鼠标悬浮操作
    def mouseHover(self, locator, index=0):
        element = self.findElement(locator, index)
        self.action.move_to_element(element).perform()

    # 鼠标点击操作
    def mouseClick(self, locator, index=0):
        element = self.findElement(locator, index)
        self.action.click(element).perform()

    # 切换frame
    def switchFrame(self, locator):
        self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, locator)))

    # 切回父frame
    def switchParentFrame(self):
        self.driver.switch_to.parent_frame()

    # 切回主文档
    def switchMainPage(self):
        self.driver.switch_to.default_content()

说明:

1.在selenium共有3种等待方式(强制等待、隐式等待和显示等待),这里推荐使用WebDriverWait元素等待(显示等待),结合EC.visibility_of_any_elements_located(locator)方法,确保页面元素均是可见的;在大部分情况下操作浏览器元素对象时,元素需要是存在且可见的
2.少数情况下执行元素操作时,不需要元素可见,仅需要保证元素是存在的,通过EC.presence_of_all_elements_located(locator)方法,判断元素是否存在于HTML页面中;常见的应用场景:文件上传,文件上传的input标签页面不可见,但可以通过send_keys()方法实现文件上传,此时就不需要元素可见了
3.常用的操作方法包括文本输入、点击事件、页面滚动、模拟鼠标事件、执行js、iframe切换已经获取元素属性等待,可根据测试需要选择合适的方法进行操作

今天分享到这儿,存在不足的地方还请大家多多指教!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值