Appium Python 常用元素定位方法&測試小米計算器實例

常用的元素定位方法

Uiautomator 定位

image.png

text屬性的方法

#text
driver.find_element_by_android_uiautomator('new UiSelector().text("Custom View")').click()   

#textContains      
driver.find_element_by_android_uiautomator('new UiSelector().textContains("View")').click()        

#textStartsWith
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("Custom")').click()  

#textMatches  
driver.find_element_by_android_uiautomator('new UiSelector().textMatches("^Custom.*")').click()    

class屬性的方法

#className
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.TextView").text("Custom View")').click()     

#classNameMatches
driver.find_element_by_android_uiautomator('new UiSelector().classNameMatches(".*TextView$").text("Custom View")').click()         

resourceId屬性的方法

#resourceId
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("android:id/text1")')    

#resourceIdMatches
driver.find_element_by_android_uiautomator('new UiSelector().resourceIdMatches(".*id/text1$")')

元素的其他屬性

driver.find_element_by_android_uiautomator('new UiSelector().clickable(true).text("Custom View")').click()

ID定位

# resourceId屬性的方法
driver.find_element_by_id("com.miui.calculator:id/btn_plus").click()

#以accessibility_id進行定位,對Android而言,就是content-description屬性
driver.find_element_by_accessibility_id('push_button').click() 

ClassName 定位

# 定位唯一元素  
self.driver.find_element_by_class_name("android.widget.EditText")
 
# 找到所有android.widget.EditText并定位第一個
self.driver.find_elements_by_class_name("android.widget.EditText")[0]

Name定位

#根據name進行定位,對于android來說,就是text屬性

driver.find_element_by_name("1").click() 

舉例 小米自帶計算器測試1

#coding=utf-8
from appium import webdriver
import sys
print "here is :",__file__,sys._getframe().f_lineno #打印行數,調試用
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = "7.0"
desired_caps['deviceName'] = '777ddad30504'
desired_caps['appPackage'] = 'com.miui.calculator'
desired_caps['appActivity'] = '.cal.CalculatorActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.find_element_by_android_uiautomator('new UiSelector().text("1")').click()
driver.find_element_by_id("com.miui.calculator:id/btn_plus").click()
driver.find_element_by_name("5").click()
driver.find_element_by_id('com.miui.calculator:id/btn_equal').click()
driver.quit()

定位方法比較
1· driver.find_element_by_name("5").click()
2· driver.find_element_by_id("com.miui.calculator:id/btn_5").click()
3· driver.find_element_by_android_uiautomator('new UiSelector().text("5")').click()

比較三種定位方法,第二種by_id最可靠,推薦。

原因分析: 可能有重復的text值,如下圖,已經輸入的1和數字鍵盤的1的text值都是1,因此按照 by_name和text查找會出錯、


參考鏈接: Appium+Python 自動化-appium常用元素定位方法

舉例 小米計算器測試2

[轉] appium-4:測試小米手機自帶的計算器

#coding=utf-8
from appium import webdriver
import unittest

class TesXiaomiCalc(unittest.TestCase):
    def setUp(self):
        # set up appium
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = "7.0"
        desired_caps['deviceName'] = '777ddad30504'
        desired_caps['appPackage'] = 'com.miui.calculator'
        desired_caps['appActivity'] = '.cal.CalculatorActivity'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

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

    def test_plus(self):
        #清空環境
        self.driver.find_element_by_id("com.miui.calculator:id/btn_c_1").click()

        calc = '159+95+5=259'
        ca, res = calc.split('=')
        for c in ca:
            if c == '+':
                c = 'plus'
            if c == '0':
                c = '0_p'
            self.driver.find_element_by_id("btn_%s" % c).click()  # 依次輸入159 + 95 + 5 =
        self.driver.find_element_by_id("btn_equal").click()

        realStr = self.driver.find_element_by_android_uiautomator(\
            'new UiSelector().className("android.widget.TextView").instance(6)').text #6為259的位置
        self.assertEqual(res, realStr)
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TesXiaomiCalc)
    unittest.TextTestRunner(verbosity=2).run(suite) 

遇到的問題: 計算結果使用下面方法無法定位,計算結果數值,無id,className不唯一,作者使用了下面方法,失敗。

    realStr=self.driver.find_elements_by_class_name("android.widget.TextView")[-2].text

解決辦法:

    realStr = self.driver.find_element_by_android_uiautomator(\
        'new UiSelector().className("android.widget.TextView").instance(6)').text

關于instance(6) 中“6”定位方法:

參考文檔: 使用uiautomator做UI測試

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容