• Python自动化测试学习(二)— Page Object实例


    一、认识PO模式:

             PO设计模式简单讲就是讲页面对象和业务逻辑分层,使得代码清晰,可维护性高

    二、 PO设计模式优点:

    1.  减少代码的可重复性
    2.  让测试具有可读性
    3.  提高了代码的可维护性(当被测程序较多时,可以方便添加)

    三、PO模式常用目录结构

    • pages:一般用来存放页面元素对象
    • report:存放测试报告
    • testcase:测试用例
    • ......

    四、通过 Page Object 设计模式来实现,以163邮箱登录为例:

    BasePage类,所有页面元素定位相关的基类

    1、可变参数*loc作为参数传递,使用可变参数时会使得公共方法会更少。例如之前写的java自动化代码,将id、css、xpath等定位方法都封装了一遍,导致相同的代码较多。

    2、定位frame框架。

    1. 通过元素定位先定位到frame框架。
    1. 再通过driver.switch_to.frame(“”)切换到frame
    from selenium import webdriver
    from time import sleep
    '''
    页面元素定位的基类
    '''
    
    
    class BasePage(object):
    
        def __init__(self, driver):
            print("进入Basepage")
            self.driver = driver
        '''
        元素定位
        '''
        def find_element(self, *loc):
            return self.driver.find_element(*loc)
        '''
        frame切换
        '''
        def switch_frame(self, *loc):
            xpath_frame = self.driver.find_element(*loc)
            self.driver.switch_to.frame(xpath_frame)
    
        '''
        打开浏览器
        '''
        def open_browser(self, url):
            print("调用打开浏览器方法....")
            self.driver.get(url)
            sleep(3)
            # 设置浏览器打开样式
            self.driver.maximize_window()
            sleep(3)
            self.driver.implicitly_wait(10)
    BasePage

    LoginPage类,登录页面相关元素定位

    from src.pages.BasePage import BasePage
    from selenium.webdriver.common.by import By
    from time import sleep
    # 相对导入 .代表当前模块,..代表上层模块,...代表上上层模块,依次类推
    # 绝对导入 import A.B 或 from A import B
    '''
    登录页面元素操作
    '''
    
    
    class LoginPage(BasePage):
        username_loc = (By.NAME, "email")
        password_loc = (By.NAME, "password")
        loginbut_loc = (By.ID, "dologin")
        frame_loc = (By.XPATH, "//div[@id='loginDiv']/iframe")
    
        # 输入用户名
    
        def login_username(self, username):
            self.find_element(*self.username_loc).send_keys(username)
    
        # 输入密码
    
        def login_password(self, password):
            self.find_element(*self.password_loc).send_keys(password)
    
        # 登录
        def login_button(self):
            self.find_element(*self.loginbut_loc).click()
    
        # 切换frame
        def switch_to_frame(self):
            self.switch_frame(*self.frame_loc)
    
        # 登录操作
        def login_operation(self, username, password):
            print("进入登录操作方法")
            self.switch_to_frame()
            self.login_username(username)
            self.login_password(password)
            self.login_button()
            sleep(3)
    LoginPage

    LoginCase类,测试用例

    1、首先继承于unittest下的TestCase类

    2、断言判断assertEqual

    from src.pages.LoginPage import LoginPage
    from selenium import webdriver
    from time import sleep
    import unittest
    
    '''
    用户登录测试用例
    '''
    
    
    class LoginCase(unittest.TestCase):
    
        def setUp(self):
            self.driver = webdriver.Chrome()
            self.url = "http://mail.163.com/"
            # 打开浏览器
            LoginPage(self.driver).open_browser(self.url)
            sleep(3)
        '''
        用户名或密码错误
        '''
        def test_login_1(self):
            LoginPage(self.driver).login_operation(username="1152", password="1213131")
            self.assertEqual("15210996111", "1152", "用户名或密码错误")
            print("测试用例成功")
    
        def tearDown(self):
            self.driver.quit()
    LoginCase
  • 相关阅读:
    跨域踩坑经验总结(内涵:跨域知识科普)
    Nginx location规则匹配
    CentOS 命令
    Centos 修改源
    Ubuntu下获取内核源码
    Ubuntu用户自定义脚本开机启动
    ubuntu 14.04安装x11VNC
    texi格式文件的读取
    更换主机后SSH无法登录的问题
    ubuntu操作系统的目录结构
  • 原文地址:https://www.cnblogs.com/liho/p/13724503.html
Copyright © 2020-2023  润新知