Appium之常用API

Appium常用API解析

 

1、current_activity:獲取當前頁面的activity名,比如com.taobao.tao.TBMainActivity 或 com.taobao.browser.BrowserActivity

如判斷當前頁面是否為收藏夾:

if driver.current_activity == "com.taobao.weex.WXActivity":
    pass

 

2、page_source:返回頁面的樹形結構源碼

如通過斷言判斷是否進入某個頁面(該頁面包含特定內容資訊)

content = driver.page_source
try:
    assert "我的淘寶" in content
except Exception as e:
    raise e

 

3、contexts:返回當前會話的上下文(可以用來識別native和H5混合頁面)

如在native和html5混合頁面測試時,需要在native層和html5層切換,則首先需要得到context層的名稱:

print(driver.contexts)

結果輸出:
['NATIVE_APP', 'WEBVIEW_com.codoon.gps']

然後使用driver.switch_to.context("NATIVE_APP"),就可以切換到native層了

 

4、find_element_by_xxx:查找當前頁面的一個元素

ele = driver.find_element_by_name("foo")

還可以使用如下:
ele = driver.find_element(by="name", value="foo")

 

5、find_elements_by_xxx:查找當前頁面的多個元素

eles = driver.find_elements_by_name("foo")

還可以使用如下:
eles = driver.find_elements(by="name", value="foo")

 

6、click():點擊(在移動端的時候,會有200~300ms的延遲)

driver.find_element(by="xpath", value="//slhsjh").click()

 

7、tap():在特定位置上輕擊,並保持一定的時間(可以減少click在移動端的延遲,提高了性能)

函數原型:tap(positions: List[Tuple[int, int]], duration: Optional[int] = None)

driver.tap([(100, 20), (100, 60), (100, 100)], 500)

click和tap都能實現單擊的效果。其區別在於click是作用於driverelement的實例化對象,而tap是對螢幕上的坐標位置進行點擊。前者對元素的位置變化並不敏感,而後者是針對具體的像素坐標點擊,受解析度和元素位置影響較大。

 

8、send_keys():調用設備的系統輸入法鍵盤,輸入文本內容

driver.find_element(by="xpath", value="//slhsjh").send_keys("Test")

 

9、swipe()、flick():滑動,從[start_x, start_y]划到[end_x, end_y]的過程。

區別:swipe比flick多了一個duration參數,有了這個參數就可以自定義從start到end動作的作用時間duration(單位為ms),以達到快速滑動或者慢速滑動的效果。

函數原型:

swipe(start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0)
flick(start_x: int, start_y: int, end_x: int, end_y: int)

driver.swipe(100, 100, 100, 400, 500)
driver.flick(100, 100, 100, 400)

 

TouchAction類

TouchAction是APPium中封裝的一個方法,主要是都手勢控制的一些操作:點擊、短按、長按、移動操作。

導入模組:from appium.webdriver.common.touch_action import TouchAction

TouchAction(driver):獲取的是TouchAction對象

 

TouchAction對象的操作方法

release()和perform()方法一般跟添加的動作後面。

(1)release():通過將指針離開螢幕(釋放指針)來結束動作

(2)perform():通過向伺服器發送命令來執行動作

from appium.webdriver.common.touch_action import TouchAction

方法1:tap(element=None, x=None, y=None, count=1) ''' 模擬手勢觸摸元素或坐標點。 (1)element:要點擊的元素 (2)x/y:移動到的螢幕坐標點,默認為空 ''' 方法2:press(el=None, x=None, y=None, pressure=None) ''' 模擬手勢短按元素或坐標。 (1)el:要點擊的元素 (2)x/y:移動到的螢幕坐標點,默認為空 (3)pressure:壓力; [iOS只]按作為強制觸摸 ''' 方法3:long_press(el=None, x=None, y=None, duration=1000) ''' 模擬手勢長按元素或坐標點,持續duration時間。 (1)el:要點擊的元素 (2)x/y:移動到的螢幕坐標點,默認為空 (3)duration:持續按壓時間,單位為ms ''' 方法4:wait(ms=0) """ 模擬動作後的等待的時間。 """ 方法5:move_to(el=None, x=None, y=None) ''' 模擬從一個點移動到指定元素或指定點。 (1)el: 移動到的元素,默認為空 (2)x/y:移動到的螢幕坐標點,默認為空 ''' 方法6:release() ''' 通過將指針離開螢幕(釋放指針)來結束動作。 ''' 方法7:perform() ''' 通過向伺服器發送命令來執行動作。 ''' 例如: from appium import webdriver from appium.webdriver.common.touch_action import TouchAction ele = driver.find_element_by_xpath("//test") TouchAction(driver).tap(element=ele).release().perform() TouchAction(driver).tap(x=920, y=950).release().perform() TouchAction(driver).press(el=ele).release().perform() TouchAction(driver).long_press(el=ele).perform() TouchAction(driver).wait(100) TouchAction(driver).move_to(el=ele).release().perform()

 

MultiTouch類

MultiTouch類主要用於多點觸控操作,可用於對介面執行放大、縮小操作。

導入模組:from appium.webdriver.common.multi_action import MultiAction

 

MultiAction(driver, element=None):獲取的是MultiAction對象

driver:操作的設備


element:操作的元素,默認為None

 

1. MultiAction對象的操作方法

1.1、添加TouchAction對象到MultiAction對象

(1)add(*touch_actions)

touch_actions:1個或多個TouchAction對象(描述一個手指要執行的一系列動作)

1.2、執行儲存在對象中的所有動作

(1)perform()

當perform執行被調用時,添加到多點觸摸的所有觸摸動作都被發送到appium,並執行,就像它們同時發生一樣。appium首先執行所有觸摸動作的第一個事件,然後執行第二個……。

# 初始化MultiAction對象
MultiActionObject = MultiAction(driver)
# 向MultiAction對象中添加動作
touchAction1 = TouchAction(driver).press(x=100, y=200)
touchAction2 = TouchAction(driver).press(x=100, y=300)
MultiActionObject.add(touchAction1, touchAction2)
# 執行操作
MultiActionObject.perform()

 

元素事件類API

(1)reset():重置應用(類似刪除應用數據)
如通過driver.reset()來模擬實現首次登錄app時出現的引導頁需求:
driver.reset()

 

(2)is_app_installed():檢查設備上是否已安裝app,參數為package_name(appPackage)。返回True表示已安裝,False表示未安裝。

如判斷是否已安裝淘寶app:

if driver.is_app_installed("com.taobao.taobao"):
    '''參數為package_name(appPackage)'''
    pass
else:
    pass

 

(3)install_app():安裝特定路徑的app;參數為app的本地或遠程路徑。

如若檢測到未安裝淘寶app,則安裝該app:
if driver.is_app_installed("com.taobao.taobao"):
    '''參數為package_name(appPackage)'''
    pass
else:
    driver.install_app(r"E:\AndroidTest\base.apk")

 

(4)remove_app():在設備上移除特定的app,參數為package_name(appPackage)。

如測試舊版本兼容用例時,需要先卸載新版本,再安裝舊版本:
driver.remove_app("com.taobao.taobao_new")
driver.install_app("com.taobao.taobao_old")

 

(5)launch_app():在設備上啟動desired capabilities中指定的app。

driver.launch_app()

 

(6)close_app():在設備上停止運行desired capabilities中指定的app。
driver.close_app()

 

(7)start_activity():在測試任務期間啟動新的app活動,參數為appPackage和appActivity。

driver.start_activity('com.tencent.mobileqq','com.tencent.mobileqq.activity.SplashActivity')
print(driver.current_activity)

 

(8)wait_activity():隱式等待某個activity出現,參數分別為目標activity、超時時間、檢測間隔(單位均為s)。

(類似於webdriver的WebDriverWait,且是Android特有的方法)

如在後續操作需要在當前activity為主頁的前提下:
driver.wait_activity("homepage.activity", 15, 1)
pass

 

其它API

(1)get_screenshot_as_file():截屏並保存在特定路徑,參數為圖片路徑和圖片名稱。
driver.get_screenshot_as_file('/Screenshots/foo.png')

 

(2)get_attribute():獲取已知元素控制項的屬性值,參數為控制項。

driver.find_element_by_xpath("//test").get_property("checkable")

driver.find_element_by_xpath("//test").get_property("text")

 

(3)background_app():將 app 置於後台,把當前應用放到後台去,參數為後台運行的時間,單位為s。

driver.background_app(5)

 

(4)pull_file():從設備中提取文件。

driver.pull_file('Library/AddressBook/AddressBook.sqlitedb')

如在手機號註冊時需要獲取手機驗證碼,此時的實現方式是用另一個apk提取到驗證碼存在手機記憶體中,再用pull_file獲取到驗證碼內容,使得appium可以將正確的驗證碼填入。

 

(5)push_file():推送文件到設備中去

driver.pull_file('Library/AddressBook/AddressBook.sqlitedb')```

 

(6)keyevent()、press_keycode():按鍵事件,向設備發送按鍵編碼,參數為按鍵編碼。(Android特有的方法)
driver.keyevent(66)         # 回車鍵
driver.press_keycode(3)     # Home鍵

 

(7)hide_keyboard():收起鍵盤

driver.hide_keyboard()

 

(8)open_notifications():打開通知欄(僅Android特有)

driver.open_notifications()

 

(9)shake():模擬設備搖晃

driver.shake()

 

 

 參考://www.jianshu.com/p/0e852c58609f

參考://www.cnblogs.com/7chentest/p/6396551.html

 

Tags: