以最右App为例

.apk文件网盘地址:

链接:https://pan.baidu.com/s/1L4MYkhpb5ECe8XeaneTx_Q

提取码:0jqm

操作类API
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 21:05 import unittest
import time
from appium import webdriver class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_operate_api(self):
self.driver.implicitly_wait(60)
time.sleep(8) # 控件点击:click()
# 场景:点击关注按钮
ele = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/title")
ele.click()
time.sleep(2) # 点击搜索按钮
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/ic_search_b").click()
time.sleep(3)
# 点击搜索输入框
ele_input = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/search_input")
ele_input.click()
# 控件输入
ele_input.send_keys("123456")
time.sleep(2)
print(ele_input.text)
ele_input.clear()
time.sleep(2) # 获取手机分辨率
height = self.driver.get_window_size()["height"]
width = self.driver.get_window_size()["width"]
print("手机分辨率是:" + str(height) + "*" + str(width)) if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
属性获取
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 21:05 import unittest
import time
from appium import webdriver class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_attribute(self):
self.driver.implicitly_wait(60)
time.sleep(8) # 获取当前活动页面
current_activity = self.driver.current_activity
print(current_activity) # 定位搜索按钮,赋值给ele_search
ele_search = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/ic_search_b") # 获取控件的属性值
# 获取控件的className
print(ele_search.get_attribute("className"))
# 获取控件的resourceId
print(ele_search.get_attribute("resourceId")) # 判断控件是否显示
print(ele_search.is_displayed())
# 判断控件是否可用
print(ele_search.is_enabled()) """
.ui.home.page.PageMainActivity
android.widget.ImageView
cn.xiaochuankeji.tieba:id/ic_search_b
True
True
执行结束!
""" if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
swipe
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 21:05 import unittest
import time
from appium import webdriver class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_swipe(self):
self.driver.implicitly_wait(60)
time.sleep(8) self.driver.swipe(500, 1000, 500, 2000) # 手势下拉
time.sleep(3)
self.driver.swipe(500, 2000, 500, 1000) # 手势上滑
time.sleep(3)
self.driver.swipe(500, 2000, 500, 2000, 100) # 点击
time.sleep(3)
self.driver.swipe(500, 2000, 500, 2000, 3000) # 长按
time.sleep(3) # 获取手机分辨率
height = self.driver.get_window_size()["height"]
width = self.driver.get_window_size()["width"]
print("手机分辨率是:" + str(height) + "*" + str(width))
# 按照手机分辨率定位
self.driver.swipe(width * 0.463, height * 0.448, width * 0.463, height * 0.896, 100) if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
TouchAction
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 19:26 import unittest
import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_touch_action(self):
self.driver.implicitly_wait(60)
time.sleep(8) # 点击控件
# 点击搜索控件
ele = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/ic_search_b")
TouchAction(self.driver).tap(ele).perform()
time.sleep(3) # 坐标点击
# 点击坐标(150,150)
# 点击搜索框,出现键盘
TouchAction(self.driver).tap(x=150, y=150).perform()
time.sleep(2) # 按压某个点
# 按压搜索框以外的空白处,键盘消失
TouchAction(self.driver).press(x=150, y=1040).release().perform()
time.sleep(2) # 返回到推荐页面
self.driver.keyevent(4)
time.sleep(3) # 长按某个点
# 长按第一条帖子
TouchAction(self.driver).long_press(x=500, y=600).release().perform()
time.sleep(2) TouchAction(self.driver).press(x=500, y=600).release().perform()
time.sleep(2) # 长按控件
# 长按第一条帖子
ele = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/holder_flow_rmdv")
TouchAction(self.driver).long_press(ele).perform() if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
keyevent

Android KEYCODE键值对:

参考地址:https://blog.csdn.net/shililang/article/details/14449527

# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 19:26 import unittest
import time
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_key_event(self):
time.sleep(8)
# 返回键
self.driver.keyevent(4)
self.driver.implicitly_wait(5)
# toast提示框
ele = WebDriverWait(self.driver, 20, 0.1).until(
EC.presence_of_element_located((By.XPATH, './/*[contains(@text,"再按一次返回键")]')))
print(ele.text)
# 再按一次返回键,退出程序
self.driver.keyevent(4) """
KEYCODE_MENU 菜单键82
KEYCODE_BACK 返回键4
KEYCODE_SEARCH 搜索键84
KEYCODE_ENTER 回车键66
KEYCODE_DEL 退格键67
KEYCODE_FORWARD_DEL 删除键112
......
"""
截图
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 19:18 import unittest
import time
from appium import webdriver class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
print("执行结束!") def test_screen_shot(self):
self.driver.implicitly_wait(60) # 场景:点击关注按钮,切换至关注页面,截图
ele = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/title")
ele.click()
time.sleep(2)
self.driver.get_screenshot_as_file(r"D:\Desktop\Testman_Study\图库\screen\test.png") if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
中文输入法
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 18:44 import unittest
import time
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait class AndroidTest(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "10"
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' # 输入中文
desired_caps['unicodeKeyboard'] = 'True' # 安装appium自带输入法
desired_caps['resetKeyboard'] = 'True' # 重置键盘,修改默认输入法 self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps) def tearDown(self):
print("执行完成!") def test_chinese(self):
time.sleep(8)
# 点击第一条帖子的评论按钮
el = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/middle_view").click()
time.sleep(5)
# 点击评论输入框
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/etInput").click()
time.sleep(4)
# 输入评论内容12346
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/etInput").send_keys('真不错!')
time.sleep(2)
# 点击发送
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/send").click()
# 通过xpath,text属性定位toast提示框
# toast_loc = ("xpath", './/*[contains(@text,"评论发送成功")]')
# 显式等待
ele = WebDriverWait(self.driver, 20, 0.1).until(
expected_conditions.presence_of_element_located((By.XPATH, './/*[contains(@text,"评论发送成功")]')))
# 输出toast提示框的文本内容
print(ele.text)
time.sleep(2)
self.driver.keyevent(4)
重置App
# -*- coding:utf-8 -*-
# 作者:IT小学生蔡坨坨
# 时间:2020/12/22 19:06 import os
import unittest
from appium import webdriver PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
) class AndroidTest(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "10"
desired_caps['deviceName'] = 'Android Emulator' # 重置app
desired_caps['fullReset'] = "true"
# .apk文件的路径
desired_caps['app'] = r"D:\Desktop\Testman_Study\apk\zuiyou518.apk"
# desired_caps['app'] = PATH("D:\Desktop\Testman_Study\apk\zuiyou518.apk") desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity' self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps) def tearDown(self):
print("执行完成!") def test_resetApp(self):
pass
toast
import os
import unittest
import time
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By # Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
) class AndroidTests(unittest.TestCase):
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['noReset'] = 'True'
desired_caps['appPackage'] = 'cn.xiaochuankeji.tieba'
desired_caps['appActivity'] = '.ui.base.SplashActivity'
# Appium1.6.3版本后才支持toast,之前封装的是Uiautomator,之后才是Uiautomator2
desired_caps['automationName'] = 'Uiautomator2'
desired_caps['newCommandTimeout'] = 200 self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self):
# self.driver.quit()
print("执行结束!") def test_toast(self):
"""
toast:
Android中的Toast是一种简易的消息提示框;
告知用户任务状态,操作结果,例如:发送成功,加载中,删除成功;
Toast会在屏幕所有层的最上方;
显示时间有限,1s+左右消失。 场景:评论帖子->点击发送->提示评论发送成功的灰黑框->1-2s后消失
"""
# self.driver.implicitly_wait(160)
time.sleep(8)
# 点击第一条帖子的评论按钮
el = self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/middle_view").click()
time.sleep(5)
# 点击评论输入框
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/etInput").click()
time.sleep(4)
# 输入评论内容12346
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/etInput").send_keys('123456')
time.sleep(2)
# 点击发送
self.driver.find_element_by_id("cn.xiaochuankeji.tieba:id/send").click()
# 通过xpath,text属性定位toast提示框
# toast_loc = ("xpath", './/*[contains(@text,"评论发送成功")]')
# 显式等待
ele = WebDriverWait(self.driver, 20, 0.1).until(
EC.presence_of_element_located((By.XPATH, './/*[contains(@text,"评论发送成功")]')))
# 输出toast提示框的文本内容
print(ele.text)
time.sleep(2)
self.driver.keyevent(4) if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)

常用Appium API的更多相关文章

  1. 移动端自动化测试(二)之 Appium常用的API(python)函数介绍

    上一章节已经介绍了Appium的环境搭建,其实只要掌握了Appium的工作原理,前期的准备工作和安装过程是比较简单的.那么当我们搭建好Appium环境后接下来做些什么呢?通常思路是开始appium的第 ...

  2. Appium常用的API函数

    在学习应用一个框架之前,应该了解一下这个框架的整体结构或是相应的API函数.这篇文章还不错:http://blog.sina.com.cn/s/blog_68f262210102vzf9.html,就 ...

  3. Appium 常用的API函数

    常用的API函数[转] http://blog.sina.com.cn/s/blog_68f262210102vzf9.html 获取信息类API (1)获取默认系统语言对应的Strings.xml文 ...

  4. 篇4 安卓app自动化测试-Appium API进阶

    篇4                 安卓app自动化测试-Appium API进阶 --lamecho辣么丑 1.1概要 大家好! 我是lamecho(辣么丑),今天是<安卓app自动化测试& ...

  5. (转载)中文Appium API 文档

    该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...

  6. appium API接口

    appium API接口 标签(空格分隔): appium常用api 1.contexts contexts(self) 返回当前会话的上下文,使用可以识别H5页面的控件: driver.contex ...

  7. 中文Appium API 文档

    该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...

  8. React常用的API说明

    楼主刚开始学习react,感受到了他的博大精深,看到很多莫名的用法,不知云云,找了很多没有找到参考手册,只有在中文社区和react官方看了一些,收集了一些比较常用的API,有补充的可以楼下评论补充.后 ...

  9. elasticsearch中常用的API

    elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...

随机推荐

  1. 从数据仓库双集群系统模式探讨,看GaussDB(DWS)的容灾设计

    摘要:本文主要是探讨OLAP关系型数据库框架的数据仓库平台如何设计双集群系统,即增强系统高可用的保障水准,然后讨论一下GaussDB(DWS)的容灾应该如何设计. 当前社会.企业运行当中,大数据分析. ...

  2. python接口测试3-JSON格式

    什么是JSON? 一种轻量级的数据交换格式.它独立于语言和平台,JSON解析器和JSON库支持不同的编程语言.JSON具有自我描述性,很容易理解. 数据格式: { "name":& ...

  3. 2017年第八届蓝桥杯【C++省赛B组】B、C、D、H 题解

    可能因为我使用暴力思维比较少,这场感觉难度不低. B. 等差素数列 #暴力 #枚举 题意 类似:\(7,37,67,97,127,157\) 这样完全由素数组成的等差数列,叫等差素数数列. 上边的数列 ...

  4. 安装Linux软件时遇到这个问题,如何解决?

    提示 Could not get lock /var/lib/dpkg/lock 报错? 有些小伙伴在使用 apt 包管理器更新或安装软件时,可能会遇到过诸如以下的错误提示: E: Could not ...

  5. TkMybatis 是什么?

    一.TkMybatis Tkmybatis 是基于 Mybatis 框架开发的一个工具,通过调用它提供的方法实现对单表的数据操作,不需要写任何 sql 语句,这极大地提高了项目开发效率. 二.怎么用? ...

  6. MySQL慢查询日志(SLOW LOG)

    慢查询日志可以帮助DBA或开发人员定位可能存在问题的SQL语句,从而进行优化. 如何开启 默认情况下,MySQL是不开启慢查询日志的.可以通过以下命令查看是否开启: mysql> SHOW VA ...

  7. 第14章 web前端开发小白学爬虫结束语

    老猿学爬虫应该是2019年7月初开始的,到现在2个多月了,有段时间了,这部分一直是老猿期待能给大家带来收获的,因为老猿爬虫实战应用的场景与网上老猿已知的场景基本都不一样,是从复用网站登录会话信息来开发 ...

  8. PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的按钮改变缺省文字的方法

    在Qt Designer中可以预先定义标准按钮,相关支持的标准按钮请见<PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的StandardButton ...

  9. Microsoft工具之Disk2vhd

    Official documents:https://docs.microsoft.com/zh-cn/sysinternals/downloads/disk2vhd 1.Introduction D ...

  10. [ASIS 2019]Unicorn shop

    点击进去之后是一个购买独角兽的界面,有四种类型的独角兽,前三种的价格比较便宜,最后的独角兽价格比较贵. 我们先尝试购买前三种独角兽,输入id,然后price输入9 然后就告诉我商品错了,可能复现靶场这 ...