页面对象设计模式(Page Objects)

页面对象设计模式(Page Objects)

  1. 对象库层
  2. 逻辑层
  3. 业务层
  4. 数据层

依据页面模式的设计思想,创建测试框架

  1. base 包里面存放基础代码
  2. page 包里面编辑关于页面对象层的代码
  3. utils 包中编写读取文件的方法
  4. textCase 编写页面对象中所有的测试代码
  5. data 文件夹存放数据,测试数据存储xml文件中,
  6. 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()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值