安卓自動化之元素定位三種方式:


方式一:

也就是大家熟悉的 你安裝Android sdk  E:\Android\android-sdk\tools 目錄下的 uiautomatorviewer.bat  直接雙擊打開就可以定位手機元素

 

存在問題一:

(1)adb 版本不能太低

(2)對於安卓版本高的手機(安卓9.0我就碰到過 死活連不上),無法連接 無法定位

 

 

方式二:

前提:需要安裝java jdk

(1)安裝SDK

Android SDK 下載地址(需要安裝sdk):

http://tools.android-studio.org/index.php/sdk

需要在系統變量中添加環境變量(是新增)

ANDROID_HOME    D:\android\Android\sdk(寫自己的地址 別復制了)

 

 

(2)安裝appium-server

https://bitbucket.org/appium/appium.app/downloads/ 安裝包地址:

安裝完成 在命令窗口輸入:appium-doctor 檢查環境 (以上兩個安裝完成之前要保證java jdk已安裝)

出現 all checks were successful    才是正確的

 

直接在命令窗口輸入 pip install Appium-Python-Client。
 
就此環境已准備好:
 
 
(3)weditor鏈接手機進行調式:
 WEditor可以理解為在瀏覽器中打開的uiautomatorviewer,個人體驗比uiautomatorviewer更好用,不會像uiautomatorviewer那樣由於安卓系統的不同,會出現各種情況,還支持安卓、ios、Neco(beta),本人是體驗了uiautomatorviewer的坑以后,決定投向WEditor。
 

1、安裝WEditor:進入cmd,輸入pip install --pre --upgrade weditor

 

2、打開WEditor:在cmd下,輸入python -m weditor,此時會自動打開默認瀏覽器

 

 

weditor可能在python2.7上安裝存在問題

 

 

方式三:

appiun-desk

 

 

 

 啟動之后點擊+號新增 你的啟動配置

 

舉個栗子:

{
"platformName": "Android", #手機系統
"platformVersion": "9",  #安卓版本
"deviceName": "2f7e2ac9",  #devices 名稱   拿 adb devices 可以查到
"noReset": "true",  #不管
"packageName": "com.oppopay.payments", #待測試應用的包名稱
"app": "E:\\Apitest\\Finshell_memory\\Finshell_15_11_2019_UAT_v25.apk", #待測應用的絕對路徑
"automationName": "uiautomator" #啟動
}

 

一切ok之后點擊start session (啟動這個之前 需啟動appium )這個東西就是為appium服務的 必須要先打開。

 

 

 

元素三種定位工具都講完了,試下吧。。。

 

ps 關於滑動操作:

 

(1)才用appium時

分享一個公共函數:

# coding=utf-8
# 跳過瀏覽器引導
import time


class swipe(object):

def __init__(self, driver):
self.driver = driver
self.width = self.driver.get_window_size()['width']
self.height = self.driver.get_window_size()['height']

def swipetoUp(self):
time.sleep(10)
self.driver.swipe(self.width / 2, self.height * 3 / 4,
self.width / 2, self.height / 4, 0)

def swipetoDown(self):
time.sleep(5)
self.driver.swipe(self.width / 2, self.height / 3,
self.width / 2, self.height * 3 / 4, 0)

def swipetoLeft(self):
time.sleep(5)
self.driver.swipe(self.width * 0.9, self.height / 2,
self.width * 0.1, self.height / 2, 0)

def swipetoRight(self):
time.sleep(5)
self.driver.swipe(self.width * 0.1, self.height / 2,
self.width * 0.9, self.height / 2, 0)


appium啟動應用 時:
# -*- coding: utf-8 -*-
from appium import webdriver


class startSession(object):
def __init__(self,desired_caps):
self.desired_caps = desired_caps

def run(self):
print '******* StartSession ******'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_caps)
return self.driver



def desired_caps():
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '7.1.1'
desired_caps['automationName'] = "uiautomator2"
desired_caps['deviceName'] ='2f7e2ac9'#YDBUJNYL8SNNPZGE 2f7e2ac9
desired_caps['noReset'] = 'true'
desired_caps['appActivity'] ='com.pay.payments.ui.activity.SplashActivity'
desired_caps['androidDeviceReadyTimeout'] = '180'
desired_caps['appPackage'] ='com.pay.payments'
return desired_caps

拿封裝兩個函數來啟動

具體使用就是:
要導入這兩個函數進來
driver = startSession(desired_caps()).run()
一行就搞定了啟動應用



下面就是u2了
個人感覺做ui自動化 u2簡單多了

啟動應用
import uiautomator2 as u2
d = u2.connect()
# d.app_start('com.pay.payments') # 第二種啟動app的方式
d.session('com.pay.payments') # 第三種啟動app的方式

一句話就啟動了應用對於appium 優勢很明顯。

但是u2對於appium 滑動操作比較麻煩:
d = u2.connect()
d(scrollable=True).scroll.to(text='Next')
這是我找到一個很好方式 滑動這個指定 內容的地方 以上就是滑動到Next 這里 頁面會首先向上 然后向下找。





 

 

 

 

 


免責聲明!

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



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