【郑州校区】PO模式实践
目标1. 能够采用PO模式的分层思想对页面进行封装 1. V4版本 采用PO模式的分层思想对代码进行拆分
1.1 PO分层封装 对登录页面进行分层封装:对象库层:LoginPage 操作层:LoginHandle 业务层:LoginProxy 调用业务层的方法,编写测试用例: 测试用例:TestLogin
1.2 示例代码 [AppleScript] 纯文本查看 复制代码 from po.utils import DriverUtil
class LoginPage: """
对象库层
"""
def init (self):
self.driver = DriverUtil.get_driver()
# 用户名输入框
self.username = None
# 密 码
self.password = None # 验证码输入框self.verify_code = None
# 登录按钮
self.login_btn = None
# 忘记密码
self.forget_pwd = None
def find_username(self):
return self.driver.find_element_by_id("username")
def find_password(self):
return self.driver.find_element_by_id("password")
def find_verify_code(self):
return self.driver.find_element_by_id("verify_code")
def find_login_btn(self):
return self.driver.find_element_by_name("sbtbutton")
def find_forget_pwd(self):
return self.driver.find_element_by_partial_link_text("忘记密码")
class LoginHandle: """
操作层
"""
def init (self): self.login_page = LoginPage()
def input_username(self, username): self.login_page.find_username().send_keys(username)
def input_password(self, pwd): self.login_page.find_password().send_keys(pwd)
def input_verify_code(self, code): self.login_page.find_verify_code().send_keys(code)
def click_login_btn(self): self.login_page.find_login_btn().click()
def click_forget_pwd(self): self.login_page.find_forget_pwd().click()
class LoginProxy: """
业务层
"""
def init (self): self.login_handle = LoginHandle()
# 登 录
def login(self, username, password, verify_code):
# 输入用户名self.login_handle.input_username(username) # 输入密码self.login_handle.input_password(password)
# 输入验证码
self.login_handle.input_verify_code(verify_code)
# 点击登录按钮
self.login_handle.click_login_btn()
# 跳转到忘记密码页面
def to_forget_pwd_page(self):
# 点击忘记密码
self.login_handle.click_forget_pwd()
import unittest
from po import utils
from po.utils import DriverUtil
from po.v4.page.login_page import LoginProxy
class TestLogin(unittest.TestCase): """
对登录模块的功能进行测试
"""
@classmethod
def setUpClass(cls):
cls.driver = DriverUtil.get_driver() cls.login_proxy = LoginProxy()
@classmethod
def tearDownClass(cls): DriverUtil.quit_driver()
def setUp(self):
# 打开首页
self.driver.get("http://localhost")
# 点击首页的‘登录’链接,进入登录页面
self.driver.find_element_by_link_text("登录").click()
# 账号不存在
def test_login_username_is_error(self): self.login_proxy.login("13099999999", "123456", "8888")
# 断言提示信息
msg = utils.get_tips_msg() print("msg=", msg)
self.assertIn("账号不存在", msg)
# 密码错误
def test_login_password_is_error(self): self.login_proxy.login("13012345678", "123456", "8888")
# 断言提示信息
msg = utils.get_tips_msg() print("msg=", msg)
self.assertIn("密码错误", msg)
2. V5版本 对PO分层之后的代码继续优化1. 优化对象库层的代码,抽取元素的定位方式,把定位信息定义在对象的属性中,便于集中管理 2. 优化操作层的代码,针对输入操作应该先清空输入框中的内容再输入新的内容 [AppleScript] 纯文本查看 复制代码 from selenium.webdriver.common.by import By from po.utils import DriverUtil
class LoginPage: """
对象库层
"""
def init (self):
self.driver = DriverUtil.get_driver()
# 用户名
self.username = (By.ID, "username")
# 密 码
self.password = (By.ID, "password")
# 验证码输入框
self.verify_code = (By.ID, "verify_code")
# 登录按钮
self.login_btn = (By.NAME, "sbtbutton")
# 忘记密码
self.forget_pwd = (By.PARTIAL_LINK_TEXT, "忘记密码")
def find_username(self):
return self.driver.find_element(self.username[0], self.username[1])
def find_password(self):
return self.driver.find_element(self.password[0], self.password[1])
def find_verify_code(self):
return self.driver.find_element(self.verify_code[0], self.verify_code[1])
def find_login_btn(self):
return self.driver.find_element(self.login_btn[0], self.login_btn[1])
def find_forget_pwd(self):
return self.driver.find_element(self.forget_pwd[0], self.forget_pwd[1])
class LoginHandle: """
操作层
"""
def init (self): self.login_page = LoginPage()
def input_username(self, username): self.login_page.find_username().clear() self.login_page.find_username().send_keys(username)
def input_password(self, pwd): self.login_page.find_password().clear() self.login_page.find_password().send_keys(pwd)
def input_verify_code(self, code): self.login_page.find_verify_code().clear() self.login_page.find_verify_code().send_keys(code)
def click_login_btn(self): self.login_page.find_login_btn().click()
def click_forget_pwd(self): self.login_page.find_forget_pwd().click()
class LoginProxy: """
业务层
"""
def init (self): self.login_handle = LoginHandle()
# 登 录
def login(self, username, password, verify_code):
# 输入用户名
self.login_handle.input_username(username)
# 输入密码
self.login_handle.input_password(password)
# 输入验证码
self.login_handle.input_verify_code(verify_code)
# 点击登录按钮
self.login_handle.click_login_btn()
# 跳转到忘记密码页面
def to_forget_pwd_page(self): # 点击忘记密码self.login_handle.click_forget_pwd() |