Appium
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