Appium 是一个自动化测试 工具,其功能是封装了 将appium.webdriver代码转化为android、ios 控制命令。下面以android为列:
比如连接android 是appium 发送的adb命令,获取android版本号:
[ADB] Trying to find a connected android device
[ADB] Getting connected devices...
[ADB] Connected devices: [{"udid":"4b36aa0c","state":"device"}]
[AndroidDriver] Looking for a device with Android '10.0.0'
[ADB] Setting device id to 4b36aa0c
[ADB] Getting device platform version
[ADB] Running 'F:\Android\Sdk\platform-tools\adb.exe -P 5037 -s 4b36aa0c shell getprop ro.build.version.release'
[ADB] Current device property 'ro.build.version.release': 10
测试app 时 第一步是要分析app结构编写控制脚本,官方一般推荐adb 自带的uiautomatorviewer(位于android sdk\tools\bin),效果如下:
uiautomatorviewer 够用但是不够强大,而且写代码要开两个工具(强迫症不能忍),所以小编推荐使用appium自带工具。下面介绍如何打开、配置appium 自带布局检测工具:
1、添加新会话
2、配置参数
参数json:
{
"platformName": "android",
"noReset": true,
"simpleIsVisibleCheck": true,
"platformVersion": "10",
"deviceName": "4b36aa0c23",
"id": "Xiaomi-MI 8 SE",
"appPackage": "cn.youth.news",
"appActivity": "com.weishang.wxrd.activity.SplashActivity"
}
appPackage、appActivity 为apk包名和启动类名称,可以使用adb 自带aapt获取
deviceName 为adb devices 获取的设备名称,
noReset 是否清除上次打开app的会话信息
3、主界面start server ,然后当前页面starta session ,效果如图,
由上图可以看出列举了各种选择器,和整个页面布局结构。现在可以直接拷贝选择器写脚本了 (python,eg)
class Qing:
def __init__(self, app):
self.app = app
def set_app_info(self, desire_caps):
desire_caps['appPackage'] = 'cn.youth.news'
desire_caps['appActivity'] = 'com.weishang.wxrd.activity.SplashActivity'
return desire_caps
def run(self, driver):
time.sleep(7)
driver.press_keycode(4)
TouchAction(driver).tap(x=200, y=500).perform()
time.sleep(2)
driver.press_keycode(4)
while self.app.times - self.app.executeTimes > 0:
try:
self.scrollItem(driver)
self.readArticle(driver)
self.app.executeTimes = self.app.executeTimes + 1
self.app.executeAt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
ses.commit()
except:
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ':exception')
driver.press_keycode(4)
driver.quit()
def scrollItem(self, driver):
try:
time.sleep(2)
print("::" + threading.current_thread().name + "::" + time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime()) + ":next")
title = driver.find_element_by_id('cn.youth.news:id/tv_article_title')
# print(title.text)
ad = Reader()
ad.title = title.text
ad.createAt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
exists = ses.query(Reader).filter_by(title=title.text).all()
if len(exists) <= 0:
ses.add(ad)
title.click()
ses.commit()
else:
el = driver.find_element_by_id("cn.youth.news:id/tv_home_tab")
el.click()
time.sleep(2)
self.scrollItem(driver)
except Exception as e:
print(str(e))
def readArticle(self, driver):
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":read")
try:
# el1 = driver.find_element_by_class_name('android.webkit.WebView')
# action1 = TouchAction(driver)
# for x in range(0, 11):
# time.sleep(2)
# action1.press(el1).move_to(x=0, y=x / 10).release().perform()
size = driver.get_window_size()
width = size["width"]
height = size["height"]
x1 = width * 0.5
y1 = height * 0.9
y2 = height * 0.4
for i in range(0, 7):
time.sleep(4)
driver.swipe(x1, y1, x1, y2)
time.sleep(10)
except Exception as e:
print(str(e))
print('read exception')
finally:
driver.press_keycode(4)
问题交流请加页脚q群