PO模式

把一个页面分为三层,对象库层、操作层、业务层。
对象库层:封装定位元素的方法。
操作层:封装对元素的操作。
业务层:将一个或多个操作组合起来完成一个业务功能。比如登录:需要输入帐号、密码、点
击登录三个操作。
例子:计算器
原始
import time
import unittest
from time import sleep
from parameterized import parameterized
from selenium import webdriver
from selenium.webdriver.common.by import By


class TestCalc(unittest.TestCase):
    driver = None

    @classmethod
    def setUpClass(cls):
        # 实例化浏览器
        cls.driver = webdriver.Chrome()
        # 打开浏览器
        url = 'http://cal.apple886.com/'
        cls.driver.get(url)
        cls.driver.maximize_window()

    @classmethod
    def tearDownClass(cls):
        # 关闭
        cls.driver.quit()
        cls.driver = None

    # 加法
    def test_Add(self, num1=20, num2=10, expect=30):
        # 遍历传进来的数字,并强制转换为字符串
        # 第一个数
        for n in str(num1):
            # 拆开单个按钮的定位方式
            self.driver.find_element(By.CSS_SELECTOR, "#simple{}".format(n)).click()

        # 点击加法按钮
        self.driver.find_element(By.CSS_SELECTOR, "#simpleAdd").click()

        # 第二个数
        for n in str(num2):
            # 拆开单个按钮的定位方式
            self.driver.find_element(By.CSS_SELECTOR, "#simple{}".format(n)).click()

        # 点击等号
        self.driver.find_element(By.CSS_SELECTOR, "#simpleEqual").click()

        # 获取结果
        result = self.driver.find_element(By.CSS_SELECTOR, "#resultIpt").get_attribute("value")

        print("实际结果:{},预期结果:{}".format(result, expect))
        try:
            # 断言
            self.assertEqual(result, str(expect))
        except AssertionError:
            # 如果实际结果不等于预期结果 截图
            self.driver.get_screenshot_as_file('./{}.png'.format(time.strftime("%Y_%m_%d %H_%M_%S")))

    # 减法
    def test_Subtr(self, num1=20, num2=10, expect=10):
        # 遍历传进来的数字,并强制转换为字符串
        # 第一个数
        for n in str(num1):
            # 拆开单个按钮的定位方式
            self.driver.find_element(By.CSS_SELECTOR, "#simple{}".format(n)).click()

        # 点击减法按钮
        self.driver.find_element(By.CSS_SELECTOR, "#simpleSubtr").click()
        # 第二个数
        for n in str(num2):
            # 拆开单个按钮的定位方式
            self.driver.find_element(By.CSS_SELECTOR, "#simple{}".format(n)).click()

        # 点击等号
        self.driver.find_element(By.CSS_SELECTOR, "#simpleEqual").click()

        # 获取结果
        result = self.driver.find_element(By.CSS_SELECTOR, "#resultIpt").get_attribute("value")

        print("实际结果:{},预期结果:{}".format(result, expect))
        try:
            # 断言
            self.assertEqual(result, str(expect))
        except AssertionError:
            # 截图
            self.driver.get_screenshot_as_file('./{}.png'.format(time.strftime("%Y_%m_%d %H_%M_%S")))

    # 乘法
    def test_Multi(self, num1=20, num2=10, expect=200):
        # 遍历传进来的数字,并强制转换为字符串
        # 第一个数
        for n in str(num1):
            # 拆开单个按钮的定位方式
            self.driver.find_element(By.CSS_SELECTOR, "#simple{}".format(n)).click()

        # 点击乘法按钮
        self.driver.find_element(By.CSS_SELECTOR, "#simpleMulti").click()
        # 第二个数
        for n in str(num2):
            # 拆开单个按钮的定位方式
            self.driver.find_element(By.CSS_SELECTOR, "#simple{}".format(n)).click()

        # 点击等号
        self.driver.find_element(By.CSS_SELECTOR, "#simpleEqual").click()

        # 获取结果
        result = self.driver.find_element(By.CSS_SELECTOR, "#resultIpt").get_attribute("value")

        print("实际结果:{},预期结果:{}".format(result, expect))
        try:
            # 断言
            self.assertEqual(result, str(expect))
        except AssertionError:
            #  截图
            self.driver.get_screenshot_as_file('./{}.png'.format(time.strftime("%Y_%m_%d %H_%M_%S")))

    # 除法
    def test_Divi(self, num1=20, num2=10, expect=2):
        # 遍历传进来的数字,并强制转换为字符串
        # 第一个数
        for n in str(num1):
            # 拆开单个按钮的定位方式
            self.driver.find_element(By.CSS_SELECTOR, "#simple{}".format(n)).click()

        # 点击除法按钮
        self.driver.find_element(By.CSS_SELECTOR, "#simpleDivi").click()
        # 第二个数
        for n in str(num2):
            # 拆开单个按钮的定位方式
            self.driver.find_element(By.CSS_SELECTOR, "#simple{}".format(n)).click()
        # 点击等号
        self.driver.find_element(By.CSS_SELECTOR, "#simpleEqual").click()

        # 获取结果
        result = self.driver.find_element(By.CSS_SELECTOR, "#resultIpt").get_attribute("value")

        print("实际结果:{},预期结果:{}".format(result, expect))
        try:
            # 断言
            self.assertEqual(result, str(expect))
        except AssertionError:
            #  截图
            self.driver.get_screenshot_as_file('./{}.png'.format(time.strftime("%Y_%m_%d %H_%M_%S")))

po模式
结构:
1.base(基类):page页面一些公共的方法。
2.page(页面对象):一个页面封装成一个对象。
3.script(业务层):导包调用page页面
base.py 基类:封装公共的方法。
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

#封装一些公共的方法

class Base:
    # 初始化方法
    def __init__(self, driver):
        self.driver = driver

    # 查找元素
    def base_find_element(self, loc, timeout=30, poll=0.5):
        """
        :param loc:  元素的定位信息
        :param timeout: 默认超时时间30秒
        :param poll: 访问频率 0.5秒查找一次元素
        :return:
        """
        # 显示等待
        return WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll).until(lambda x: x.find_element(*loc))

    # 点击
    def base_click(self, loc):
        # 先查找元素,然后点击
        self.base_find_element(loc).click()

    # 获取value属性
    def base_get_value(self, loc):
        # #get_attribute() 获取指定元素属性值
        return self.driver.find_element(*loc).get_attribute("value")

    # 截图
    def base_get_img(self):
        self.driver.get_screenshot_as_file('../img/{}.jpg'.format(time.strftime("%Y_%m_%d %H_%M_%S")))

get_driver.py 获取驱动类

from selenium import webdriver

from day07.cal import page


class GetDriver:
    # 设置类属性
    driver = None

    # 获取
    @classmethod
    def get_driver(cls):
        if cls.driver is None:
            # 实例化浏览器
            cls.driver = webdriver.Chrome()
            # 打开浏览器
            cls.driver.get(page.url)
            cls.driver.maximize_window()
        return cls.driver

    # 关闭
    @classmethod
    def quit_driver(cls):
        if cls.driver is not None:
            cls.driver.quit()
            # 置空
            cls.driver = None

page_calc.py 对元素的操作

from selenium.webdriver.common.by import By
from day07.cal import page
from day07.cal.base.base import Base


class PageCalc(Base):
    # 点击数字
    def page_click_num(self, num):
        # 遍历传进来的数字,并强制转换为字符串
        for n in str(num):
            # 拆开单个按钮的定位方式
            loc = By.CSS_SELECTOR, "#simple{}".format(n)
            self.base_click(loc)

    # 点击运算符
    def page_click_operator(self, operator):
        if operator == 'Add':
            self.base_click(page.calc_Add)
        elif operator == 'Subtr':
            self.base_click(page.calc_Subtr)
        elif operator == 'Multi':
            self.base_click(page.calc_Multi)
        elif operator == 'Divi':
            self.base_click(page.calc_Divi)

    # 点击等号
    def page_click_equal(self):
        self.base_click(page.calc_equal)

    # 获取结果
    def page_get_value(self):
        return self.base_get_value(page.calc_result)

    # 清屏
    def page_get_clear(self):
        self.base_click(page.calc_clear)

    # 截图
    def page_get_img(self):
        self.base_get_img()

    # 组装
    def page_calc(self, num1, num2, operator):
        self.page_click_num(num1)
        self.page_click_operator(operator)
        self.page_click_num(num2)
        self.page_click_equal()

__init__.py 初始化数据

"""服务器域名配置地址"""
url = 'http://cal.apple886.com/'

"""计算器配置数据"""
from selenium.webdriver.common.by import By

# 加
calc_Add = By.CSS_SELECTOR, "#simpleAdd"
# 减
calc_Subtr = By.CSS_SELECTOR, "#simpleSubtr"
# 乘
calc_Multi = By.CSS_SELECTOR, "#simpleMulti"
# 除
calc_Divi = By.CSS_SELECTOR, "#simpleDivi"

# 等
calc_equal = By.CSS_SELECTOR, "#simpleEqual"

# 获取结果
calc_result = By.CSS_SELECTOR, "#resultIpt"

# 清屏
calc_clear = By.CSS_SELECTOR, "#simpleClearAllBtn"


test_calc.py 业务层:调用page页面的功能 进行测试

import unittest
from time import sleep
from parameterized import parameterized
from day07.cal.base.get_driver import GetDriver
from day07.cal.page.page_calc import PageCalc
import json


def data_get():
    #读数据
    with open("../data/calc.json", encoding="utf-8") as f1:
        datas = json.load(f1)

    new_list = []
    for i in datas.values():
        new_list.append((i["num1"], i["num2"], i["operator"], i["expect"]))
    return new_list


class TestCalc(unittest.TestCase):
    driver = None

    @classmethod
    def setUpClass(cls):
        # 获取driver
        cls.driver = GetDriver.get_driver()
        # 初始化 计算页面对象
        cls.calc = PageCalc(cls.driver)

    @classmethod
    def tearDownClass(cls):
        # 关闭
        GetDriver.quit_driver()

    # 测试加法
    @parameterized.expand(data_get())
    def test_calc_add(self, num1, num2, operator, expect):

        self.calc.page_calc(num1, num2, operator)
        # 获取结果
        result = self.calc.page_get_value()
        print("实际结果:{},预期结果:{}".format(result, expect))
        try:
            # 断言
            self.assertEqual(result, str(expect))
        except AssertionError:
            # 异常 截图
            self.calc.page_get_img()

测试数据

calc.json

{
  "calc_001": {
    "num1": 1,
    "num2": 1,
    "operator": "Add",
    "expect": 2
  },
  "calc_002": {
    "num1": 20,
    "num2": 10,
    "operator": "Subtr",
    "expect": 10
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值