Appium 公开课_PageObject 模式实战_20190820

思寒_seveniruby for 霍格沃兹测试学院外部交流圈 · August 20, 2019 · Last by S大魔导 replied at August 21, 2019 · 1099 hits

晚上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

公开课介绍

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

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

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

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

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up