Appium的使用學(xué)習(xí)

安裝Appium

  1. 安裝命令

    • npm install -g appium
  2. 安裝完成后,在cmd中輸入appium,如果看到

    [Appium] 歡迎來到 Appium vx.x.x

    說明安裝成功

安裝驅(qū)動(dòng)

  1. 查看可安裝的驅(qū)動(dòng)

    cmd中輸入appium driver list

    appium_driver_list.png
  1. 安裝各驅(qū)動(dòng)

    • 安裝uiautomator2

      cmd中輸入appium driver install uiautomator2

    • 安裝xcuitest

      cmd中輸入appium driver install xcuitest

    • 針對(duì)flutter安裝

      cmd中輸入appium driver install --source=npm appium-flutter-driver

      或者本地安裝,

      cmd中輸入appium driver install --source local /path/to/appium-flutter-driver/driver

    appium_driver_install.png

Appium Inspector

地址配置

Remote Host:127.0.0.1
Remote Port:4723

IPhone配置參數(shù)

{
  "platformName": "ios",
  "appium:automationName": "Flutter",
  "appium:bundleId": "cn.com.ShuShiKeJi.pd",
  "appium:platformVersion": "18.3.1",
  "appium:noReset": true,
  "appium:deviceName": "iPhone",
  "appium:webDriverAgentUrl": "http://localhost:8100",
  "appium:usePrebuiltWDA": false,
  "appium:useXctestrunFile": false,
  "appium:skipLogCapture": true,
  "appium:udid": "00008101-000849813698001E"
}

安卓配置參數(shù)

{
  "platformName": "Android",
  "appium:platformVersion": "15",
  "appium:deviceName": "1C021FDEE00847",
  "appium:automationName": "uiautomator2",
  "appium:appPackage": "com.reality3.realitythird",
  "appium:noReset": false,
  "appium:appActivity": "com.reality3.realitythird.global.global.MainActivity"
}

Appium-Python-Client

封裝類中的驅(qū)動(dòng)對(duì)象生成

from appium.options.ios.xcuitest.base import XCUITestOptions
from appium.options.android.uiautomator2.base import UiAutomator2Options
from appium import webdriver
from thirdReality import caps, logger

class uiAppiumControl:
    def __init__(self):
        """類初始化

        Args:
            deviceid (_type_): 設(shè)備序列號(hào),IOS是UDID,Android是mac地址
        """
        self.driver: webdriver.Remote
        self.setup()

    @staticmethod
    def createIOSDriver():
        """創(chuàng)建IOS驅(qū)動(dòng)options

        Returns:
            _type_: _description_
        """
        options = XCUITestOptions().load_capabilities(dict(caps))
        return options

    @staticmethod
    def createAndroidDriver():
        """創(chuàng)建安卓驅(qū)動(dòng)options

        Returns:
            _type_: _description_
        """
        options = UiAutomator2Options().load_capabilities(dict(caps))
        return options

    def setup(self):
        """創(chuàng)建驅(qū)動(dòng)對(duì)象

        Returns:
            _type_: _description_
        """
        if os_plat == "Android":
            options = uiAppiumControl.createAndroidDriver()
        elif os_plat == "IOS":
            options = uiAppiumControl.createIOSDriver()
        connect_caps = caps
        logger.debug(f"connect_caps: {connect_caps}")
        self.driver = webdriver.Remote("http://127.0.0.1:4723", options=options,keep_alive=False)
        logger.debug(f"self.driver: {self.driver}")
  • 補(bǔ)充:

    1. thirdReality/__init__.py中的caps定義
    if os_plat == "Android":
        caps = {
            "platformName": "Android",
            "platformVersion": androidPhone.platform_version,
            "deviceName": androidPhone.device_id,
            "automationName": "uiautomator2",
            "appPackage": androidPhone.thirdreality_package_name,
            "noReset": True,
            "appActivity": androidPhone.thirdrealtiy_activity,
        }
    
    else:
        caps = {
            "platformName": "iOS",
            "platformVersion": iosPhone.platform_version,
            "automationName": "XCUITest",
            "bundleId": iosPhone.thirdreality_package_name,
            "noReset": True,
            "webDriverAgentUrl": "http://localhost:8100",
            "usePrebuiltWDA": False,
            "useXctestrunFile": False,
            "skipLogCapture": True,
            "udid": iosPhone.device_id,
        }
    

查找元素

find_element

self.uac.driver.find_element(AppiumBy.ACCESSIBILITY_ID,"Privacy Policy")
  • AppiumBy.ACCESSIBILITY_ID是查詢的方式,其他的方式包括

    IOS_PREDICATE = '-ios predicate string'
    IOS_CLASS_CHAIN = '-ios class chain'
    ANDROID_UIAUTOMATOR = '-android uiautomator'
    ANDROID_VIEWTAG = '-android viewtag'
    ANDROID_DATA_MATCHER = '-android datamatcher'
    ANDROID_VIEW_MATCHER = '-android viewmatcher'
    ACCESSIBILITY_ID = 'accessibility id'
    IMAGE = '-image'
    CUSTOM = '-custom'
    
    # For Flutter integration usage https://github.com/AppiumTestDistribution/appium-flutter-integration-driver/tree/main
    FLUTTER_INTEGRATION_SEMANTICS_LABEL = '-flutter semantics label'
    FLUTTER_INTEGRATION_TYPE = '-flutter type'
    FLUTTER_INTEGRATION_KEY = '-flutter key'
    FLUTTER_INTEGRATION_TEXT = '-flutter text'
    FLUTTER_INTEGRATION_TEXT_CONTAINING = '-flutter text containing'
    
  • "Privacy Policy"為需要查詢的元素內(nèi)容


xpath

模糊查詢

  • contains定位方法:用以獲取包含符合要求內(nèi)容的元素

    uac.driver.find_element(AppiumBy.XPATH,'//*[contains(@content-desc,"Sign Up")]').click()
    
  • starts-with定位方法:用以獲取開頭內(nèi)容符合要求的元素

    driver.find_element(AppiumBy.XPATH, '//XCUIElementTypeButton[starts-with(@name,"Settings")]')
    

兄弟定位

由弟弟節(jié)點(diǎn)推出哥哥節(jié)點(diǎn)
  • 通過父節(jié)點(diǎn)獲取其大哥節(jié)點(diǎn)

    driver.find_element_by_xpath("http://div[@id='二哥節(jié)點(diǎn)']/../div[1]").text
    
  • preceding-sibling

    driver.find_element_by_xpath("http://div[@id='二哥節(jié)點(diǎn)']/preceding-sibling::div[1]").text
    
由哥哥節(jié)點(diǎn)推出弟弟節(jié)點(diǎn)
  • xpath,通過父節(jié)點(diǎn)獲取其弟弟節(jié)點(diǎn)

    driver.find_element_by_xpath("http://div[@id='二哥節(jié)點(diǎn)']/../div[3]").text
    
  • following-sibling

    driver.find_element(AppiumBy.XPATH,'//*[contains(@content-desc,"Sign Up")]/following-sibling::android.widget.CheckBox').click()
    
  • Xpath軸 following

    driver.find_element_by_xpath("http://div[@id='二哥節(jié)點(diǎn)']/following::*").text
    

父子定位

  • xpath定位: .代表當(dāng)前節(jié)點(diǎn); '..'代表父節(jié)點(diǎn)

    driver.find_element_by_xpath("http://div[@id='子節(jié)點(diǎn)']/../..").text
    
    • <font color=red>通過find_elements從父節(jié)點(diǎn)獲取所有指定的子節(jié)點(diǎn),并通過下標(biāo)操作子節(jié)點(diǎn)</font>

      uac.driver.find_elements(AppiumBy.XPATH, '//android.view.View[@content-desc="Sign In"]/../android.widget.EditText')[0].click()
      uac.driver.find_elements(AppiumBy.XPATH, '//android.view.View[@content-desc="Sign In"]/../android.widget.EditText')[1].click()
      
  • parent

    driver.find_element_by_xpath("http://div[@id='子節(jié)點(diǎn)']/parent::*/parent::div").text
    

點(diǎn)擊

通過元素點(diǎn)擊

self.uac.driver.find_element(AppiumBy.ACCESSIBILITY_ID,"Privacy Policy").click()

通過坐標(biāo)點(diǎn)擊

指定范圍點(diǎn)擊

driver.tap([(x1, y1), (x2, y2)], duration)
  • [(x1, y1), (x2, y2)]:坐標(biāo)范圍
  • duration:持續(xù)時(shí)間,單位是毫秒,默認(rèn)為None,可以省略

制定坐標(biāo)點(diǎn)擊

driver.tap([(x1, y1)], duration)
  • [(x1, y1)]:坐標(biāo)點(diǎn)
  • duration:持續(xù)時(shí)間,單位是毫秒,默認(rèn)為None,可以省略

問題解決

1. 'NoneType' object has no attribute 'to_capabilities'

  • 問題描述:運(yùn)行代碼self.dev = webdriver.Remote(connect_url,connect_caps)時(shí),報(bào)<font color=red>AttributeError: 'NoneType' object has no attribute 'to_capabilities'</font>的錯(cuò)誤
  • 問題分析:由于python的appium包使用是基于selenium的, 而當(dāng)我們安裝了最新版的selenium和最新版的appium3.0.0, 就會(huì)導(dǎo)致版本沖突問題
  • 解決方案:
    1. 卸載selenium和appium: pip uninstall selenium appium-python-client
    2. 安裝制定的2.0版本appium:pip install appium-python-client==2.0
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容