页面对象设计模式(Page Objects)
- 对象库层
- 逻辑层
- 业务层
- 数据层
依据页面模式的设计思想,创建测试框架
- base 包里面存放基础代码
- page 包里面编辑关于页面对象层的代码
- utils 包中编写读取文件的方法
- textCase 编写页面对象中所有的测试代码
- data 文件夹存放数据,测试数据存储xml文件中,
- report 存放测试报告
1、 basePage.py
from selenium import webdriver
from selenium.webdriver.support.expected_conditions import NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
import time as t
class WebDriver(object):
def __init__(self,driver):
'''定义开启浏览器的类型 ,及webdriver实例化后的对象'''
self.webdr = driver
def findElement(self,*loc):
'''单个定位元素的方法'''
try:
return WebDriverWait(self.webdr,20).until(lambda x:x.find_element(*loc))
except NoSuchElementException as e:
print('Error Details {0}'.format(e.args[0]))
def findElements(self,*loc):
'''多个定位元素的方法'''
try:
return WebDriverWait(self.webdr,20).until(lambda x:x.find_elements(*loc))
except NoSuchElementException as e:
print('Error Details {0}'.format(e.args[0]))
def switchFrame(self, *loc):
self.webdr.switch_to.frame(*loc)
2、init.py
from selenium import webdriver
import unittest
from operationXml import *
import time as t
class Init(unittest.TestCase, OperationXml):
def setUp(self) -> None:
self.webdr = webdriver.Firefox()
self.webdr.maximize_window()
self.webdr.get(self.getXmlData("url"))
self.webdr.implicitly_wait(20)
def tearDown(self) -> None:
self.webdr.quit()
3、mail126.py
from base.basePage import *
from selenium.webdriver.common.by import By
class Mail126(WebDriver):
swich_ToUserPAWMethod_loc = (By.ID, "lbNormal")
iframe_loc = (By.XPATH, "//div[@id='loginDiv']/iframe")
userName_loc = (By.NAME, "email")
paw_loc = (By.NAME, "password")
loginButton_loc = (By.ID, "dologin")
login_Error_loc = (By.CLASS_NAME, "ferrorhead")
@property
def swichTo_UserPAWLogin(self):
self.findElement(*self.swich_ToUserPAWMethod_loc).click()
@property
def swichTo_Iframe(self):
# iframe = self.findElement(*self.iframe_loc)
self.switchFrame(self.findElement(*self.iframe_loc))
def type_Useranme(self,username):
self.findElement(*self.userName_loc).send_keys(username)
def type_passWord(self,password):
self.findElement(*self.paw_loc).send_keys(password)
@property
def clickLogin(self):
self.findElement(*self.loginButton_loc).click()
@property
def getLoginError(self):
return self.findElement(*self.login_Error_loc).text
def login(self,username,password):
self.swichTo_UserPAWLogin
self.swichTo_Iframe
self.type_Useranme(username)
self.type_passWord(password)
self.clickLogin
4、test_mail126.py
import unittest
from init import *
from mail126 import *
from selenium import webdriver
from operationXml import *
import time as t
class Mail126Test(Init, Mail126):
def test_mail126Login_001(self, parent="divText", value="emailNull"):
'''登录业务:账号和密码为空'''
self.login('', '')
self.assertEqual(self.getLoginError, self.getXmluser(parent, value))
def test_mail126Login_002(self,parent="divText", value="emailFormat"):
'''登录业务:账号格式不正确'''
self.login("王五","123456")
self.assertEqual(self.getLoginError, self.getXmluser(parent,value))
if __name__ == '__main__':
unittest.main(verbosity=2)
5、operationXml.py
import xml.dom.minidom
import os
class OperationXml(object):
def _dir_base(self,fileName,filepath="data"):
'''
:param fileName: 读取的文件名称
:param filepath: 要读取的文件的相对路径
:return:
'''
return os.path.join(os.path.dirname(os.path.dirname(__file__)),filepath,fileName)
@property
def _open_xml(self):
dom = xml.dom.minidom.parse(self._dir_base("ui"))
# dom = xml.dom.minidom.parse('D:/学习文件/Appium自动化测试/Page_objects/Page_objects/data/ui')
# dom = xml.dom.minidom.parse('D:/学习文件/Appium自动化测试/Page_objects/data/ui')
db = dom.documentElement
return db
def getXmlData(self,value):
'''
获取xml单个节点的数据
:param value: xml单个节点的名称
:return:
'''
db = self._open_xml
name = db.getElementsByTagName(value)
nameValue = name[0]
return nameValue.firstChild.data
def getXmluser(self,parent,child):
db =self._open_xml
itemlist = db.getElementsByTagName(parent)
item = itemlist[0]
return item.getAttribute(child)
6、allTests.py
import unittest
import os
import HTMLTestRunner
import time
def allTests():
'''获取所有需要执行的测试用例'''
path =r"D:\学习文件\Appium自动化测试\Page_objects\test_Case"
# suit = unittest.defaultTestLoader.discover(start_dir=os.path.join(os.path.dirname(os.path.dirname(__file__)),"testCase"),pattern='test_*.py',top_level_dir=None)
suit = unittest.defaultTestLoader.discover(start_dir=path, pattern='test_*.py',top_level_dir=None)
return suit
def getNowTime():
'''获取当前时间'''
return time.strftime('%Y-%m-%d %H_%M_%S', time.localtime(time.time()))
def run():
fileName = os.path.join(os.path.dirname(os.path.dirname(__file__)), "report",getNowTime()+"mail126Report.html")
with open(fileName,"wb") as f:
runner = HTMLTestRunner.HTMLTestRunner(stream=f, title="126邮箱自动化测试报告",description="126邮箱登录自动化测试报告详细信息")
runner.run(allTests())
if __name__ == '__main__':
run()