appium+python搭建自動化測試框架_Appium元素定位(二)


Appium元素定位:

工具:Android\android-sdk\tools    uiautomatorviewer.bat

 

1. id定位:

self.driver.find_element_by_id('com.tencent.mobileqq:id/btn_login').click()

 

2. class定位:

self.driver.find_element_by_class_name('android.widget.Button').click()

(注:一般一個頁面上的class屬性不唯一,元素不唯一的話定位會報錯了)

 

 

3. 相對定位:

相對定位是先找到該元素的有對應屬性的父元素節點,然后基於父元素進行元素定位。

代碼舉例:

此處只是舉例什么是相對定位,一般有id直接可以定位當然不這么干,在沒有id的情況下可以這么定位。。。。。。。。。。。

self.driver.find_element_by_id('com.tencent.mobileqq:id/name') self.driver.find_element_by_class_name('android.widget.Button').click()

uiautomatorviewer截屏:

 

4. xpath定位:

 

代碼舉例:

 name = self.driver.find_element_by_xpath('//android.widget.EditText[@text="QQ號/手機號/郵箱"]').send_keys('********')
 name = self.driver.find_element_by_xpath('//*[@class="android.widget.EditText" and @index="3"]').send_keys('********')

uiautomatorviewer截屏:

 

 

5. List定位:

List定位首先是使用find_elements_by_id(class_name/path)獲取一組相同的class屬性的的元素,然后使用數組下標來區分標記不同元素進行相關操作

代碼舉例:

一般從相冊設置頭像或是選擇照片時,會用到list定位,因為每一張照片的id是相同,那就要通過下標來定位所選的照片了..............

images = self.driver.find_elements_by_id('id') images[5].click

 

 

6. Appium元素等待:

強制等待:設置固定的等待時間,使用sleep()方法即可實現

from time import sleep #強制等待5秒
sleep(5)

隱式等待:針對全部元素設置等待時間

driver.implicitly_wait(20)

顯示等待:針對某個元素來設置的等待時間,方法WebDriverWait()一般和until()或until_not()方法配合使用,另外,lambda提供了一個運行時動態創建函數的方法

from selenium.webdriver.support.ui import WebDriverWait WebDriverWait(self.driver,3).until(lambda x:x.find_element_by_id('com.tencent.mobileqq:id/btn_login'))

 

 

7. Toast元素識別:

下圖為一般的toast提示,uiautomatorviewer工具是無法獲取到這種toast的任何信息

代碼實現:

注意如果內容為中文,必須注釋#coding=utf-8,否則會因為編碼導致文字識別失敗

 #適用toast彈窗
 def get_toast(self):
error_message= "賬號或密碼錯誤,請重新輸入"
limit_message=""
message1 ='//*[@text=\'{}\']'.format(error_message)
#message2 = '//*[@text=\'{}\']'.format(limit_message)
toast_element = WebDriverWait(self.driver,5).until(lambda x:x.find_element_by_xpath(message1))
print(toast_element.text)
 

 

 

8. 屏幕截圖:

方法一:save_screenshot()該方法直接保存當前屏幕截圖到當前腳本所在的文件位置

self.driver.save_screenshot('login.png')

 

方法二:get_screenshot_as_file(self,filename)將截圖保存在指定文件路徑

self.driver.get_screenshot_as_file('.\screenshots\login.png')

 

 

9. 連續滑動操作_TouchAction:

Touch Action包含一系列操作,比如按壓,長按,點擊,移動,暫停,組成一套動作。

按壓:press()

TouchAction(driver).press(x=0,y=308)

長按:longPress() ,比press多個按的時間參數duration,以毫秒為單位

long_press(x=0,y=308,duration=1000)

點擊:tap()對一個元素或是控件執行點擊操作

tap(self, element=None, x=None, y=None, count=1)

移動:move_to()將指針從上一個點一道指定的元素或點

move_to(self, element=None, x=None, y=None)

暫停:Wait(),暫停腳本的執行,單位為毫秒

wait(self, ms=0)

釋放:release()結束行動,取消屏幕上的指針

release(self)

執行:perform()執行的操作發送到服務器的命令操作

perform(self)

 

代碼舉例:已設置手勢密碼鎖為例,先進入密碼鎖的設置頁面:

#導入模塊
from appium.webdriver.common.touch_action import TouchAction for i in range(2): TouchAction(driver).press(x=243,y=381).wait(2000)\ .move_to(x=455,y=390).wait(1000) \ .move_to(x=455, y=390).wait(1000) \ .move_to(x=455, y=390).wait(1000) \ .release().perform()

 

 

10. 多點觸控操作_MultiAction:

多點觸控的類,可以模擬用戶多點操作,主要包含add()和perform()兩個方法

 

代碼舉例:以放大縮小百度地圖為例

from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.common.multi_action import MultiAction from time import sleep desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '5.1.1' desired_caps['deviceName'] = '127.0.0.1:62001' desired_caps['app'] = r'C:\Users\heber\Downloads\baidumap.apk'  # 被測試的App在電腦上的位置
desired_caps['appPackage'] = 'com.baidu.BaiduMap' desired_caps['appActivity'] = 'com.baidu.baidumaps.WelcomeScreen' desired_caps['noReset'] = True driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)  # 啟動app
 x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] def pinch(): action1 = TouchAction(driver) action2 = TouchAction(driver) pinch_action = MultiAction(driver) action1.press(x=x*0.2, y=y*0.2).wait(1000).move_to(x=x*0.4, y=y*0.4).wait(1000).release() action2.press(x=x*0.8, y=y*0.8).wait(1000).move_to(x=x*0.6, y=y*0.6).wait(1000).release() pinch_action.add(action1,action2) print("start pinch....") pinch_action.perform() def zoom(): action1 = TouchAction(driver) action2 = TouchAction(driver) zoom_action = MultiAction(driver) action1.press(x=x*0.4, y=y*0.4).wait(1000).move_to(x=x*0.2, y=y*0.2).wait(1000).release() action2.press(x=x*0.6, y=y*0.6).wait(1000).move_to(x=x*0.8, y=y*0.8).wait(1000).release() zoom_action.add(action1,action2) print("start zoom....") zoom_action.perform() if __name__ == '__main__': for i in range(3): pinch() for i in range(3): zoom()

 


免責聲明!

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



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