appium自動化測試(二)

來源:互聯網
上載者:User

標籤:說明   mat   進度   driver   col   android布局   source   菜單   button   

一. 擷取應用程式套件名和入口activity

擷取應用程式套件名和入口activity:aapt命令

 

aapt目錄:

安卓sdk的build-tools目錄下(如果要在cmd裡直接運行,要配置環境變數,否則需要在aapt所在目錄下開啟cmd)

樣本:

adt-bundle-windows-x86_64-20140702\sdk\build-tools\android-4.4W

 

命令文法:

aapt dump badging apk安裝包名

樣本:

aapt dump badging D:\Python全棧自動化\python_工具\appium+模擬器\Future-release-2018.apk

搜尋launchable-activity,得到的是啟動頁面的Activity

部分代碼

#啟動頁兩種方式desired_caps["appActivity"] = ".activity.addition.WelcomeActivity"desired_caps["appActivity"] = "com.xxzb.fenwoo.activity.addition.WelcomeActivity"

實際效果

二. Hybird & Webview

Hybird混合應用程式,在應用程式中嵌入了webview,通過webview訪問網頁

webview是一個基於webkit引擎,展現web頁面的控制項

作用:

  • 顯示和渲染web介面

  • 直接使用html檔案(網路或apk資源套件assets)作布局

  • 可和javascript互動調用

三. Native APP

傳統的原生APP開發模式,Android基於Java語言,底層調用Google提供的API,IOS基於Objective C或者Swift,底層調用Apple官方提供的API

 

tips:

1. 在手機/模擬器中點擊關於手機中的版本號碼5下,出來開發人員選項

2. 在開發人員選項中勾選上顯示布局邊界

3. 如果是html的介面,那介面不會有布局邊界顯示,如有則說明是native的介面

四. Android布局類型

五. 常見控制項
  • TextView

    文本控制項,展示文本/文字

  • EditText

    可編輯輸入框,用於接收使用者輸入的資料

  • Button  

    按鈕,使用者通過點擊Button觸發一系列的事件

  • CheckBox

    複選/多選按鈕

  • ImageView

     圖片控制項,用於顯示圖片

  • ProgressBar 

     載入進度條,表示正在載入一些資料

六. 控制項屬性
  • index

     索引

  • text

     文本描述,新版本需要使用:findElementByAndroidUIAutomator("new UiSelector().text(\"師資團隊\")")

  • resource-id

 

     控制項/布局界限

    [480 1179] 描述控制項/布局的起始座標:X, Y軸

    [720 1280] 描述控制項/布局的終止座標:X, Y軸

附錄:

七. appium-app頁面元素定位

1. 通過id定位元素:resource-id

2. 通過text定位(已捨棄)

3. 通過ClassName定位:classname

4. 通過AccessibilityId定位:content-desc

5. 通過AndroidUiAutomator定位

6. 通過xpath定位

 

#resource-id定位driver.find_element_by_id("com.xxzb.fenwoo:id/layout_borrow_money")#classname定位driver.find_element_by_class_name("android.widget.RelativeLayout")#content-desc定位driver.find_element_by_accessibility_id("")#uiautomator(所有屬性都支援)定位driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("com.xxzb.fenwoo:id/layout_item3")‘)#xpath定位driver.find_element_by_xpath(‘//android.widget.TextView[@text=\"借款期限\"]‘)

 

xpath, id, class, accessibility id, -android uiautomator

 

八. UI Automator

UI自動化測試架構,安卓移動端app

要求:Android 4.3以上

 

  • 提供了一系列API:執行UI測試在系統或者第三方app上面

  • 允許在被測裝置上執行操作,比如開啟系統設定菜單

  • 適合編寫黑盒自動化測試

 

UI Automator架構的主要特點:

1. 元素定位:UI Automator Viewer 掃描、分析待測應用的UI組件的映像工具

2. 元素操作:Accessing device state 在目標裝置和app上的各種操作

3. 元素識別:UI Automator APIs 在多個應用程式中捕獲和操作UI組件

九. UI Automator Viewer

元素識別工具:UI Automator Viewer

 

在安卓的SDK下的tools目錄中

十. UI Automators APIs

UiObject類:安卓組件對象

對象有許多類比實際操作手機的方法和屬性,比如:文本的編輯、手勢操作等

類似web自動化中,元素對象(webelement),有點擊、輸入操作等

 

UiSelector類:

通過組件的各種屬性與節點關係定位組件

調用方法:new UiSelector().resourceId("")

       new UiSelector().className(\"android.widget.LinearLayout\").resourceId(\"com.xxzb.fenwoo:id/layout_item3\")

因為函數的傳回值就是執行個體化對象本身,那麼它可以不斷的調用很多方法

 

UiDevice類:提供了一系列方法和屬性來類比在手機上的實際操作(更高一級,類似於document或者window)

擷取裝置資訊:螢幕解析度、選裝狀態、亮滅屏......

操作:按鍵、座標操作、滑動、拖拽、......

 

十一. AndroidUiAutomator定位(在java中雙引號才表示字串,因此定位運算式必須是雙引號)

使用UiAutomator中的UiSelector類來處理元素定位

 

在python用戶端appium庫中通過,uiautomator來擷取元素的方法為:driver.find_element_by_android_uiautomator()

 

該方法的參數為UiSelector類定位元素的運算式:new UiSelector().函數名稱("定位運算式")

new UiSelector().函數名稱("定位運算式")

執行個體化一個UiSelector對象,然後通過執行個體調用介面

 

樣本:

driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("com.xxzb.fenwoo:id/btn_login")‘)

 

十二. 執行個體
from appium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom appium.webdriver.common.mobileby import MobileBy#由你來主動告訴appium server,我要操作哪個裝置上的哪個app#Desired Capabilities——索引值名。鍵名都是已經定義好的#操作對象的資訊準備desired_caps = {}#作業系統——目標機desired_caps["platformName"] = "Android"#系統版本desired_caps["platformVersion"] = "5.1.1"#裝置名稱字desired_caps["deviceName"] = "Android Emulator"#app資訊#包名desired_caps["appPackage"] = "com.xxzb.fenwoo"#首頁desired_caps["appActivity"] = ".activity.MainActivity"#串連appium server,並告訴其要操作的對象driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps)#等待WebDriverWait(driver, 30, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.xxzb.fenwoo:id/btn_login")))#點擊登入註冊按鈕driver.find_element_by_id("com.xxzb.fenwoo:id/btn_login").click()#等待WebDriverWait(driver, 30, 1).until(EC.visibility_of_element_located((MobileBy.ID, "com.xxzb.fenwoo:id/et_phone")))#輸入使用者名稱driver.find_element_by_id("com.xxzb.fenwoo:id/et_phone").send_keys("18684720553")#下一步driver.find_element_by_id("com.xxzb.fenwoo:id/btn_next_step").click()
十三. appium功能介紹

 

 

 

 

 

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.