Appium移動自動化測試(三)之元素定位

來源:互聯網
上載者:User

標籤:res   mail   它的   host   程式   簡介   quit   缺陷   python   

實驗簡介

  做過UI自動化(web自動化, 移動自動化)的同學都會知道, 除去架構的選型和搭建以外, 落到實處的對元素進行定位就成了最重要的技能. 

  做過UI自動化的同學會知道, 對頁面元素的定位方式有8種: id, name, xpath, class, link text, partial link text, tag name, css selector, 使用手法, 優先順序的選擇以及效率稍有不同, 但是殊途同歸, 目標就是要找到元素本身. 

  同樣, 要做Native APP的自動化, 在元素定位上也大同小異, web 元素通用的方式, Appuim也適用, 甚至它還有一些個人化的方式.  但是對於Web View容器下的元素識別, 和web driver並無太多差異

實驗目的

  1. 練習使用工具uiautomatorviewer對元素進行定位

  2. 熟悉元素定位的方法

  3. 編寫自動化測試案例 - 新增連絡人...

實驗流程

  1. 首先來看一下測試對象, 一個新增連絡人...的app, 功能頁面如排序.

 

   2. 在apk/tools下, 找到uiautomatorviewer.bat

    

    點擊如標, 同步App頁面元素詳細資料

    

    3. 從目前的頁面上, 我們可以得到一些屬性, 其對應關係如:

    

 

    4. 瞭解了這些基本屬性, 其實就可以完成代碼的編寫了, 如下

#coding=utf-8from appium import webdriverimport timefrom selenium.webdriver.common.by import Bydesired_caps = {}desired_caps[‘platformName‘] = ‘Android‘desired_caps[‘platformVersion‘] = ‘23‘desired_caps[‘deviceName‘] = ‘Android Emulator‘desired_caps[‘appPackage‘] = ‘com.example.android.contactmanager‘desired_caps[‘appActivity‘] = ‘.ContactManager‘driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps)#   點擊Add Contactdriver.find_element_by_name(‘Add Contact‘).click()  #text#   輸入 Contact Namename = ‘test03‘driver.find_element_by_id(‘com.example.android.contactmanager:id/contactNameEditText‘).send_keys(name)  #resource-id#   輸入 Contact Phonedriver.find_element_by_id(‘com.example.android.contactmanager:id/contactPhoneEditText‘).send_keys(‘13111111111‘)#   輸入 Contact Emaildriver.find_element_by_id(‘com.example.android.contactmanager:id/contactEmailEditText‘).send_keys(‘[email protected]‘)#   點擊 Save Buttondriver.find_element_by_name(‘Save‘).click()#   點擊 "Show Invisible Contacts(Only)"driver.find_element_by_accessibility_id(‘Show Invisible Contacts (Only)‘).click()nameAdded = driver.find_element_by_name(name).textprint(nameAdded)if nameAdded == name:    print(‘成功新增連絡人....‘)else:    print(‘新增連絡人...失敗‘)driver.quit()

    Accessibility ID在Android上面就等同於contentDescription,這個屬性是方便一些生理功能有缺陷的人使用應用程式的。比如我們有一個ImageView裡面放置一張顏色複雜的圖片,可能一些色弱色盲的人,分不清這張圖片中畫的是什麼東西。如果使用者安裝了輔助瀏覽工具比如TalkBack,TalkBack就會大聲朗讀出使用者目前正在瀏覽的內容。TextView控制項TalkBack可以直接讀出裡面的內容,但是ImageView TalkBack就只能去讀contentDescription的值,告訴使用者這個圖片到底是什麼。

  5. 上面用到了最常用的幾種定位方式, 心心念念的強大的xpath還沒的提到, 我們來看看它的幾種基本用法

    1).使用絕對路徑定位,如所顯示的 xpath 路徑
      find_element_by_xpath("className/className/className/className")
    2).使用相對路徑定位
      find_element_by_xpath("//className")
    3).通過元素的索引定位
      find_element_by_xpath("//className[index]")
    4).通過元素的屬性定位
      一種屬性:find_element_by_xpath("//className[@label=‘更多資訊‘]")
      兩種屬性:find_element_by_xpath("//className[@label=‘更多資訊‘][@isVisible=‘1‘]")
      部分屬性(最強大):find_element_by_xpath("//className[contains(@label,‘更多‘)]")

#   輸入 Contact Namename = ‘test03‘driver.find_element_by_xpath(‘//android.widget.EditText["3"]‘).send_keys(name)

  6. 使用android uiautomator定位

    這個方法也屬於 Appium(Android)擴充的定位方法。同樣使用 UIAutomatorViewer.bat 工具直接查看。

    也就是說一個元素的任意屬性都可以通過android uiautomator方法來進行定位,但要保證這種定位方式的唯一性。

#  點擊 "Show Invisible Contacts(Only)"driver.find_element_by_android_uiautomator("new UiSelector().text(\"Show Invisible Contacts (Only)\")").click()# description對應的屬性是content-desc# driver.find_element_by_android_uiautomator("new UiSelector().description(\"Show Invisible Contacts (Only)\")").click()  

    順便貼一下UiSelector的其它方法, 依然很好用.

    文本方面的方法:
      1.text(String text) 文本
      2.textContains(String text) 文本包含
      3.textMatches(String regex) 文本正則
      4.textStartsWith(String text) 文本開始字元

    描述方面的方法:
      1.description(String desc) 描述
      2.descriptionContains(String desc) 描述包含
      3.descriptionMatches(String regex) 描述正則
      4.descriptionStartsWith(String desc) 描述開始字元

driver.find_element_by_android_uiautomator("new UiSelector().descriptionContains(\"Show Invisible Contacts\")").click()

  曾哥有話說:

    方法find_element_by_android_uiautomator裡的語句是用java處理, 它的字串是放在雙引號中的. 如果把字串放在單引號中的話, 在python裡看起來沒有問題, 但是java卻並不認為這是一個字串. 錯誤資訊在appuim server上可以清楚地看到:

    

    這個坑挺隱形的, 我趴了好久, 差點以為 該方法在此處不適用, 很難過.

    

 

    

Appium移動自動化測試(三)之元素定位

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.