学习笔记——测试进阶之路 学习笔记:PO 模式设计及代码应用 (下)

大海 · 2020年04月24日 · 939 次阅读

测试数据

  • Appium Desktop V1.15.1
  • PyCharm Community 2019.2
  • 夜神模拟器 V6.6.0.2
  • 雪球股票 APK V12.4.6

PO 模式架构

PO 模式解析:

base 类:经常需要调用的元素定位方法
page 类:各个页面下的函数方法
testcase 类:各个测试用例集合

PO 模式代码

BASE 类
# !/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@File    : base.py
@Time    : 2020/02/22
"""


class Base():
    def __init__(self, driver):
        self.driver = driver

    def find_ID(self, ID):
        return self.driver.find_element_by_id(ID)

    def find_XPATH(self, XPATH):
        return self.driver.find_element_by_xpath(XPATH)

PAGE 类
  • main_page 页
# !/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@File    : main_page.py
@Time    : 2020/02/22
"""

from testPO5.base.base import Base
from testPO5.page.search_page import SearchPage


class MainPage(Base):
    textview = "//android.widget.TextView[@text='同意']"
    search_input_box = "com.xueqiu.android:id/tv_search"

    def click_search(self):
        self.find_XPATH(self.textview).click()
        self.find_ID(self.search_input_box).click()
        return SearchPage(self.driver)
  • search_page 页
# !/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@File    : search_page.py
@Time    : 2020/02/22
"""

from testPO5.base.base import Base

class SearchPage(Base):
    search_input = "com.xueqiu.android:id/search_input_text"
    alibaba = "//android.widget.TextView[@text='阿里巴巴']"
    price = "com.xueqiu.android:id/current_price"
    add_me = "com.xueqiu.android:id/follow_btn"

    def input_search_box(self, search_key):
        self.find_ID(self.search_input).send_keys(search_key)
        return self

    def click_search_key(self):
        self.find_XPATH(self.alibaba).click()
        return self

    def get_price(self):
        current_price = float(self.find_ID(self.price).text)
        return current_price

    def addseleciton(self):
        self.find_ID(self.add_me).click()
        return self

TestCases 类
# !/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@File    : search_cases.py
@Time    : 2020/02/22
"""

from appium import webdriver
from testPO5.page.main_page import MainPage


class TestSearchFunc():
    def setup(self):
        desire_cap = {}
        desire_cap['platformName'] = 'Android'
        desire_cap['platformVersion'] = '5.1.1'
        desire_cap['deviceName'] = 'emulator-5554'
        desire_cap['appPackage'] = 'com.xueqiu.android'
        desire_cap['appActivity'] = 'view.WelcomeActivityAlias'
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desire_cap)
        self.driver.implicitly_wait(10)

    def teardown(self):
        self.driver.quit()

    def test_search_01(self):
        mp = MainPage(self.driver)
        print(mp.click_search().input_search_box("alibaba").click_search_key().get_price())

    def test_search_02(self):
        mp = MainPage(self.driver)
        mp.click_search().input_search_box("alibaba").click_search_key().addseleciton()

运行过程

测试结果

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册