appium的安裝
官網:
http://appium.io
下載地址:
https://github.com/appium/appium-desktop/releases/tag/v1.8.2
linux,mac配置環境變量:
sudo vim /etc/hosts
export ANDROID_HOME=/Users/seancheney/Library/Android/sdk
export PATH=$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$PATH
python安裝appium api:
pip install Appium-Python-Client
adb查看設備名:
adb devices -l
bb43c0d0 device product:kenzo model:Redmi_Note_3
WiFi連接手機
開啟端口(端口不能被占用)
adb -s bb43c0d0 tcpip 5556
開啟連接
adb connect 192.168.2.181:5556
adb命令查看包名:
adb shell pm list packages
package:com.pandavisa
package:com.qrd.omadownload
package:com.android.providers.telephony
# 查看appActivity
1、cmd命令中輸入:adb shell 進入shell命令模式
2、shell中輸入:logcat | grep ActivityManager 真機運行應用,可以實時 查看當前正在運行的Activity;
appium的使用
初始化
# Android environment
from appium import webdriver
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '8.1'
desired_caps['automationName'] = 'uiautomator2'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['app'] = PATH('../../../apps/selendroid-test-app.apk')
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 查找元素
el = self.driver.find_element_by_android_uiautomator('new UiSelector().description("Animation")')
els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
el = self.driver.find_element_by_android_viewtag('a tag name')
els = self.driver.find_elements_by_android_viewtag('a tag name')
# 點擊
tap()
click()
# 動作鏈
el = self.driver.find_element_by_accessibility_id('Animation')
action = TouchAction(self.driver)
action.tap(el).perform()
# 多動作
els = self.driver.find_elements_by_class_name('listView')
a1 = TouchAction()
a1.press(els[0]) \
.move_to(x=10, y=0).move_to(x=10, y=-75).move_to(x=10, y=-600).release()
a2 = TouchAction()
a2.press(els[1]) \
.move_to(x=10, y=10).move_to(x=10, y=-300).move_to(x=10, y=-600).release()
ma = MultiAction(self.driver, els[0])
ma.add(a1, a2)
ma.perform();
# 拖動
driver.swipe
FLICK_START_X = 300
FLICK_START_Y = 300
FLICK_DISTANCE = 700
def scroll(self):
while True:
self.driver.swipe(FLICK_START_X, FLICK_START_Y + FLICK_DISTANCE, FLICK_START_X, FLICK_START_Y)
sleep(2)
appium
Appium是一個開源測試自動化框架,可用於原生,混合和移動Web應用程序測試。 它使用WebDriver協議驅動iOS,Android應用程序。





C:\Users\Administrator\AppData\Local\Android\Sdk\build-tools\28.0.3>aapt.exe dump badging G:\安裝包!!!!!!!!!!!!!!!!!!!!\安卓\app\com.tal.kaoyan_3.3.5.2_liqucn.com.apk

或者

第二個方法

然后啟動app

{
"platformName": "Android",
"deviceName": "127.0.0.1:62001",
"platformVersion": "5.1.1",
"appPackage": "com.tal.kaoyan",
"appActivity": "ui.activity.SplashActivity",
"noReset": true
}
怎么使用Uiautomatorviewer獲取元素的xpath
http://www.bubuko.com/infodetail-2259722.html
uiautomatorviewer.bat
關閉appium后才能進去


appium的應用,進入新聞app並不斷下滑
先在模擬機設置里 清空app的數據
import time
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
server = 'http://localhost:4723/wd/hub'
desired_caps = {
"platformName": "Android",
"deviceName": "Redmi_Note_3",
"appPackage": "com.android36kr.app",
"appActivity": ".ui.MainActivity"
}
print('111')
driver = webdriver.Remote(server, desired_caps)
print('222')
wait = WebDriverWait(driver,30)
print('333')
kuaixun = wait.until(EC.presence_of_element_located((By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.view.ViewGroup/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.HorizontalScrollView/android.widget.LinearLayout/android.widget.TextView[3]')))
kuaixun.click()
FLICK_START_X = 300
FLICK_START_Y = 300
FLICK_DISTANCE = 700
while True:
driver.swipe(FLICK_START_X, FLICK_START_Y + FLICK_DISTANCE, FLICK_START_X, FLICK_START_Y)
time.sleep(2)
模擬登陸app並滑動
#需要安裝客戶端的包
#pip3 install Appium-Python-Client
import time
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
cap = {
"platformName": "Android",
"deviceName": "127.0.0.1:62001",
"platformVersion": "5.1.1",
"appPackage": "com.tal.kaoyan",
"appActivity": "ui.activity.SplashActivity",
"noReset": True
}
driver = webdriver.Remote("http://localhost:4723/wd/hub",cap)
def get_size():
x = driver.get_window_size()['width']
y = driver.get_window_size()['height']
return(x,y)
try:
#是否跳過
if WebDriverWait(driver,3).until(lambda x:x.find_element_by_xpath("//android.widget.TextView[@resource-id='com.tal.kaoyan:id/tv_skip']")):
driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.tal.kaoyan:id/tv_skip']").click()
except:
pass
try:
if WebDriverWait(driver,3).until(lambda x:x.find_element_by_xpath("//android.widget.EditText[@resource-id='com.tal.kaoyan:id/login_email_edittext']")):
driver.find_element_by_xpath("//android.widget.EditText[@resource-id='com.tal.kaoyan:id/login_email_edittext']").send_keys("a349553119")
driver.find_element_by_xpath("//android.widget.EditText[@resource-id='com.tal.kaoyan:id/login_password_edittext']").send_keys("a123456")
driver.find_element_by_xpath("//android.widget.Button[@resource-id='com.tal.kaoyan:id/login_login_btn']").click()
except:
pass
try:
#隱私協議
if WebDriverWait(driver,3).until(lambda x:x.find_element_by_xpath("//android.widget.TextView[@resource-id='com.tal.kaoyan:id/tv_title']")):
driver.find_element_by_xpath("//android.widget.TextView[@resource-id='com.tal.kaoyan:id/tv_agree']").click()
driver.find_element_by_xpath("//android.support.v7.widget.RecyclerView[@resource-id='com.tal.kaoyan:id/date_fix']/android.widget.RelativeLayout[3]").click()
except:
pass
#點擊研訊
if WebDriverWait(driver,3).until(lambda x:x.find_element_by_xpath("//android.support.v7.widget.RecyclerView[@resource-id='com.tal.kaoyan:id/date_fix']/android.widget.RelativeLayout[3]/android.widget.LinearLayout[1]/android.widget.ImageView[1]")):
driver.find_element_by_xpath("//android.support.v7.widget.RecyclerView[@resource-id='com.tal.kaoyan:id/date_fix']/android.widget.RelativeLayout[3]/android.widget.LinearLayout[1]/android.widget.ImageView[1]").click()
l = get_size()
x1 = int(l[0]*0.5) #滑動位置
y1 = int(l[1]*0.75) #從哪滑動
y2 = int(l[1]*0.25) #滑到哪個位置
#滑動操作
while True:
driver.swipe(x1,y1,x1,y2)
time.sleep(0.5)
Android studio安裝與配置
https://www.jianshu.com/p/3aac8eace376
