那么问题来了:什么是PO模型呢?简单理解就是:把每个页面当成一个对象,给这些页面当成一个类,主要就是完成元素定位和业务操作;它和测试脚本区分开来,需要什么取这些页面类去调用即可。这样的好处在于页面元素发生变化时,只需要去维护页面类即可,其他部分就不用管。
GitHub代码commits id:73a583a
PO模型的基本思路:register_page(查找页面元素类) —> register_handle(操作层,将查找到的元素位置上传递数据) —> register_business(业务层:调用操作层,根据操作层的传递的结果进行判断场景,如邮箱错误场景等) —> first_case(封装调用业务层,进行测试用例的场景组装)
此处代码居多里面的详细操作教程稍后补上,这块的代码还有一个小问题,就是传递的数据会重复输入,下面章节将会采用新的方法来解决这个问题。
此页面当然是指的我们的注册页面了,通过前面的学习我们发现查找页面元素可以独立封装成一个类,以便在其他地方去调用。
#!/bin/usr/env python3
# -*- coding: utf-8 -*-
# --------------------------------
# ProjectName:
# Author: Crisimple
# CreateTime: 2019/6/10 17:34
# FileName: register_page.py
# Description: 主页用来获取注册页面的页面元素
# Question: PO的精髓:页面、case、数据分离开来
# --------------------------------
from code_package.base.find_element import FindElement
class RegisterPage(object):
def __init__(self, driver):
self.find_element = FindElement(driver)
def get_email_element(self):
return self.find_element.get_element("user_email")
def get_username_element(self):
return self.find_element.get_element("user_name")
def get_password_element(self):
return self.find_element.get_element("password")
def get_code_element(self):
return self.find_element.get_element("code_text")
def get_button_element(self):
return self.find_element.get_element("register_button")
# 错误信息......
# 获取邮箱栏错误提示
def get_email_error_info(self):
return self.find_element.get_element("register_email_error")
# 获取用户名错误提示
def get_username_error_info(self):
return self.find_element.get_element("register_username_error")
# 获取密码错误提示
def get_password_error_info(self):
return self.find_element.get_element("register_password_error")
# 获取验证码错误提示
def get_code_error_info(self):
return self.find_element.get_element("captcha_code_error")
很自然滴,查找到页面元素后当然是要给这些元素的输入框输入我们的注册数据了。当然这块是得调用到页面元素查找类。
#!/bin/usr/env python3
# -*- coding: utf-8 -*-
# --------------------------------
# ProjectName:
# Author: Crisimple
# CreateTime: 2019/6/10 17:35
# FileName: register_handle.py
# Description: 输入传递即给页面传值------操作层
# Question:
# --------------------------------
from code_package.page.register_page import RegisterPage
class RegisterHandle(object):
def __init__(self, driver):
self.register_page = RegisterPage(driver)
# 输入邮箱
def send_user_email(self, email):
self.register_page.get_email_element().send_keys(email)
# 输入用户名
def send_user_name(self, username):
self.register_page.get_username_element().send_keys(username)
# 输入密码
def send_user_password(self, password):
self.register_page.get_password_element().send_keys(password)
# 输入验证码
def send_user_code(self, code):
self.register_page.get_code_element().send_keys(code)
# 获取错误信息(获取到注册页中的错误信息)
def get_user_text(self, info, user_info):
text = None
if info == "register_email_error":
text = self.register_page.get_email_error_info().send_keys("value")
elif info == "register_username_error":
text = self.register_page.get_username_error_info().send_keys("value")
elif info == "register_password_error":
text = self.register_page.get_password_error_info().send_keys("value")
elif info == "captcha_code_error":
text = self.register_page.get_code_error_info().send_keys("value")
else:
print("你的错误无法解决,本猿无能为力......")
return text
# 点击注册按钮
def click_register_button(self, button):
self.register_page.get_button_element().send_keys(button)
# 获取注册按钮的文字
def get_register_text(self):
return self.register_page.get_button_element().text
根据我们的测试业务需求,那些场景下需要进行测试;这里以异常输入场景的错误提示为测试点进行业务的封装。
#!/bin/usr/env python3
# -*- coding: utf-8 -*-
# --------------------------------
# ProjectName:
# Author: Crisimple
# CreateTime: 2019/6/12 13:16
# FileName: register_business
# Description: 封装操作层的方法:
# register_page ---> register_handle ---> register_business ---> first_case
# Question:
# --------------------------------
from code_package.handle.register_handle import RegisterHandle
class RegisterBusiness(object):
def __init__(self, driver):
self.register_handle = RegisterHandle(driver)
def user_base(self, email, name, password, code):
self.register_handle.send_user_email(email)
self.register_handle.send_user_name(name)
self.register_handle.send_user_password(password)
self.register_handle.send_user_code(code)
# self.register_handle.click_register_button()
def register_success(self):
if self.register_handle.get_register_text() == None:
return True
else:
return False
# 邮箱错误
def login_email_error(self, email, name, password, code):
self.user_base(email, name, password, code)
if self.register_handle.get_user_text("email_error", "请输入有效的电子邮件地址") == None:
print("邮箱校验不成功")
return True
else:
return False
# 用户名错误
def login_username_error(self, email, name, password, code):
self.user_base(email, name, password, code)
if self.register_handle.get_user_text("username_error", "字符长度必须大于等于4,一个中文字算2个字符") == None:
print("用户名校验不成功")
return True
else:
return False
# 密码错误
def login_password_error(self, email, name, password, code):
self.user_base(email, name, password, code)
if self.register_handle.get_user_text("password_error", "最少需要输入 5 个字符") == None:
print("密码校验不成功")
return True
else:
return False
# 验证码错误
def login_code_error(self, email, name, password, code):
self.user_base(email, name, password, code)
if self.register_handle.get_user_text("password_error", "验证码错误") == None:
print("验证码校验不成功")
return True
else:
return False
根据我们的业务测试类,编写对应的测试 case ,进行调用测试执行
#!/bin/usr/env python3
# -*- coding: utf-8 -*-
# --------------------------------
# ProjectName:
# Author: Crisimple
# CreateTime: 2019/6/12 13:08
# FileName: first_case.py
# Description:
# Question:
# --------------------------------
from code_package.business.register_business import RegisterBusiness
from selenium import webdriver
import unittest
class FirstCase(unittest.TestCase):
def setUp(self) -> None:
self.driver = webdriver.Chrome("../tools/chromedriver.exe")
self.driver.maximize_window()
self.driver.get("http://www.5itest.cn/register")
self.register_business = RegisterBusiness(self.driver)
def tearDown(self) -> None:
self.driver.close()
def test_login_email_error(self):
email_error = self.register_business.login_email_error('34', 'U1212', '12Test12', 'der56')
if email_error == True:
print("注册成功了,此条 case 执行失败")
# login('223', '1111')
# 通过 assert 判断是否为 error
def test_login_username_error(self):
username = self.register_business.login_username_error("[email protected]", "2", "Test@123", 'sdsds')
if username == True:
print("注册成功了,此条 case 执行失败")
def test_login_password_error(self):
password = self.register_business.login_password_error("[email protected]", "2gw", "2", 'sdsds')
if password == True:
print("注册成功了,此条 case 执行失败")
def test_login_code_error(self):
code = self.register_business.login_code_error("[email protected]", "2gw", "Test@123", 's')
if code == True:
print("注册成功了,此条 case 执行失败")
def test_login_success(self):
success = self.register_business.user_base("[email protected]", "Crisimple", "Test@123", "test1")
if success == True:
print("注册成功")
else:
print("注册失败")
# def main():
# first = FirstCase()
# first.test_login_email_error()
# first.test_login_username_error()
# first.test_login_password_error()
# first.test_login_success()
if __name__ == "__main__":
unittest.main()
到这里差不多就可以跑通测试流程了…