1.安装node.js & Appium
1.1 下载 node.js 建议最高版本: https://nodejs.org/download/
1.2. tar -xvzf xxx.tar.gz
1.3. set classpath
1.4. `npm install appium
遇翻墙
npm install -g cnpm --registry=https://registry.npm.taobao.org`cnpm install -g appium --unsafe-perm
1.5. appium -v
2.下载MuMu安卓模拟器
2.1 连接mumu虚拟机
这个是设备名称,因为我们是通过安卓的adb连接虚拟机的,需要在控制台执行如下的命令,让adb连接上虚拟机:
adb connect 127.0.0.1:7555
2.2 获取deviceName
执行了上面的连接模拟器的命令后,在cmd控制台输入:adb devices,查看目前连接的虚拟机,显示如下,图片里面的"127.0.0.1:7555" 就是设备的名称
List of devices attached
127.0.0.1:7555 device
2.3 连接appium 127.0.0.1:4723
对应的地址,端口是appium启动的时候,填写的地址,端口,如下图
3. 启动
根据前面的环境配置,操作后,运行脚本,在虚拟机里面可以启动qq,并且看到虚拟机里面多了如下两个图标,就表示成功。
4.python脚本测试
# -*- coding: utf-8 -*-
"""
Create by Mr.Hao on 2019/7/8.
"""
import time
import config
from time import sleep
from appium import webdriver
##以下代码可以操控手机app
class Action():
"""
真机
self.desired_caps = {
"platformName": "Android",
"deviceName": "GM1910",
"platformVersion":"9", # 安卓版本
"app": 'E:\pro\\roboter\\config\\autohome.apk', # 此处测试需要下载app路径
'autoGrantPermissions':True,
}
模拟器使用路径
self.desired_caps = {
"platformName": "Android",
"deviceName": "MuMu",
"platformVersion": "6.0.1", # 安卓版本
"app": 'E:\pro\\roboter\\config\\autohome.apk', # 此处测试需要下载app路径
'autoGrantPermissions':True,
}
"""
def __init__(self):
# 初始化配置,设置Desired Capabilities参数
self.desired_caps = {
"platformName": "Android",
"deviceName": "mate9",
"platformVersion": "6.0.1", # 安卓版本
'appActivity':'.MainActivity',
'appPackage':'com.cubic.autohome'
}
# 指定Appium Server
self.server = 'http://localhost:4723/wd/hub'
# 新建一个Session
self.driver = webdriver.Remote(self.server, self.desired_caps)
# 设置滑动初始坐标和滑动距离
self.start_x = 500
self.start_y = 1500
self.distance = 1300
# self.driver = webdriver.Remote(webdriver_remote, sys_pras)
def main(self):
print "程序开始"
# 获取屏幕的高
x = self.driver.get_window_size()['width']
# 获取屏幕宽
y = self.driver.get_window_size()['height']
# 向下滑动
print "APP_page向下滑动===========》"
time.sleep(10)
# self.driver.swipe(self.start_x, self.start_y, self.start_x, self.start_y - self.distance)
# # 设置延时等待
self.driver.swipe(0.5 * x, 0.75 * y, 0.5 * x, 0.25 * y, 1000)
sleep(3)
self.driver.swipe(0.5 * x, 0.75 * y, 0.5 * x, 0.25 * y, 1000)
sleep(3)
i=0
while True:
try:
print "进入详情页面"
self.driver.find_element_by_xpath("//android.widget.ListView/android.widget.LinearLayout[1]").click()
time.sleep(3)
print "打印详情页HTML"
print self.driver.page_source
except Exception as e:
print e
# self.scroll(x,y)
print "点击返回......"
xpathlist = ['//*[@resource-id="com.autohome.main.article:id/article_nav_left"]',
'//*[@resource-id="com.autohome.main.article:id/action_back"]',
'//android.widget.ImageView[@clickable="true" and @bounds="[33,107][120,194]"]',
'//android.widget.ImageView[@clickable="true" and @bounds="[15,47][58,90]"]',
'//android.widget.ImageView[@resource-id="com.autohome.main.car:id/tv_back"]',
'//android.widget.ImageView[@resource-id="com.cubic.autohome:id/ahlib_close_activity"]',
'//android.widget.ImageView[@resource-id="com.autohome.plugin.koubei:id/main_return"]',
'//android.widget.ImageView[@resource-id="com.autohome.plugin.uchuang:id/final_page_nav_left_icon"]']
for xx in xpathlist:
try:
self.driver.find_element_by_xpath(xx).click()
time.sleep(5)
except:
continue
time.sleep(3)
print "APP 第{}次刷新".format(i)
self.driver.swipe(0.5 * x, 0.75 * y, 0.5 * x, 0.25 * y, 1000)
sleep(5)
i = i+1
if __name__ == '__main__':
action = Action()
action.main()