POM(Page Object Model):页面对象模型,是一种设计模式,用来管理维护一组web元素集的对象库。使用POM设计模式最终的目的是为了程序松耦合。
from selenium import webdriver
import unittest
from time import sleep
class TestBaidu(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.driver = webdriver.Chrome()
cls.driver.maximize_window()
def test_baidu(self):
self.driver.get('http://www.baidu.com')
self.driver.find_element_by_id('kw').send_keys('selenium')
self.driver.find_element_by_id('su').click()
sleep(5)
@classmethod
def tearDownClass(cls) -> None:
cls.driver.quit()
if __name__ == '__main__':
unittest.main()
在使用POM模式,代码改写如下:
from selenium import webdriver
import unittest
from selenium.webdriver.common.by import By
from time import sleep
class BaiduPage(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.input_element = (By.ID, 'kw')
self.btn_element = (By.ID, 'su')
def goto_baidu(self, url):
self.driver.get(url)
def test_search(self, url, kw):
self.goto_baidu(url)
self.driver.find_element(*self.input_element).send_keys(kw)
self.driver.find_element(*self.btn_element).click()
sleep(5)
# 松耦合
class TestBaidu(unittest.TestCase):
def setUp(self) -> None:
self.baiduPage = BaiduPage()
def test_search(self):
self.baiduPage.test_search('http://www.baidu.com', 'selenium')
if __name__ == '__main__':
unittest.main()
我们定义了一个BaiduPage类,里面定义了搜索的方法;在TestBaidu类中,定义了test_search方法,调用BaiduPage的test_search方法进行执行。
设计BasePage类,主要是为了提高项目的代码重用性。例如:上述例子,我们可以设计一个BasePage类,定义点击,搜索等方法。代码如下:
from selenium import webdriver
from selenium.webdriver.common.by import By
class BasePage(object):
def __init__(self, driver):
self.driver = driver
def get_element(self, *loc):
return self.driver.find_element(*loc)
def input_text(self, text, *loc):
self.get_element(*loc).send_keys(text)
def click(self, *loc):
self.driver.find_element(*loc).click()
def get_title(self):
return self.driver.title
class BaiduPage(BasePage):
def __init__(self, driver):
BasePage.__init__(self, driver)
driver.get('http:/www.baidu.com')
def test_search(self):
loc = (By.ID, 'kw')
loc2 = (By.ID, 'su')
self.input_text('selenium', *loc)
self.click(*loc2)
if __name__ == '__main__':
driver = webdriver.Chrome()
baiduPage = BaiduPage(driver)
baiduPage.test_search()
首先设计BasePage类:定义查找元素等一些基本方法
class BasePage(object):
def __init__(self, driver):
self.driver = driver
def find_element(self, *loc):
return self.driver.find_element(*loc)
def type_text(self, text, *loc):
self.find_element(*loc).send_keys(text)
def click(self, *loc):
self.find_element(*loc).click()
def clear(self, *loc):
self.find_element(*loc).clear()
def get_title(self):
return self.driver.title
定义userLoginPage.py,代码如下:继承了BasePage类
from time import sleep
from selenium.webdriver.common.by import By
from testcases.pom.ddt.pages.basePage import BasePage
class UserLoginPage(BasePage):
username_input = (By.NAME, 'user')
pwd_input = (By.NAME, 'pwd')
login_btn = (By.CLASS_NAME, 'btn')
def __init__(self, driver):
BasePage.__init__(self, driver)
def goto_login_page(self):
self.driver.get('http://localhost:8080/jpress/user/login')
self.driver.maximize_window()
def input_username(self, username):
self.clear(*self.username_input)
self.type_text(username, *self.username_input)
def input_pwd(self, pwd):
self.clear(*self.pwd_input)
self.type_text(pwd, *self.pwd_input)
def click_login_btn(self):
self.click(*self.login_btn)
sleep(2)
定义testUserLogin.py:
import pytest
from selenium.webdriver.support.wait import WebDriverWait
from testcases.pom.ddt.pages.userLoginPage import UserLoginPage
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from time import sleep
class TestUserLogin(object):
login_data=[
('','123456','账号不能为空'),
('admin', '123456', '用户中心-2'),
]
def setup_class(self) -> None:
self.driver = webdriver.Chrome()
self.loginPage = UserLoginPage(self.driver)
self.loginPage.goto_login_page()
# 测试用户登录
@pytest.mark.parametrize('username, pwd, expected', login_data)
def test_user_login(self, username, pwd, expected):
# 输入用户名
self.loginPage.input_username(username)
# 输入密码
self.loginPage.input_pwd(pwd)
# 点击登录
self.loginPage.click_login_btn()
sleep(3)
# 验证
if username != '':
# 等待提示框
WebDriverWait(self.driver, 5).until(EC.title_is(expected))
sleep(3)
# 验证
assert self.driver.title == expected
else:
# 等待提示框
WebDriverWait(self.driver, 5).until(EC.alert_is_present())
alert = self.driver.switch_to.alert
assert alert.text == expected
alert.accept()
if __name__ == '__main__':
pytest.main(['-sv', 'testUserLogin.py'])