常用Appium API
以最右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的更多相关文章
- 移动端自动化测试(二)之 Appium常用的API(python)函数介绍
上一章节已经介绍了Appium的环境搭建,其实只要掌握了Appium的工作原理,前期的准备工作和安装过程是比较简单的.那么当我们搭建好Appium环境后接下来做些什么呢?通常思路是开始appium的第 ...
- Appium常用的API函数
在学习应用一个框架之前,应该了解一下这个框架的整体结构或是相应的API函数.这篇文章还不错:http://blog.sina.com.cn/s/blog_68f262210102vzf9.html,就 ...
- Appium 常用的API函数
常用的API函数[转] http://blog.sina.com.cn/s/blog_68f262210102vzf9.html 获取信息类API (1)获取默认系统语言对应的Strings.xml文 ...
- 篇4 安卓app自动化测试-Appium API进阶
篇4 安卓app自动化测试-Appium API进阶 --lamecho辣么丑 1.1概要 大家好! 我是lamecho(辣么丑),今天是<安卓app自动化测试& ...
- (转载)中文Appium API 文档
该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...
- appium API接口
appium API接口 标签(空格分隔): appium常用api 1.contexts contexts(self) 返回当前会话的上下文,使用可以识别H5页面的控件: driver.contex ...
- 中文Appium API 文档
该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...
- React常用的API说明
楼主刚开始学习react,感受到了他的博大精深,看到很多莫名的用法,不知云云,找了很多没有找到参考手册,只有在中文社区和react官方看了一些,收集了一些比较常用的API,有补充的可以楼下评论补充.后 ...
- elasticsearch中常用的API
elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...
随机推荐
- 【C++】数组中的第k个最小元素
分治思想求解的问题,但是比较特殊,只有分解问题和求解小问题,不需要合并 每次也只需要经过判断,分解一半,所以比其他分解两边的效率高 最坏情况时间复杂度为O(n^2),期望时间复杂度为O(n) 找基准值 ...
- js 手机号验证
1 js 通过正则表达式对手机号进行验证 2 3 var reg = /^1[3|4|5|8][0-9]\d{4,8}$/; 4 var sMobile = document.mobileform.m ...
- python selenium 时间搜索框查询和日期大小比较
在做selenium自动化的时候遇到 时间搜索框查询(如下图)并比较查询结果是否在输入的时间之类. 首先,第一步要做的就是选择时间,并获取到所选时间的文本信息 如上图所示,获取到的时间搜索框并没有文本 ...
- python+selenium利用cookie记住密码
先上代码 1 from selenium import webdriver 2 from time import sleep 3 4 dr = webdriver.Chrome() 5 dr.get( ...
- JQuery案例:购物车编辑
购物车编辑 实现了:商品的加减,总价的变动 实现了:全选/全不选(使用prop而不是attr) 实现了:删除(遮罩层) <html> <head> <meta chars ...
- FL Studio——电音编曲人的奠基石
随着近年来摇滚.电音的发展,越来越多的人开始对电子音乐编曲感兴趣,而电音编曲的首要条件,就是需要一个好的DAW(数字音频工作站),常用的DAW有很多,例如Cubase.Nuendo.Pro Tools ...
- 企业安全06-Fastjson-CNVD-2017-02833
Fastjson-CNVD-2017-02833 一.漏洞概述 fastjson在解析json的过程中,支持使用@type字段来指定反序列化的类型,并调用该类的set/get方法来访问属性,当组件开启 ...
- Cloud-Native! 实战 Helm 3 部署 Traefik 2
介绍 Traefik 是什么? Traefik, The Cloud Native Edge Router Traefik 是一种现代 HTTP 反向代理和负载均衡器,用于轻松部署微服务. 这篇文章对 ...
- IntelliJ IDEA 2020.3正式发布,年度最后一个版本很讲武德
仰不愧天,俯不愧人,内不愧心.关注公众号[BAT的乌托邦],有Spring技术栈.MyBatis.JVM.中间件等小而美的原创专栏供以免费学习.分享.成长,拒绝浅尝辄止.本文已被 https://ww ...
- JMeter 安装 启动(即中文的修改)
一.安装 (1).java 和 apache-jmeter-4.0 2.点击apache-jmeter-4.0进入bin目录,点击jmeter.bat 如果没有安装java就会出现下图 遇到上面是因为 ...