python+appium

目录

一、环境搭建

1、安装JDK(1.8.0)并配置环境

2、安装Andorid SDK(建议使用29版本)

3、安装NodeJs

4、安装appium

5、安装appium-python-client

二、连接

1、adb连接设备

2、开启Appium Server

3、 python脚本

三、API Documentation - Appium

1、app

2、元素

3、交互


一、环境搭建

1、安装JDK(1.8.0)并配置环境

 1、下载

        下载JDK文件,点击运行安装
 2、配置环境变量
     (1)新建一个名为JAVA_HOME的环境变量,变量值为JDK的安装路径
     (2)将%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin添加到path中
     (3)新建一个名为classpath的环境变量,变量值为:
          %JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
 3、验证

        cmd窗口中输入java -version

2、安装Andorid SDK(建议使用29版本)

1、下载

        下载Andorid SDK文件,点击进行安装
2、安装相关工具(默认前三个)

        双击Andorid SDK Manager,选择前三个tools点击安装

3、配置环境变量
   (1)新建ANDROID_HOME变量,变量值为Andorid SDK的路径
   (2)将%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\tools添加到path中
4、验证

        cmd窗口中输入adb,出现版本号则说明安装成功

3、安装NodeJs

1、下载

        下载NodeJS文件,点击进行安装
2、配置环境变量

        自动

3、验证

        cmd窗口中输入node -v,npm -v

4、安装appium

1、下载

        下载appium-desktop文件,点击进行安装
2、配置环境变量

5、安装appium-python-client

1、pip install -U appium-python-client

二、连接

1、adb连接设备

1、打开手机或模拟器开发者模式

2、win+r打开cmd

3、adb connect 127.0.0.1:21503

4、adb devices -l

5、adb shell dumpsys window |findstr mCurrent

2、开启Appium Server

{
  "deviceName": "127.0.0.1:62001",
  "appPackage": "com.android.launcher3",
  "appActivity": "com.android.launcher3.launcher3.Launcher",
  "platformName": "Android",
  "noReset": true
}

3、 python脚本

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
import time

desired_caps = {
    "deviceName": "127.0.0.1:62001",
    "appPackage": "com.hypergryph.arknights",
    "appActivity": "com.u8.sdk.U8UnityContext",
    "platformName": "Android",
    "noReset": True,
    "newCommandTimeout": 600
}

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
actions = TouchAction(driver)
# start、开始唤醒、取得神经连接
time.sleep(30)
actions.tap(x=635, y=515).perform()
time.sleep(15)
actions.tap(x=635, y=515).perform()
time.sleep(40)
# 签到
actions.tap(x=591, y=591).perform()
time.sleep(5)
# 关闭签到
actions.tap(x=1216, y=79).perform()
time.sleep(5)
# 关闭公告
actions.tap(x=1236, y=74).perform()
time.sleep(5)
# 点击当前
actions.tap(x=929, y=220).perform()
time.sleep(5)
# 点击前往上一次作战
actions.tap(x=1137, y=602).perform()
time.sleep(10)
# 循环刷取关卡
for i in range(2):
    actions.tap(x=1144, y=671).perform()
    time.sleep(10)
    actions.tap(x=1103, y=604).perform()
    time.sleep(180)
    actions.tap(x=1137, y=602).perform()
    time.sleep(15)

driver.quit()

三、API Documentation - Appium

1、app

# 安装
driver.install_app('/Users/johndoe/path/to/app.apk')
# 是否已安装
driver.is_app_installed('com.example.AppName')
# 启动
driver.launch_app()
# 后台应用程序
driver.background_app(10)
# 关闭应用程序
driver.close_app()
# 重启应用程序
driver.reset()
# 删除应用程序
driver.remove_app('com.example.AppName')
# 激活应用程序
driver.activate_app('com.apple.Preferences')
driver.activate_app('io.appium.android.apis')
# 终止程序
driver.terminate_app('com.apple.Preferences')
driver.terminate_app('io.appium.android.apis')
# 获取应用状态
driver.query_app_state('com.apple.Preferences')
driver.query_app_state('io.appium.android.apis')
# 得到应用的字符串
appStrings = driver.app_strings("en", "/path/to/file")
# 结束测试覆盖率
driver.end_test_coverage("Intent", "/path")

2、元素

# 查找元素
driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="SomeAccessibilityID")
driver.find_elements(AppiumBy.ACCESSIBILITY_ID,'SomeAccessibilityID')
# 行为
el = driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value='SomeId')
el.click()
el.send_keys('Hello world!')
el.clear()
# 元素
el = self.driver.find_element_by_accessibility_id('SomeAccessibilityID')
el.text
el.tag_name
el.get_attribute('content-desc')
el.is_selected()
el.is_enabled()
el.is_displayed()
el.location
el.size

3、交互

from appium.webdriver.common.touch_action import TouchAction
# ...
actions = TouchAction(driver)

# 单次点击
actions.tap(element)
# 双击
actions.double_tap(element)
# 移动
actions.tap_and_hold(element)
actions.move_to(element, 50, 50)
# 向下触摸
actions.tap_and_hold(element)
actions.move(50, 50)
# 向上触摸
actions.tap_and_hold(element)
actions.release(50, 50)
# 长按
actions.long_press(element)
# 滚动
actions.scroll_from_element(element, 10, 100)
actions.scroll(10, 100)
# 使用手指运动事件在触摸屏上轻拂
actions.flick_element(element, 1, 10, 10)
# 触摸执行
actions.tap_and_hold(20, 20)
actions.move_to(10, 100)
actions.release()

actions.perform()
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值