浅谈测试自动化selenium之POM模式

基于本人也是一个初学者,在运用POM模式的时候记录一下自己的学习笔记。

如果你是大神,那么可以略过,如果你是初学者,希望对你有帮助。

本文阐述了以下几个问题:

什么叫POM模式

为什么要用POM模式

POM模式的思想

POM模式的运用

在刚学习selenium自动化测试的时候,一般都是:

1.打开网页

2.定位元素,进行操作

3.关闭网页

以百度为例:

python 复制代码
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

driver=webdriver.Chrome()
driver.get("https://www.baidu.com/")

text_input=driver.find_element(By.XPATH,'//*[@id="kw"]')
text_input.send_keys("京东")
submit_button=driver.find_element(By.XPATH,'//*[@id="su"]')
submit_button.click()
time.sleep(8)
driver.quit()

以上是一个简单的访问百度网页,搜索京东的例子。

因为只有一个页面,所以相对来说使用这种模式的弊端没有显露出来。

当被测对象有很多页面的时候,问题开始显现出来了。

假设在5个测试用例中都使用到了元素X,那么,当前端对元素X做了更新处理的时候。我们就需要找到这5个引用到了元素X的地方,对元素X进行更改。

所以,引入POM模式。

什么叫POM模式

POM模式:Page Object Model,即页面对象模型。

通俗讲,就是把页面的元素、操作、数据等分离开来,再通过用例调用。

本质上就是一种封装的思想,让代码逻辑更清晰,容易维护。这样的话,就能减少重复大量的定位元素和维护的时间成本。

为什么要用POM模式

通过这种模式,我们把页面的元素定位和业务操作分离开。

1.多个测试人员可同时编写和维护脚本

2.代码逻辑更清晰,更易维护

POM模式的思想

将页面分为3层:操作层、页面层、用例层

操作层:就是对一些元素的公共操作。比如:点击,输入,拖拽。

页面层:页面元素的定位,及属于该页面独有的操作也可封装在这里。

用例层:在页面中操作元素。也就是测试用例。

关系如下图,网络找的,侵删。

操作层:BasePage,点击,输入,拖拽等公共的操作。

页面层:Page,继承BasePage,实现元素定位以及一些该页面独有的功能。

用例层:TestCase,测试用例。

POM模式的运用

根据POM模式的思想:

1.首先封装公共操作到base_page

2.然后定位页面元素至page

3.最后在test_case写测试用例

base_page.py:

定义了打开网页,定位元素,点击,输入,关闭网页的方法。

可被其他页面继承。

python 复制代码
from selenium import webdriver
import logging
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class BasePage:
    def __init__(self):
        self.driver=webdriver.Chrome()

    def open_url(self,url):
        self.driver.get(url)
        self.driver.maximize_window()

    def find_element(self,locator,timeout=10):
        try:

            element=WebDriverWait(self.driver,timeout).until(EC.presence_of_element_located(locator))
            return element
        except:
            logging("{locator}元素没有找到")
            raise


    def click(self,locator):
        element=self.driver.find_element(*locator)
        element.click()

    def send_keys(self,locator,text):
        element=self.driver.find_element(*locator)

        element.send_keys(text)

    def quit(self):
        self.driver.quit()

index_page.py:

具体的页面元素定位,以及一些该页面元素独有的方法。

继承了BasePage。

python 复制代码
from selenium.webdriver.common.by import By
from base_page import BasePage
class IndexPage(BasePage):
    text_input = (By.XPATH, '//*[@id="kw"]')
    submit_button = (By.XPATH, '//*[@id="su"]')
    def input_text(self,text):
        self.send_keys(self.text_input,text)
    def submit(self):
        self.click(self.submit_button)

testcase.py

前置操作,初始化driver,打开网页。

执行测试用例。

后置操作,关闭浏览器。

python 复制代码
import time
import unittest
from index_page import IndexPage

class TestCaseSearch(unittest.TestCase):
    def setUp(self)->None:
        self.driver=IndexPage()
        self.driver.open_url("https://www.baidu.com/")

    def tearDown(self)->None:
        self.driver.quit()

    def testSearch(self):
        self.driver.input_text(text="京东")
        self.driver.submit()
        time.sleep(8)

if __name__ =="__main__":
    unittest.main()
相关推荐
橡晟3 小时前
深度学习入门:让神经网络变得“深不可测“⚡(二)
人工智能·python·深度学习·机器学习·计算机视觉
墨尘游子3 小时前
神经网络的层与块
人工智能·python·深度学习·机器学习
倔强青铜34 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
企鹅与蟒蛇4 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
autobaba4 小时前
编写bat文件自动打开chrome浏览器,并通过selenium抓取浏览器操作chrome
chrome·python·selenium·rpa
Rvelamen5 小时前
LLM-SECURITY-PROMPTS大模型提示词攻击测评基准
人工智能·python·安全
【本人】5 小时前
Django基础(一)———创建与启动
后端·python·django
七夜zippoe6 小时前
破解 VMware 迁移难题:跨平台迁移常见问题及自动化解决方案
运维·自动化·vmware
SHIPKING3937 小时前
【python】基于pygame实现动态粒子爱心
开发语言·python·pygame
kk_stoper8 小时前
如何通过API查询实时能源期货价格
java·开发语言·javascript·数据结构·python·能源