POM设计模式,即Page Object Model。UII自动化测试中,POM模式是一种设计思路,把每个页面当成一个对象。
POM模式的优点:
提供了页面元素操作和业务流程相分离的模式,使测试的代码结构比之前更加清晰,可读性强。
方便复用对象和方法,提高了代码的复用性
POM设计模式 四层架构:
1层:basepage 层 每个页面的相同的属性和方法
2层:pageobject 针对每个页面定义页面类 每个页面独有的属性和方法
3层:testcase层 用例层 处理业务逻辑
4层:testdata数据层
basepage 层
bash
from demo.pagebase.page_base_drission import PageBaseDrission
from demo.pagebase.page_base_selenium import PageBaseSelenium
class PageBase(PageBaseSelenium,PageBaseDrission):
def __init__(self, web_driver,web_page):
self.web_driver = web_driver
self.web_page=web_page```
```bash
from DrissionPage import WebPage
class PageBaseDrission:
def __init__(self,web_page):
self.web_page=web_page
def input_d(self,loc,value):
loc_sel=self.finde_ele_d(loc)
loc_sel.clear()
loc_sel.input(value)
def click_d(self,loc):
self.finde_ele_d(loc).click()
def finde_ele_d(self,loc,timeout=10):
self.web_page.wait.eles_loaded(loc)
return self.web_page.ele(loc)
bash
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class PageBaseSelenium:
def __init__(self,web_driver):
self.web_driver=web_driver
def input(self,loc,value):
self.finde_ele(loc).send_keys(value)
def click(self,loc):
self.finde_ele(loc).click()
def finde_ele(self,loc,timeout=10):
WebDriverWait(self.web_driver,timeout=timeout).until(EC.presence_of_element_located(loc))
return self.web_driver.find_element(*loc)
pageobject
bash
import time
from demo.pagebase.page_base import PageBase
from DrissionPage.common import By as DBy
from selenium.webdriver.common.by import By
class LoginPage(PageBase):
url = "https://passport.jd.com/new/login.aspx"
def login(self,loginname,password):
self.web_driver.get(LoginPage.url)
self.web_page.get(LoginPage.url)
el = (By.ID, "loginname")
self.input(el,loginname)
el = (By.ID, "nloginpwd")
self.input(el,password)
el = (By.ID, "loginsubmit")
self.click(el)
time.sleep(15)
el = (DBy.ID, "loginname")
self.input_d(el, loginname)
el = (DBy.ID, "nloginpwd")
self.input_d(el, password)
el = (DBy.ID, "loginsubmit")
self.click_d(el)
time.sleep(15)
testcase 层
bash
import unittest
import BeautifulReport
from selenium import webdriver
from demo.pageobject.login_page import LoginPage
from DrissionPage import WebPage
class TestLogin(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.web_driver = webdriver.Firefox()
cls.web_page=WebPage("d")
@classmethod
def tearDownClass(cls):
cls.web_driver.quit()
cls.web_page.quit()
def test_login(self):
login_name = "用户名"
pwd = "密码"
LoginPage(self.web_driver,self.web_page).login(loginname=login_name,password=pwd)
if __name__ == '__main__':
case = unittest.TestLoader().loadTestsFromTestCase(TestLogin)
BeautifulReport.BeautifulReport(case).report(description="测试报告",filename="report")