直接上代码
class WakaLoginPage(BasePage
):
'''
此类为页面类,通过每个页面封装成类来进行元素的定位器和流程写函数和变量
在实际工作中根据不同的业务逻辑进行参考变换,不要一成不变的使用POM思想
'''
# 定位器,定位页面的元素内容
username_locator
= ('id',
'username') # 输入账号框
password_locator
= ('id',
'password') # 输入密码框
submit_locator
= ('id',
'div_login_btn') # 登录按钮框
registration_locator
= ('css selector',
'#lose_password > span:nth-child(2)') # 用户注册框
Forget_locator
= ('css selector',
'#a_to_register > span:nth-child(2)') # 忘记密码框
locator_text
= ('link text',
'退出')
def Login(self, username, password
):
'''
登录流程
:param username:
:param password:
:return:
'''
self.send_keys
(self.username_locator, username
)
self.send_keys
(self.password_locator, password
)
self.click
(self.submit_locator
)
我们可以看到,WakaLoginPage这个页面类继承了BasePage类
在这个类总有两个主要的东西 一个是定位器 一个事业务逻辑功能
以下是测试代码 waka_login.py:
需要注意的是我们在实例化页面类的时候需要把BrowserEngine中返回的driver传入
因为页面类继承了basepage,而basepage中初始化函数便是driver,只有保证driver的统一才能掉用方法
import unittest
from Framework.browser_engine
import BrowserEngine
from pageobjects.waka_loginpage
import WakaLoginPage
class WkLogin(unittest.TestCase
):
def setUp(self):
self.browser
= BrowserEngine
(self)
self.driver
= self.browser.open_browser
(self)
self.login
= WakaLoginPage
(self.driver
)
def tearDown(self):
self.browser.quit_browser
(self.driver
)
def login_case(self, username, password
):
self.login.Login
(username, password
)
result
= self.login.is_text_in_element
(self.login.locator_text,
'退出')
self.assertEqual
(result,
True)
def test_login01(self):
# 输入正确的账号密码
self.login_case
('13968141450',
'1111111')
def test_login_02(self):
# 输入错误的账号密码
self.login_case
('111111',
'111111')
if __name__
== '__main__':
unittest.main
()
以下是跑完的断言和日志信息
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe "D:\PyCharm 5.0.4\helpers\pycharm\utrunner.py" D:\AutomationFramework\test_suit\waka_login.py::WkLogin true
Testing started at 17:44 ...
2018-01-19 17:44:38,423 - BrowserEngine - INFO - you select browser is Firefox
2018-01-19 17:44:38,423 - BrowserEngine - INFO - you select URL is https://www.waka6.com
2018-01-19 17:44:38,424 - BrowserEngine - INFO - Starting firefox browser
2018-01-19 17:44:46,745 - BrowserEngine - INFO - open url is https://www.waka6.com
2018-01-19 17:44:50,263 - BrowserEngine - INFO - maximize the current windows.
2018-01-19 17:44:50,267 - BrowserEngine - INFO - oppen https://www.waka6.com title success
2018-01-19 17:44:50,282 - BaseP - INFO - Positioning to the idusername element.
2018-01-19 17:44:50,335 - BaseP - INFO - SendKeys 13968141450 in ('id', 'username') success.
2018-01-19 17:44:50,343 - BaseP - INFO - Positioning to the idpassword element.
2018-01-19 17:44:50,390 - BaseP - INFO - SendKeys 1111111 in ('id', 'password') success.
2018-01-19 17:44:50,395 - BaseP - INFO - Positioning to the iddiv_login_btn element.
2018-01-19 17:44:51,276 - BaseP - INFO - click success iddiv_login_btn element.
2018-01-19 17:44:51,300 - BrowserEngine - INFO - Now , Close the browser
2018-01-19 17:44:52,166 - BrowserEngine - INFO - you select browser is Firefox
2018-01-19 17:44:52,167 - BrowserEngine - INFO - you select URL is https://www.waka6.com
2018-01-19 17:44:52,167 - BrowserEngine - INFO - Starting firefox browser
2018-01-19 17:45:00,541 - BrowserEngine - INFO - open url is https://www.waka6.com
2018-01-19 17:45:04,051 - BrowserEngine - INFO - maximize the current windows.
2018-01-19 17:45:04,061 - BrowserEngine - INFO - oppen https://www.waka6.com title success
2018-01-19 17:45:04,078 - BaseP - INFO - Positioning to the idusername element.
2018-01-19 17:45:04,146 - BaseP - INFO - SendKeys 111111 in ('id', 'username') success.
2018-01-19 17:45:04,153 - BaseP - INFO - Positioning to the idpassword element.
2018-01-19 17:45:04,203 - BaseP - INFO - SendKeys 111111 in ('id', 'password') success.
2018-01-19 17:45:04,210 - BaseP - INFO - Positioning to the iddiv_login_btn element.
2018-01-19 17:45:05,163 - BaseP - INFO - click success iddiv_login_btn element.
2018-01-19 17:45:15,328 - BaseP - INFO - No location to the element
2018-01-19 17:45:15,329 - BrowserEngine - INFO - Now , Close the browser
Failure
Traceback (most recent call last):
File "D:\AutomationFramework\test_suit\waka_login.py", line 28, in test_login_02
self.login_case('111111', '111111')
File "D:\AutomationFramework\test_suit\waka_login.py", line 18, in login_case
self.assertEqual(result, True)
AssertionError: False != True
到这里我们做到了 基本的配置文件类
浏览器引擎类
日志模块类
公共函数二次封装类
简述页面基本类继承公共函数二次封装类
页面基本类的内容
如何在testsuit中调用页面基本类完成测试用例
在接下来的学习中我们要解决问题
1.如何实现一次性跑多条测试用例
2.如何在拥有输入框的情况下参数化
3.如何解决在不关闭浏览器的情况下验证多条用例
4.如何在失败用例的时候结合截图装饰器
5.如何添加测试报告,优化测试报告
6.如何进行邮件发送的功能
7.如何运用集成环境完美的监控功能自动化脚本
在接下来我们从用例细节上考虑,对于一些特定的情况基本操作,对于一些特定的元素定位方法
细节的东西还需要我们以后多多实战慢慢基类。我希望能从大到小的思路和讲解能给大家思路。有帮助点个赞谢谢。