PO模式实战

PO模式实战

基本的封装 三层

conf为配置文件、一般放yaml
page 元素页面
test_case 测试用例
conf.py类里面的一些默认配置文件





# -*- coding: utf-8 -*-
# @Author:lsf
# @File:conf 
# @Time:2021/7/20
# @Email:2795849212@qq.com
# @Software:PyCharm

HOST = 'http://127.0.0.1:8088'
userName = 'libai'
passWord = 'opmsopms123'

driver.py 类里面的内容




# -*- coding: utf-8 -*-
# @Author:lsf
# @File:driver 
# @Time:2021/7/20
# @Email:2795849212@qq.com
# @Software:PyCharm

from selenium import webdriver
from day6.conf.conf import *
import time


class Driver:

    # 声明一个全局变量
    _driver = None

    @classmethod
    def get_driver(cls, bower_name='chrome'):

        # 先判断 driver是否为None、为None代表新创建 driver对象、否则直接返回出去
        if cls._driver is None:
            if bower_name == 'chrome':
                cls._driver = webdriver.Chrome()
                # 最大化浏览器
                cls._driver.maximize_window()

                # 打开登录页面
                cls._driver.get(HOST)

                # 隐士等待
                cls._driver.implicitly_wait(10)
                time.sleep(3)
                cls.login()

        return cls._driver

    # 登录
    @classmethod
    def login(cls):
        # 输入用户名和密码
        cls._driver.find_element_by_name('username').send_keys(userName)
        cls._driver.find_element_by_name('password').send_keys(passWord)

        # 点击登录按钮
        cls._driver.find_element_by_css_selector('button').click()


if __name__ == '__main__':
    Driver().get_driver()




basePage.py里的内容



# -*- coding: utf-8 -*-
# @Author:lsf
# @File:basePage 
# @Time:2021/7/26
# @Email:2795849212@qq.com
# @Software:PyCharm

from day6.conf.driver import Driver

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:

    def __init__(self):
        self.driver = Driver().get_driver()

    # 封装一个 显示等待的方法、查找单个元素
    def get_element(self, locate):
        WebDriverWait(driver=self.driver, timeout=20, poll_frequency=0.5).until(
            EC.visibility_of_element_located(locate)
        )

        return self.driver.find_element(*locate)

    # 封装一个 显示等待的方法、查找多个元素
    def get_elements(self, locate):
        WebDriverWait(driver=self.driver, timeout=20, poll_frequency=0.5).until(
            EC.visibility_of_element_located(locate)
        )

        return self.driver.find_elements(*locate)

    # 切入iframe里面后在切回默认页面
    def iframe_default(self):
        self.driver.switch_to_default_content()

    # 关闭 driver驱动
    def close_driver(self):
        self.driver.quit()




managerPage.py类里的内容


# -*- coding: utf-8 -*-
# @Author:lsf
# @File:managerPage 
# @Time:2021/7/26
# @Email:2795849212@qq.com
# @Software:PyCharm
from selenium.webdriver.common.by import By
from day6.page.basePage import BasePage
from day6.conf.conf import *
import time


class ManagerPage(BasePage):

    def __init__(self):
        super().__init__()
        self.new_project_locater = (By.CSS_SELECTOR, 'a[href="/project/add"]')

        self.project_name = (By.CSS_SELECTOR, '#project-form>div:nth-child(1)>div input')

        # 项目别名
        self.project_alias = (By.CSS_SELECTOR, '#project-form>div:nth-child(2)>div input')

        # 切入 iframe元素
        self.inframe_element = (By.CSS_SELECTOR, '.ke-edit-iframe')

        # 描述里面输入内容
        self.project_desc = (By.CSS_SELECTOR, '.ke-content')

        # 提交按钮
        self.submit = (By.CSS_SELECTOR, 'button:nth-child(2)')

        # 右上角关闭按钮
        self.close = (By.CSS_SELECTOR, '.close')

    # 打开进入管理页面
    def toPage(self):
        url = HOST + '/project/manage'
        self.driver.get(url)

    # 查找新项目元素
    def find_new_project(self):
        time.sleep(5)
        return self.get_element(self.new_project_locater)

    # 项目名称
    def find_project_name(self):
        return self.get_element(self.project_name)

    # 项目别名
    def find_project_alias(self):
        return self.get_element(self.project_alias)

    # 查询 iframe 元素
    def get_iframe(self):
        return self.get_element(self.inframe_element)

    # body元素
    def find_iframe_body(self):
        time.sleep(3)
        return self.get_element(self.project_desc)

    # 点击提交按钮
    def find_submit(self):
        return self.get_element(self.submit)

    # 点击右上角关闭按钮
    def find_close(self):
        return self.get_element(self.close)


class ManagerPageAction(ManagerPage):

    # 初始化的时候让打开
    def open_projce(self):
        self.toPage()

    def find_new_project_click(self):
        self.find_new_project().click()

    def find_project_name_input(self):
        self.find_project_name().send_keys('测试一下')

    def find_project_alias_input(self):
        self.find_project_alias().send_keys('测试一下的一下')

    # 切入 iframe
    def switch_iframe(self):
        self.driver.switch_to.frame(self.get_iframe())

    # 输入内容
    def find_iframe_body_input(self):
        self.find_iframe_body().send_keys('body内容')

    # 点击提交按钮
    def find_submit_click(self):
        self.find_submit().click()

    # 点击左上角关闭按钮
    def find_close_click(self):
        self.find_close().click()


ManagerPageActionObj = ManagerPageAction()


if __name__ == '__main__':
    ManagerPageActionObj.toPage()
    ManagerPageActionObj.find_new_project_click()
    ManagerPageActionObj.find_project_name_input()
    ManagerPageActionObj.find_project_alias_input()
    ManagerPageActionObj.switch_iframe()
    ManagerPageActionObj.find_iframe_body_input()
    # 在退出 iframe
    ManagerPageActionObj.iframe_default()
    ManagerPageActionObj.find_submit_click()
    ManagerPageActionObj.find_close_click()

    # 最后关闭 driver驱动
    ManagerPageActionObj.close_driver()



test_case里面是一些测试用例

test_manager.py里面的内容














from day6.page.managerPage import ManagerPageActionObj
import pytest, os


class TestManager:

    # 初始化打开浏览器
    def setup_class(self):
        ManagerPageActionObj.toPage()

    def test_demo01(self):
        ManagerPageActionObj.find_new_project_click()
        ManagerPageActionObj.find_project_name_input()
        ManagerPageActionObj.find_project_alias_input()
        ManagerPageActionObj.switch_iframe()
        ManagerPageActionObj.find_iframe_body_input()
        # 在退出 iframe
        ManagerPageActionObj.iframe_default()
        ManagerPageActionObj.find_submit_click()

    def teardown_class(self):
        ManagerPageActionObj.find_close_click()


if __name__ == '__main__':
    pytest.main(['-s', 'test_manager.py', '-k demo01', '--alluredir=tmp/report', '--clean-alluredir'])
    os.system('allure serve tmp/report')



ionObj.iframe_default()
ManagerPageActionObj.find_submit_click()

def teardown_class(self):
    ManagerPageActionObj.find_close_click()

if name == ‘main’:
pytest.main([’-s’, ‘test_manager.py’, ‘-k demo01’, ‘–alluredir=tmp/report’, ‘–clean-alluredir’])
os.system(‘allure serve tmp/report’)