移動端測試——APP元素定位操作 (3)


一、appium基礎API講解

1.1 APP元素定位操作

建議:

  • 使用顯示等待
  • 能用id、class定位就不用xpath定位
  • 只要看的見的,用xpath的text就是萬能的
  • 手工測試主要通過可見按鈕操作,而自動化是通過元素進行交互操作
  • 元素的基本定位基於當前屏幕范圍內展示的可見元素
  • 前置代碼
# 導入driver對象
from appium import webdriver

import time

# server 啟動參數
desired_caps = {}
# 設備信息(系統、版本、設備號)
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '9'
desired_caps['deviceName'] = '192.168.72.103:5555'
# app信息(包名、啟動名)
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'

# 聲明driver對象
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

time.sleep(5)

# 關閉驅動對象
driver.quit()

1.1.1 Appium常用元素定位方式

名稱
id id屬性值
class class屬性值
xpath xpath表達式
  • 定位一組元素,注意element -> elements

應用場景為元素值重復,無法通過元素屬性直接定位到某個元素,只能通過elements方式來選擇,返回一個定位對象的列表

1-1. 通過id定位

方法:id_value:為元素的id屬性值
find_element_by_id(id_value)

eg(業務場景):

進入設置頁面后,通過id定位方式點擊搜索按鈕

driver.find_element_by_id("com.android.settings:id/search").click()
driver.quit()

1-2. 通過id定位一組元素

方法:id_value:為元素的id屬性值
find_elements_by_id(id_value)

eg:

查看返回值類型

title = driver.find_elements_by_id("android:id/title")
print(type(title))
print(title)

查看一組元素的text

title = driver.find_elements_by_id("android:id/title")
for i in title:
    print(i.text)

點擊"電池"

title = driver.find_elements_by_id("android:id/title")
    for i in title:
        if i.text == "電池":
            i.click()

點擊"電池"(改進)

title = driver.find_elements_by_id("android:id/title")
    for i in title:
        if i.text == "電池":
            i.click()
            time.sleep(2)
            break

eg(業務場景):

進入設置頁面后,點擊WLAN菜單欄(id定位對象列表中第1個)

 # 定位到一組元素
title = driver.find_elements_by_id("com.android.settings:id/title")
# 打印title類型,預期為list
print(type(title))
# 取title返回列表中的第一個定位對象,執行點擊操作
title[0].click()

2-1. 通過class定位

方法:class_value:為元素的class屬性值
find_element_by_class_name(class_value)

eg(業務場景):

進入設置頁面后,點擊搜索按鈕,通過class定位方式點擊輸入框的返回按鈕

 # id 點擊搜索按鈕
driver.find_element_by_id("com.android.settings:id/search").click()
# class 點擊輸入框返回按鈕
driver.find_element_by_class_name('android.widget.ImageButton').click()
driver.quit()

2-2. 通過class定位一組元素

方法:class_value:為元素的class屬性值
find_elements_by_class_name(class_value)

點擊"電池"

title = driver.find_elements_by_class_name("android.widget.TextView")
    for i in title:
        print(i.text)
        if i.text == "電池":
            i.click()
            time.sleep(2)
            break

點擊"電池"(改進)

title = driver.find_elements_by_class_name("android.widget.TextView")
    for i in title:
        print(i.text)
        if "電池" in i.text:
            i.click()
            time.sleep(2)
            break

eg(業務場景):

進入設置頁面后,點擊WLAN菜單欄(class定位對象列表中第3個)

 # 定位到一組元素
title = driver.find_elements_by_class_name("android.widget.TextView")
# 打印title類型,預期為list
print(type(title))
# 取title返回列表中的第一個定位對象,執行點擊操作
title[3].click()

3-1. 通過xpath定位

方法:xpath_value:為可以定位到元素的xpath語句
find_element_by_xpath(xpath_value)

  • android端xptah常用屬性定位
屬性定位 說明
id //*[contains(@resource-id,'com.android.settings:id/search')]
class //*[contains(@class,'android.widget.ImageButton')]
text //*[contains(@text,'WLA')]

模糊定位:
contains(@key,value):(value可以是部分值)

定位父節點:
/..

父節點應用說明:

  • 可以通過用元素的父節點的坐標信息進行比較,判斷元素的相對位置

eg:

eg:
//*[contains(@resource-id,'com.android.settings:id/search')]/..

eg(業務場景):

進入設置頁面后,點擊WLAN菜單欄

# xpath 點擊WLAN按鈕
driver.find_element_by_xpath("//*[contains(@text,'WLA')]").click()

3-2. 通過xpath定位一組元素

方法:xpath_value:為可以定位到元素的xpath語句
find_elements_by_xpath(xpath_value)

點擊"電池"

xpath_value = "//*[contains(@class,'android.widget.TextView')]"
    title = driver.find_elements_by_xpath(xpath_value)
    for i in title:
        print(i.text)
        if "電池" in i.text:
            i.click()
            time.sleep(2)
            break

eg(業務場景):

進入設置頁面后,點擊WLAN菜單欄(xpath中class屬性定位對象列表中第3個)

 # 定位到一組元素
title = driver.find_elements_by_xpath("//*[contains(@class,'widget.TextView')]")
# 打印title類型,預期為list
print(type(title))
# 取title返回列表中的第一個定位對象,執行點擊操作
title[3].click()

4. WebDriverWait 顯示等待操作

在一個超時時間范圍內,每隔一段時間去搜索一次元素是否存在,如果存在返回定位對象,如果不存在直到超時時間到達,報超時異常錯誤

方法:WebDriverWait(driver, timeout, poll_frequency).until(method)

參數 說明
driver 手機驅動對象
timeout 搜索超時時間
poll_frequency 每次搜索間隔時間,默認時間為0.5s
method 定位方法(匿名函數)
  • 匿名函數:
    lambda x: x

等價於python函數:

def test(x):
return x

eg:

WebDriverWait(driver, timeout, poll_frequency).until(lambda x: x.find_elements_by_id(id_value))

解釋:
➢ x傳入值為:driver,所以才可以使用定位方法

函數運行過程:
➢ 實例化WebDriverWait類,傳入driver對象,之后driver對象被賦值給WebDriverWait的一個類變量:self._driver
➢ until為WebDriverWait類的方法,until傳入method方法(即匿名函數),之后method方法會被傳入self._driver
➢ 搜索到元素后until返回定位對象,沒有搜索到函數until返回超時異常錯誤

eg(業務場景):

進入設置頁面后,通過ID定位方式點擊搜索按鈕

from selenium.webdriver.support.wait import WebDriverWait # 導入WebDriverWait類
# 超時時間為30s,每隔1秒搜索一次元素是否存在,如果元素存在返回定位對象並退出
search_button = WebDriverWait(driver, 3, 0.5).until(lambda x: x.find_elements_by_id(com.android.settings:id/search))
search_button.click()
driver.quit()

其他:

  • 打印時間
    print(time.strftime("%H:%M:%S", time.localtime()))

1.1.2 綜合應用

  • 關閉移動數據

更新中......



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM