appium 常用api介紹(2)


前言:接着上一篇繼續講常用的一些api

參考博文:http://blog.csdn.net/bear_w/article/details/50330565

 

1.send_keys

 send_keys(self, *value):

 Simulates typing into the element在元素中模擬輸入(開啟appium自帶的輸入法並配置了appium輸入法后,可以輸入中英文)】

 :Args:

      - value - A string for typing, or setting form fields. For setting file inputs, this could be a local file path.

      Use this to send simple key events or to fill out form fields::

      form_textfield = driver.find_element_by_name('username')

      form_textfield.send_keys("admin") This can also be used to set file inputs.

  ::

      file_input = driver.find_element_by_name('profilePic')

      file_input.send_keys("path/to/profilepic.gif")

      # Generally it's better to wrap the file path in one of the methods

      # in os.path to return the actual path to support cross OS testing.

      # file_input.send_keys(os.path.abspath("path/to/profilepic.gif"))

  用法:driver.element.send_keys(“中英”)

 send_keys方法需要在配置Capabilities信息時打開模擬鍵盤unicodeKeyboard與resetKeyboard,如下面代碼舉例:

from appium import webdriver
import time
desired_caps = {
               'platformName' : 'Android',
               'deviceName' : '76P4C15813005463',
               'platformVersion' : '5.1',
               #測試apk包名
               'appPackage' : 'com.huawei.android.launcher',
               #測試apk的launcherActivity
               'appActivity' : '.Launcher',
               #打開模擬鍵盤                        
               'unicodeKeyboard' : True ,
               'resetKeyboard' : True,
               }
#進入android系統launcher
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
time.sleep(5)
#進入應用市場
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'應用市場)]").click()
#使用模擬鍵盤輸入'今日頭條'     
driver.find_element_by_id('com.huawei.appmarket:id/search_edit_text_view').send_keys(u"今日頭條")
driver.find_element_by_id('com.huawei.appmarket:id/search_icon_view').click()
time.sleep(5)
driver.quit()

 

2.find_element_by_id

 find_element_by_id(self, id_):

 Finds element within this element's children by ID(通過元素的ID定位元素)

 :Args: - id_ - ID of child element to locate.

 用法 driver. find_element_by_id(“id”)

 find_element_by_id方法,是對於那些有id而且可以互相區分的控件的操作使用,一般通過android sdk 的tools路徑下的uiautomatorviewer.bat自帶工具來獲取,

 使用這個工具首先要確保前面環境配置ok,然后確定測試機器或android模擬器處於連接狀態(cmd上輸入adb devices),下面是使用uiautomatorviewer.bat工具

 獲取id的步驟:

 (1)在android-sdk\tools路徑下找到uiautomatorviewer.bat工具,雙擊uiautomatorviewer.bat打開,如下圖

 

 (2)獲取id,如下圖所示,右邊Node Detail的紅色框內resource-id就是左邊天氣應用的id

 

 代碼舉例:

#點擊天氣應用
driver.find_element_by_id('com.huawei.android.totemweather:id/mulan_widget_currentweather_smallicon').click()

 

3.find_element_by_xpath

 find_element_by_xpath(self, xpath):

 Finds element by xpath通過Xpath定位元素,詳細方法可參閱http://www.w3school.com.cn/xpath/ )

 :Args:

     xpath - xpath of element to locate. "//input[@class='myelement']"

     Note: The base path will be relative to this element's location.

     This will select the first link under this element.

 ::

     myelement.find_elements_by_xpath(".//a")

     However, this will select the first link on the page.

 ::

     myelement.find_elements_by_xpath("//a")

 用法 find_element_by_xpath(“//*”)

 find_element_by_xpath方法也需要使用uiautomatorviewer.bat工具來定位控件,如下圖所示右邊相機控件id為空,這時就需要使用xpath來定位元素了,

 我們可以使用xpath用text和index來定位元素,如下圖的就可以這樣定位相機:

 driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'相機')]")

 driver.find_element_by_xpath("//android.widget.TextView[contains(@index,8)]")

 

 如下圖的圖庫那個控件index和天氣溫度控件是相同的,都是index=7,就只能用text或text與index結合了,如:

 driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'圖庫')]")

 driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'圖庫')and@index='7']")

 

 上面的代碼就可以這樣寫

# -*-coding=utf-8 -*-

from appium import webdriver
import time
desired_caps = {
               'platformName' : 'Android',
               'deviceName' : '76P4C15813005463',
               'platformVersion' : '5.1',
               #測試apk包名
               'appPackage' : 'com.huawei.android.launcher',
                #測試apk的launcherActivity
               'appActivity' : '.Launcher',
               }
#進入android系統launcher
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
time.sleep(5)
#從launcher主界面進入相機應用並退出的兩種方法
driver.keyevent('3')
#xpath使用text定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'相機')]").click()
time.sleep(2)
driver.keyevent('3')
time.sleep(2)
#xpath使用index定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@index,8)]").click()
time.sleep(2)
driver.keyevent('3')
time.sleep(2)

#從launcher主界面進入圖庫應用並退出的兩種方法
#xpath使用text定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'圖庫').click()
time.sleep(2)
driver.keyevent('3')
time.sleep(2)
#xpath使用text與index一起定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'圖庫')and@index='7']").click()
time.sleep(5)
driver.keyevent('3')
time.sleep(2)

driver.quit()

 

4. ind_element_by_name

 find_element_by_name(self, name):

 Finds element within this element's children by name【通過元素Name定位(元素的名稱屬性text)】

  :Args: - name - name property of the element to find.

  用法: driver.find_element_by_name(“name”)

 find_elements_by_name方法的控件元素也需要使用uiautomatorviewer.bat工具來定位,如上面例子中帶有text信息的控件就可以直接使用,如

 driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'相機')]")

 driver.find_element_by_xpath("//android.widget.TextView[contains(@index,8)]")

 可以使用

 driver.find_element_by_name('相機')

 

 腳本如下

from appium import webdriver
import time
desired_caps = {
               'platformName' : 'Android',
               'deviceName' : '76P4C15813005463',
               'platformVersion' : '5.1',
               #測試apk包名
               'appPackage' : 'com.huawei.android.launcher',
               #測試apk的launcherActivity
                'appActivity' : '.Launcher',
               }
#進入android系統launcher
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
time.sleep(5)

#name方法進入相機應用
time.sleep(2)
driver.find_element_by_name("相機").click()
time.sleep(5)

driver.quit()

 

5. find_element_by_class_name

 find_element_by_class_name(self, name):

 Finds element within this element's children by class name(通過元素class name屬性定位元素

 :Args: - name - class name to search for.

 用法 driver. find_element_by_class_name(“android.widget.LinearLayout”)

find_element_by_class_name方法其實很不實用,一般控件的class基本相同,如下圖上面的應用控件class都是android.widget.TextView,所以不好區分元素。

 

 

6.tap

 tap(self, positions, duration=None):

 Taps on an particular place with up to five fingers, holding for a certain time 【模擬手指點擊(最多五個手指),可設置按住時間長度(毫秒)】

 :Args:

         - positions - an array of tuples representing the x/y coordinates of the fingers to tap. Length can be up to five.

         - duration - (optional) length of time to tap, in ms

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

  用法 driver.tap([(x,y),(x1,y1)],500)

 當控件無法獲取時,那我們就可以使用坐標用tap方法做點擊操作,而且tap方法可以用於多點點擊。

driver.tap([(300,500)],10)

  

7.zoom

 zoom(self, element=None, percent=200, steps=50):

 Zooms in on an element a certain amount (在元素上執行放大操作)

 :Args:

          - element - the element to zoom

          - percent - (optional) amount to zoom. Defaults to 200%  

          - steps - (optional) number of steps in the zoom action

 :Usage: driver.zoom(element)

 用法 driver.zoom(element)

 zoom方法用來模擬手機上的放大操作,主要是要確定element,具體例子和下面pinch方法一起講。

el=driver.find_element_by_class_name('android.widget.RelativeLayout')
driver.zoom(el,150,30)    #percent參數和steps可以不寫,不寫保持默認數值

 

8.pinch

 pinch(self, element=None, percent=200, steps=50):

 Pinch on an element a certain amount 在元素上執行模擬雙指捏(縮小操作)

  :Args:

            - element - the element to pinch

            - percent - (optional) amount to pinch. Defaults to 200%

            - steps - (optional) number of steps in the pinch action

  :Usage: driver.pinch(element)

 用法 driver.pinch(element)

 pinch方法用來模擬手機上的縮小操作,主要是要確定element,下面舉例進入圖庫查看圖片時放大和縮小圖片,使用uiautomatorviewer.bat工具來定位元素。

 (1)進入圖庫,如下圖所示,圖庫控件text是唯一的,所以我們采用by_name方法獲取元素。

  driver.find_element_by_name('圖庫')

 

 (2)選擇圖庫的一個文件夾,如下圖所示,選擇雜志鎖屏,由於該元素text也是唯一的,所以使用by_name方法獲取控件元素。

   driver.find_element_by_name('雜志鎖屏')

 

  (3)選擇一張圖片,所下圖所示,下面圖片是一個整的布局,沒有單獨分開的控件元素,所以我們只能選擇使用tap方法點擊屏幕。

   driver.tap([(300,500)],50)

 

 (4)放大和縮小圖片,如下圖所示,整個圖片是一個布局,而且只有class信息,由於放大和縮小需要獲得element,所以我們使用class_name的

  方法獲取整個布局作為元素。

  el=driver.find_element_by_class_name('android.widget.RelativeLayout')

  driver.zoom(el)

  driver.pinch(el,200,50)

 

  上面操作的具體腳本如下

from appium import webdriver
import time
desired_caps = {
               'platformName' : 'Android',
               'deviceName' : '76P4C15813005463',
               'platformVersion' : '5.1',
               #測試apk包名
               'appPackage' : 'com.huawei.android.launcher',
               #測試apk的launcherActivity
               'appActivity' : '.Launcher',
               }
#進入android系統launcher
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
time.sleep(5)
#進入圖庫
driver.find_element_by_name('圖庫').click()
driver.find_element_by_name('雜志鎖屏').click()
driver.tap([(300,500)],50)
time.sleep(1)
el=driver.find_element_by_class_name('android.widget.RelativeLayout')
#放大圖片
driver.zoom(el)
time.sleep(5)
#縮小圖片
driver.pinch(el,200,50)
time.sleep(5)
driver.quit()

 

9.start_activity 

啟動其他app

用法:driver.start_activity("包名","界面名")

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM