Appium 公开课_PageObject 模式实战_20190820

思寒_seveniruby · 2019年08月20日 · 最后由 Keyens 回复于 2022年05月20日 · 3838 次阅读

晚上 8:00 开始

课前准备

获取 app 的入口

adb logcat | grep Displayed
08-20 20:14:19.034   439   473 I ActivityManager: Displayed com.mumu.launcher/.Launcher: +2s236ms
08-20 20:16:00.097   439   473 I ActivityManager: Displayed com.xueqiu.android/.view.WelcomeActivityAlias: +1s927ms
08-20 20:16:07.040   439   473 I ActivityManager: Displayed com.xueqiu.android/.common.MainActivity: +2s142ms

appium desktop 的配置

{
  "platformName": "android",
  "deviceName": "hogwarts",
  "appPackage": "com.xueqiu.android",
  "appActivity": ".view.WelcomeActivityAlias",
  "autoGrantPermissions": "true"
}

录制并生成基础用例

# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver

caps = {}
caps["platformName"] = "android"
caps["deviceName"] = "hogwarts"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"
caps["autoGrantPermissions"] = "true"

driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)

el1 = driver.find_element_by_id("com.xueqiu.android:id/tv_search")
el1.click()
el2 = driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
el2.send_keys("alibaba")
el3 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.TextView")
el3.click()

driver.quit()

unitest 测试用例改造

# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver
import pytest
import unittest

class TestPO(unittest.TestCase):
    def setUp(self):

        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "hogwarts"
        caps["appPackage"] = "com.xueqiu.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps["autoGrantPermissions"] = "true"

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(5)

    def test_search(self):

        el1 = self.driver.find_element_by_id("com.xueqiu.android:id/tv_search")
        el1.click()
        el2 = self.driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
        el2.send_keys("alibaba")
        el3 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.TextView")
        el3.click()

    def tearDown(self) -> None:
        self.driver.quit()

pytest 风格的测试用例

# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver
import pytest

class TestPO:
    def setup(self):

        caps = {}
        caps["platformName"] = "android"
        caps["deviceName"] = "hogwarts"
        caps["appPackage"] = "com.xueqiu.android"
        caps["appActivity"] = ".view.WelcomeActivityAlias"
        caps["autoGrantPermissions"] = "true"

        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
        self.driver.implicitly_wait(5)

    def test_search(self):

        el1 = self.driver.find_element_by_id("com.xueqiu.android:id/tv_search")
        el1.click()
        el2 = self.driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
        el2.send_keys("alibaba")
        el3 = self.driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout[1]/android.widget.TextView")
        el3.click()

    def teardown(self) -> None:
        self.driver.quit()

PO



PO 框架封装

  • 测试用例可以写
  • 界面的主功能已经设计完成,但是还没实现

实现 Page

import pytest
from appium import webdriver

from page.xueqiu_page import XueqiuPage


class TestSearch:

    def setup(self):
        self.xueqiu=XueqiuPage()

    @pytest.mark.parametrize("keyword", [
        "alibaba",
        "xiaomi",
        "pingan",
        "pdd",
        "facebook"
    ])
    def test_search(self, keyword):
        assert self.xueqiu.search()\
                   .search(keyword)\
                   .get_price() == 100
    def test_search_xiaomi(self):
        assert self.xueqiu.search().search("xiaomi").get_price() == 100

公开课介绍

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 5 条回复 时间 点赞

最近一直在思考两个问题:

  • Selenium 自带的 PageFactory/虫师的 poium 框架在编写页面元素定位符的时候会比较简洁、逻辑也比较清晰。实际工作中应用这种模式的问题在哪里呢?
  • 能否实现一套 PO 框架模型,以作为不同项目 PO 模型封装的脚手架?

上面的 pytest 风格的测试用例是不是粘错了?

@Q poium 目前支持 selenium 和 appium ,最近将这种模式应用到了 facebook-wda 中,理论上也支持 uiautomator2 ,我想可以使用这种模式来兼容目前主流的测试库。

最近在用 poium,发现没有判断一个元素是否存在的方法?是我没找到吗?
已经找到方法了,之前是我用错了😂

poium 库里有个 Elements 的类,里面 find 方法要怎么调用。有没有例子?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册