Appium+python移動自動化測試(四)--Monitor/uiautomatorviewer工具及元素定位方法

來源:互聯網
上載者:User

標籤:進入   dex   常用方法   coding   inf   機器人   form   driver   表徵圖   

寫在前面:

本文介紹識別元素的工具和元素的定位方法,識別元素的工具主要有uiautomator和monitor,以及Appium Inspector(Windows下小編在1.7.0之後版本可使用,1.4.16版本不行),本系列教程用的appium1.4.16本文,故暫不介紹Appium Inspector。

一、常用的識別元素的工具介紹

  1. 啟動uiautomatorviewer.bat/monitor.bat。開啟sdk/tools目錄找到uiautomatorviewer.bat/monitor.bat.

     

  2. 雙擊後啟動,啟動後



二、Monitor串連手機

  1. win+R快速鍵輸入cmd開啟,輸入adb devices,確認當前有裝置接入。
  2. 開啟魔漫相機主介面,在Monitor介面點擊手機機器人小表徵圖,此按鈕每次都會重新整理最新介面

三、定位元素

  1. 移動滑鼠到需要定位的元素地區,並點擊一下,使紅色實線條框住。點擊按鈕進漫畫

    Node Detail裡能看到元素對應的屬性
    resource-id:com.manboker.headportrait:id/entry_album_set
    class:android.widget.ImageView
    index 介面有多個相同屬性時使用

四、點擊我的按鈕

  通過resource-id定位並點擊.最終代碼如下:

#coding:utf-8import timefrom appium import webdriverdesired_caps = {}desired_caps[‘deviceName‘] = ‘192.168.199.101:5555‘desired_caps[‘platformName‘] = ‘Android‘desired_caps[‘platformVersion‘] = ‘5.0‘desired_caps[‘appPackage‘] = ‘com.manboker.headportrait‘desired_caps[‘appActivity‘] = ‘com.manboker.headportrait.activities.FirstActivity‘driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps)time.sleep(8)#休眠8秒driver.find_element_by_id(‘com.manboker.headportrait:id/entry_album_set‘).click()#點擊進入我的頁面driver.quit()

五、Appium工具簡單使用介紹

  1. 雙擊appium.exe開啟,進setting介面。如下1是被測apk的路徑,2查看app的包名及Launch Activity,3是被測app的裝置名稱
  2. 開啟appium
    確認手機成功電腦,點擊搜尋按鈕右側的三角形表徵圖啟動appium服務,當介面出現如下現象則證明服務已啟動。
  3. Remote遠端控制(appium--General Setting介面)

    appium預設服務地址是127.0.0.1連接埠號碼4723,一般在本地機器上調試的話,無需修改。
    若是遠程到某台自動化測試電腦運行則此ip就能派上用場了,通過修改如下代碼即可
    driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps)

    改成:

    driver = webdriver.Remote(‘http://192.168.0.105:4723/wd/hub‘, desired_caps)

    如下:ipconfig查看電腦ip


  4. 遠端連線
    瀏覽器輸入:http://192.168.0.105:4723/wd/hub,看到如下介面,證明遠程連結成功
  5. 指令碼裡的代碼修改後如下
    #coding:utf-8import timefrom appium import webdriverdesired_caps = {}desired_caps[‘deviceName‘] = ‘192.168.199.101:5555‘desired_caps[‘platformName‘] = ‘Android‘desired_caps[‘platformVersion‘] = ‘5.0‘desired_caps[‘appPackage‘] = ‘com.manboker.headportrait‘desired_caps[‘appActivity‘] = ‘com.manboker.headportrait.activities.FirstActivity‘driver = webdriver.Remote(‘http://192.168.0.105:4723/wd/hub‘, desired_caps)time.sleep(8)#休眠8秒driver.find_element_by_id(‘com.manboker.headportrait:id/entry_album_set‘).click()time.sleep(5)driver.quit()

     

六、元素定位(常用方法)

  1. 通過id定位
    取resource-id的值:driver.find_element_by_id(‘com.manboker.headportrait:id/entry_album_set‘)
  2. 通過class_name定位
    取class內容:driver.find_element_by_class_name("android.widget.ImageView")
  3. 通過text定位
    使用text的內容:driver.find_elements_by_android_uiautomator("new UiSelector().text(\"登入\")")
  4. 通過name定位:driver.find_element_by_name()
  5. 通過xpath定位:driver.find_element_by_xpath("//android.widget.TextView[@resource-id=\"com.manboker.headportrait:id/nologin_retry\"]")
  6. 通過css_selector定位:driver.find_element_by_css_selector()
    如上的方法pycharm輸入driver.find_element_by會自動匹配出來。

OK,本文就介紹到這,下篇介紹Appium Python API。

Appium+python移動自動化測試(四)--Monitor/uiautomatorviewer工具及元素定位方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.