根据课堂视频,安装搭建Appium运行环境,并运行示例代码
安装Appium Python Client 包的命令
pip install Appium-Python-Client
下载安装Appium Desktop的安装包, 下载地址 https://github.com/appium/appium-desktop/releases/latest 下载扩展名为.exe的包
官方中文网站 https://developer.android.google.cn/studio/archive.html 选择2.3.3 版本,包含了sdk的安装包 Windows IDE bundle with SDK (64-bit)
特别注意,安装程序要求路径中最好不要有空格。
到 oracle 官方网站下载JDK 1.8 的安装包,进行安装
先试试android studio里面自带的模拟器
打开 studio,创建一个项目,通过tools - android - AVD Manager菜单创建一个安卓模拟设备
如果不行,可以试试Genymotion,安装过程参考 https://github.com/jcyrss/songqin-testdev/issues/3
dfgdfg
https://github.com/jcyrss/songqin-testdev/raw/master/appium/uploads/duoduoCalculators.apk
# coding=utf8
from appium import webdriver
import time,traceback
desired_caps = {}
desired_caps[‘platformName‘] = ‘Android‘
desired_caps[‘platformVersion‘] = ‘6‘
desired_caps[‘deviceName‘] = ‘test‘
desired_caps[‘app‘] = r‘d:\apk\duoduoCalculators.apk‘
desired_caps[‘appPackage‘] = ‘com.ibox.calculators‘
desired_caps[‘appActivity‘] = ‘.SplashActivity‘
desired_caps[‘unicodeKeyboard‘] = True
desired_caps[‘noReset‘] = True
desired_caps[‘newCommandTimeout‘] = 6000
#启动Remote RPC
driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps)
driver.implicitly_wait(10)
try:
# -----------------
num3 = driver.find_element_by_id("com.ibox.calculators:id/digit3")
num5 = driver.find_element_by_id("com.ibox.calculators:id/digit5")
num9 = driver.find_element_by_id("com.ibox.calculators:id/digit9")
plus = driver.find_element_by_id("com.ibox.calculators:id/plus")
mul = driver.find_element_by_id("com.ibox.calculators:id/mul")
equal = driver.find_element_by_id("com.ibox.calculators:id/equal")
num3.click()
plus.click()
num9.click()
equal.click()
mul.click()
num5.click()
equal.click()
# 检查结果,需要我们去找结果对应的界面元素,发现是下面这个TextView
# 研究发现没有特点,大家想想我们该怎么办
# 可以看看父节点有没有唯一标识,发现 父节点是有id的,
# 就可以怎么样?
# 先查找父节点,
# 再根据父节点元素 调用 find element 就是在父节点的范围内 查找
retLayout = driver.find_element_by_id(‘com.ibox.calculators:id/cv‘)
retTvs = retLayout.find_elements_by_class_name(‘android.widget.TextView‘)
retStr = retTvs[1].text
print(retStr)
if retStr == ‘60‘:
print(‘pass‘)
else:
print(‘fail‘)
# -----------------
except:
print(traceback.format_exc())
input(‘**** Press to quit..‘)
driver.quit()
找到一个安卓设备(没有可以向朋友借用一下),将其连接到电脑上,根据课堂视频指导,确保可以被命令 adb devices -l 检测到
安装多多计算器(怎么装?自己想办法)
练习用 Appium Desktop中的 inspector查看界面。
将上次作业,用xpath方式定位元素,再实现一遍
# coding=utf8
from appium import webdriver
import time,traceback
desired_caps = {}
desired_caps[‘platformName‘] = ‘Android‘
desired_caps[‘platformVersion‘] = ‘7.1‘ #
desired_caps[‘deviceName‘] = ‘test‘
desired_caps[‘app‘] = r‘d:\apk\duoduoCalculators.apk‘
desired_caps[‘appPackage‘] = ‘com.ibox.calculators‘ #
desired_caps[‘appActivity‘] = ‘com.ibox.calculators.SplashActivity‘
desired_caps[‘unicodeKeyboard‘] = True
desired_caps[‘noReset‘] = True
desired_caps[‘newCommandTimeout‘] = 6000
driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps)
driver.implicitly_wait(10)
try:
# -----------------
num3 = driver.find_element_by_id(‘com.ibox.calculators:id/digit3‘)
num9 = driver.find_element_by_id(‘com.ibox.calculators:id/digit9‘)
num5 = driver.find_element_by_id(‘com.ibox.calculators:id/digit5‘)
plus = driver.find_element_by_id(‘com.ibox.calculators:id/plus‘)
equal = driver.find_element_by_id(‘com.ibox.calculators:id/equal‘)
mul = driver.find_element_by_id(‘com.ibox.calculators:id/mul‘)
num3.click()
plus.click()
num9.click()
equal.click()
mul.click()
num5.click()
equal.click()
# ----------------------
xpath = ‘//*[@resource-id="com.ibox.calculators:id/cv"]/android.widget.TextView[2]‘
ele = driver.find_element_by_xpath(xpath)
retStr = ele.text
# ----------------------
print(retStr)
if retStr == ‘60‘:
print(‘pass‘)
else:
print(‘fail!‘)
# -----------------
except:
print(traceback.format_exc())
input(‘**** Press to quit..‘)
driver.quit()
找到一个安卓设备(没有可以向朋友借用一下)
到华为官网 http://app.hicloud.com/ ,微信扫码右侧二维码,安装华为应用市场apk,
如果手机安装后不能打开的,可以点击这里下载老版本安装,老版本安装运行后,不要选择更新到新版本。
进入排行页面,滚动到 口碑最佳 部分
打印出所有 口碑最佳 部分的5个应用名称
# coding=utf8
from appium import webdriver
import time
import traceback
desired_caps = {}
desired_caps[‘platformName‘] = ‘Android‘
desired_caps[‘platformVersion‘] = ‘6‘
desired_caps[‘deviceName‘] = ‘test‘
desired_caps[‘app‘] = r‘d:\apk\HiSpace.apk‘
desired_caps[‘appPackage‘] = ‘com.huawei.appmarket‘ #app package名,指定了要运行的app
desired_caps[‘appActivity‘] = ‘com.huawei.appmarket.MainActivity‘ #app默认Activity
desired_caps[‘unicodeKeyboard‘] = True
desired_caps[‘noReset‘] = True
desired_caps[‘newCommandTimeout‘] = 6000
driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps) #启动Remote RPC
driver.implicitly_wait(10)
try:
# ------------------------------
javaCode = ‘new UiSelector().resourceId("com.huawei.appmarket:id/tabLayout").childSelector(new UiSelector().text("排行") )‘
driver.find_element_by_android_uiautomator(javaCode).click()
javaCode = ‘new UiSelector().text("总榜").resourceId("com.huawei.appmarket:id/ItemTitle")‘
ele = driver.find_element_by_android_uiautomator(javaCode)
destPosY = ele.location[‘y‘]
xPos = ele.location[‘x‘]
driver.implicitly_wait(0.5)
while True:
driver.swipe(xPos,destPosY,xPos,destPosY-100, 1000)
javaCode = ‘new UiSelector().text("口碑最佳").resourceId("com.huawei.appmarket:id/ItemTitle")‘
eles = driver.find_elements_by_android_uiautomator(javaCode)
if not eles:
continue
driver.swipe(xPos,eles[0].location[‘y‘],xPos,destPosY,3000)
break
driver.implicitly_wait(10)
eles = driver.find_elements_by_class_name("android.widget.TextView")
tvs = []
for ele in eles:
tvs.append(ele.text)
tvsStr = ‘|||‘.join(tvs)
pos1 = tvsStr.find(‘口碑最佳|||‘)
targetStr = tvsStr[pos1:]
def getName(No): #‘1‘, ‘2‘
tp1 = targetStr.find(No+‘|||‘) + 4
tp2 = targetStr.find(‘|||‘,tp1)
return targetStr[tp1:tp2]
print(‘================ finally ++++++++++++ \n‘)
for i in range(1,6):
print(getName(str(i)))
# ------------------------------
except:
print(traceback.format_exc())
input(‘**** Press to quit..‘)
driver.quit()
# coding=utf8
from appium import webdriver
import time,traceback
desired_caps = {}
desired_caps[‘platformName‘] = ‘Android‘
desired_caps[‘platformVersion‘] = ‘5.1‘
desired_caps[‘deviceName‘] = ‘test‘
desired_caps[‘app‘] = r‘e:\apk\toutiao.apk‘
desired_caps[‘appPackage‘] = ‘io.manong.developerdaily‘
desired_caps[‘appActivity‘] = ‘io.toutiao.android.ui.activity.LaunchActivity‘
desired_caps[‘unicodeKeyboard‘] = True
desired_caps[‘noReset‘] = True
desired_caps[‘newCommandTimeout‘] = 6000
#启动Remote RPC
driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps)
print driver.session_id
driver.implicitly_wait(10)
try:
# -----------------
# 用下面的表达式,也可以用xpath
code = u‘new UiSelector().resourceId("io.manong.developerdaily:id/btn_item").instance(0).childSelector(new UiSelector().className("android.widget.TextView"))‘
ele1 = driver.find_element_by_android_uiautomator(code)
text1 = ele1.text
print text1
ele1.click()
time.sleep(2)
ele2 = driver.find_element_by_id(‘io.manong.developerdaily:id/tv_title‘)
text2 = ele2.text
print text2
if text2 == text1:
print ‘pass‘
else:
print ‘fail‘
driver.press_keycode(4)
# 检查是否回到刚才的页面
ele2 = driver.find_elements_by_id(‘io.manong.developerdaily:id/tab_bar_plus‘)
if ele2:
print ‘we return back‘
# -----------------
except:
print traceback.format_exc()
driver.quit()
原文:https://www.cnblogs.com/xiao-qing/p/9230905.html